首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

无法执行触发器更新操作解决方案

2012-02-02 
无法执行触发器更新操作USE [DB_AutoServiceCenter]GO/****** Object:Trigger [dbo].[User_Update]Script

无法执行触发器更新操作
USE [DB_AutoServiceCenter]
GO
/****** Object: Trigger [dbo].[User_Update] Script Date: 10/25/2011 13:37:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[User_Update]
  ON [dbo].[HR_User]
  AFTER UPDATE
AS 
BEGIN
SET NOCOUNT ON;
DECLARE @LoginName varchar(30)
SELECT @LoginName=LoginName FROM deleted

if Update(UserFlag)
DELETE CallCenter.dbo.Employee WHERE EmployeeNo=@LoginName
   
if Update(LoginName) or Update(UserName) or Update(LoginPwd) or Update(ChannelNo)
Update Employee
Set EmployeeNo=deleted.LoginName,EmployeeName=deleted.UserName,Password=deleted.LoginPwd,ChannelNo=deleted.ChannelNo FROM CallCenter.dbo.Employee,deleted
WHERE EmployeeNo='@LoginName'
END

上面是代码,第一个更新是没问题,但是不知道为什么第二个更新操作不管怎样都不执行?请大家帮忙看看

[解决办法]
跨数据库的触发器.....

SELECT @LoginName=LoginName FROM deleted
只能获得一条记录的loginname值,要改成:

SQL code
Update EmployeeSet EmployeeNo=deleted.LoginName,EmployeeName=deleted.UserName,Password=deleted.LoginPwd,ChannelNo=deleted.ChannelNo FROM CallCenter.dbo.Employee,deletedWHERE Employee.EmployeeNo=deleted.LoginName
[解决办法]
SQL code
--两表关联字段自己补上Update Employee Set EmployeeNo=deleted.LoginName,EmployeeName=deleted.UserName,Password=deleted.LoginPwd,ChannelNo=deleted.ChannelNo FROM CallCenter.dbo.Employee a join deleted bon (a.关联字段=b.关联字段)WHERE EmployeeNo='@LoginName'
[解决办法]
SQL code
--应该这样更新...Update a Set EmployeeNo=b.LoginName,EmployeeName=b.UserName,Password=b.LoginPwd,ChannelNo=b.ChannelNo FROM CallCenter.dbo.Employee a join deleted bon (a.关联字段=b.关联字段)WHERE a.EmployeeNo=@LoginName
[解决办法]
SQL code
--确定这是关联条件的话用下面试试,然后update触发下,看看有没走到1,然后有没走到2?print 1Update a Set EmployeeName=b.UserName,Password=b.LoginPwd,ChannelNo=b.ChannelNoFROM CallCenter.dbo.Employee a join deleted b on (a.EmployeeNo=b.LoginName)WHERE a.EmployeeNo=@LoginNameprint 2 

热点排行