Frage

Dieser Beitrag ist ein Update (mit Trigger-Code) aus meinem früheren Posting gestern

Ich bin mit SQL Server 2000. Ich einen Trigger schreibe, die ausgeführt wird, wenn ein Feld Applicant.AppStatusRowID

Tabelle Antragsteller ist mit Tisch Lage, Tisch Unternehmen & Tabelle AppStatus.

Mein Problem ist die Schaffung der schließt sich in meiner Anfrage.

Wenn Applicant.AppStatusRowID aktualisiert wird, möchte ich die Werte aus Applicant.AppStatusRowID, Applicant.FirstName bekommen, Applicant.Lastname, Location.LocNumber, Location.LocationName, Company.CompanyCode, AppStatus.DisplayText

Das schließt sich wäre:

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 

Dies ist in eine Audit-Tabelle eingefügt werden (Felder sind ApplicantID, Nachname, Vorname, Datum, Zeit, Unternehmen, Standort Anzahl, Ort Name, StatusDisposition, Benutzer)

Die Trigger-Abfrage ist:

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

Irgendwelche Ideen?

War es hilfreich?

Lösung

Versuchen Sie es mit diesem 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

Mit diesem Code erhalten Sie die Update-gelöschte Zeile (die old_value)

Sie sehen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top