Sql 编写复杂触发器
这篇文章是我昨天早些时候发布的更新(带有触发代码) 我正在使用SQLServer2000。我正在编写一个触发器,当字段申请者.AppStatusRowID 表申请人链接到表位置、表公司和表AppStatus 我的问题是在我的查询中创建联接 更新applicator.AppStatusRowID时,我希望从applicator.AppStatusRowID、applicator.FirstName、applicator.Lastname、Location.LocNumber、Location.LocationName、Company.CompanyCode、AppStatus.DisplayText中获取值 连接将是:Sql 编写复杂触发器,sql,sql-server,tsql,sql-server-2000,triggers,Sql,Sql Server,Tsql,Sql Server 2000,Triggers,这篇文章是我昨天早些时候发布的更新(带有触发代码) 我正在使用SQLServer2000。我正在编写一个触发器,当字段申请者.AppStatusRowID 表申请人链接到表位置、表公司和表AppStatus 我的问题是在我的查询中创建联接 更新applicator.AppStatusRowID时,我希望从applicator.AppStatusRowID、applicator.FirstName、applicator.Lastname、Location.LocNumber、Location.Lo
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
这将被插入到审核表中(字段为applicationID、LastName、FirstName、日期、时间、公司、地点编号、地点名称、状态处置、用户)
触发器查询是:
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
有什么想法吗?试试这个代码
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
使用此代码可以获得更新删除行(旧的_值)
再见