2009-10-21 50 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)

见到你。