CREATE TRIGGER [dbo].[trgAfterDelete12] ON dbo.[project_employee]
AFTER DELETE
AS
declare @empname varchar(25);
declare @emp_password varchar(25);
declare @emp_username varchar(25);
declare @audit_action varchar(50);
select @empname=d.Emp_Name from deleted d;
select @emp_password=d.Emp_Password from deleted d;
select @emp_username=d.Emp_username from deleted d;
set @audit_action=' This employee record has been deleted ';
insert into project_employee_audit(Emp_name,Emp_username,Emp_password,action,update_date)
values(@empname,@emp_username,@emp_password,@audit_action,getdate());
PRINT 'AFTER DELETE Trigger fired.'
GO
这是我已经创建的触发器脚本,现在当我再次尝试执行它时,它给出了错误“必须声明一个标量变量,你能告诉我脚本中的问题是什么。当它第一次出现时是好的为什么它给我这个错误信息?
答案 0 :(得分:2)
应该是:
CREATE TRIGGER [dbo].[trgAfterDelete12] ON dbo.[project_employee]
AFTER DELETE
AS
insert into project_employee_audit(Emp_name,Emp_username,Emp_password,action,update_date)
select Emp_Name,Emp_username,Emp_Password,'This employee record has been deleted ',getdate()
from deleted
由于deleted
是一个伪表,可以包含多行(或没有行)。即使你对标量变量的单独赋值也会从同一(任意)行得到一致的值,也无法保证