1
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

这是我已经创建的触发脚本,现在当我尝试再次执行它时,它给了我错误“必须声明一个标量变量,你能告诉我脚本中有什么问题。现在第一次没问题时为什么会这样给我这个错误信息?

4

1 回答 1

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是一个伪表,可以包含多行(或不包含行)。即使您对标量变量的单独分配也不能保证从同一(任意)行中始终如一地获取值

于 2013-10-23T06:12:28.623 回答