此帖子是我昨天早些时候发布的更新(包含触发器代码)
我正在使用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,LastName,FirstName,Date,Time,Company,Location Number,Location Name,StatusDisposition,User)
触发器查询是:
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 :(得分: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)
见到你。