Écrire un déclencheur complexe
-
05-07-2019 - |
Question
Cette publication est une mise à jour (avec le code de déclenchement) de ma précédente publication hier
J'utilise SQL Server 2000. J'écris un déclencheur qui est exécuté lorsqu'un champ Applicant.AppStatusRowID
Le nom de la table est lié à la table Lieu, table Société & amp; table AppStatus.
Mon problème est de créer les jointures dans ma requête.
Lorsque Applicant.AppStatusRowID est mis à jour, je souhaite obtenir les valeurs de Applicant.AppStatusRowID, Applicant.FirstName, Demandant.Lastname, Location.LocNumber, Location.LocationName, Company.CompanyCode, AppStatus.DisplayText
.Les jointures seraient:
Select * from Applicant A
Inner Join AppStatus ast on ast.RowID = a.AppStatusRowID
Inner Join Location l on l.RowID = a.LocationRowID
Inner Join Company c on c.RowID = l.CompanyRowID
Ceci doit être inséré dans une table d'audit (les champs sont: demandeur, nom, prénom, date, heure, entreprise, numéro d'emplacement, nom de l'emplacement, état de la disposition, utilisateur)
La requête de déclenchement est:
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update
AS
declare @Approwid int
Declare @triggername sysname
Set @rowCnt = @@rowcount
Set @triggername = object_name(@@procid)
If @rowCnt = 0
RETURN
If Update(appstatusrowid)
BEGIN
-----------------------------------------------------------------------------
-- insert a record to the AppDispo table, if AppstatusRowid
-- is being Updated
-----------------------------------------------------------------------------
Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
StatusDispo,[Username])
Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
SUSER_SNAME()+' '+User
From deleted d with(nolock)
Inner join Test_App a with (nolock) on a.RowID = d.rowid
inner join location l with (nolock) on l.rowid = d.Locationrowid
inner join appstatus ast with (nolock) on ast.rowid = d.appstatusrowid
inner join company c with (nolock) on c.rowid = l.CompanyRowid
--Inner Join Deleted d ON a.RowID = d.RowID
--Where (a.rowid = @Approwid)
END
GO
Des idées?
La solution
Essayez avec ce code
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER tri_UpdateAppDispo ON dbo.Test_App
For Update
AS
declare @Approwid int
Declare @triggername sysname
Set @rowCnt = @@rowcount
Set @triggername = object_name(@@procid)
If @rowCnt = 0
RETURN
If Update(appstatusrowid)
BEGIN
-----------------------------------------------------------------------------
-- insert a record to the AppDispo table, if AppstatusRowid
-- is being Updated
-----------------------------------------------------------------------------
Insert AppDispo(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName,
StatusDispo,[Username])
Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,l.locnum,l.locname, ast.Displaytext,
SUSER_SNAME()+' '+User
From deleted d with(nolock),location l with (nolock),appstatus ast with (nolock),company c with (nolock)
where d.Locationrowid =l.rowid and
d.appstatusrowid = ast.rowid and
c.rowid = l.CompanyRowid
END GO
avec ce code, vous obtenez la ligne mise à jour-supprimée (old_value)
à bientôt.