2009-10-21 47 views
0

此公告是從我早期發佈的更新(以觸發代碼)昨日編寫複雜的觸發

我使用SQL Server 2000中我寫的時候現場Applicant.AppStatusRowID

時執行的觸發

表申請人鏈接到表位置,表公司&表AppStatus。

我的問題是在我的查詢中創建連接。

當Applicant.AppStatusRowID更新,我想從Applicant.AppStatusRowID,Applicant.FirstName,Applicant.Lastname,Location.LocNumber,Location.LocationName,Company.CompanyCode,AppStatus.DisplayText

的值聯接將是:

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 

這是要插入到審覈表(字段ApplicantID,姓氏,名字,日期,時間,公司,地點數量,位置名稱,StatusDisposition,用戶)

的特里格呃查詢是:

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 

任何想法?

回答

0

嘗試使用此代碼

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 

蒙山此代碼獲取更新,刪除行(OLD_VALUE)

見到你。