Scrivere un trigger complesso
-
05-07-2019 - |
Domanda
Questo post è un aggiornamento (con codice trigger) rispetto al mio post precedente di ieri
Sto usando SQL Server 2000. Sto scrivendo un trigger che viene eseguito quando un campo Applicant.AppStatusRowID
Il candidato alla tabella è collegato alla posizione della tabella, alla tabella Company & amp; tabella AppStatus.
Il mio problema è creare i join nella mia query.
Quando si aggiorna Applicant.AppStatusRowID, desidero ottenere i valori da Applicant.AppStatusRowID, Applicant.FirstName, Applicant.Lastname, Location.LocNumber, Location.LocationName, Company.CompanyCode, AppStatus.DisplayText
I join sarebbero:
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
Questo deve essere inserito in una tabella di controllo (i campi sono ID richiedente, Cognome, Nome, Data, Ora, Azienda, Numero posizione, Nome posizione, Posizione stato, Utente)
La query trigger è:
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
Qualche idea?
Soluzione
Prova con questo codice
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
con questo codice si ottiene la riga eliminata dall'aggiornamento (valore_vecchio)
ci vediamo.