无法在Delete触发器中传递当前用户

时间:2014-12-14 08:45:33

标签: sql sql-server-2008 sql-delete triggers

我正在为删除查询创建一个触发器,因此如果有人从Tasklists中删除该行,则该表会将两行复制到Tasklistslog表。 一行将是现有数据,由原始表修改,表示行已更新。另一行将要求按列名修改为当前用户,该用户从MVC模型返回(ieHttpContext.Current.User.Identity.Name),而不是从主表中删除行。

我无法传递(ieHttpContext.Current.User.Identity.Name)来触发 下面是触发我想要的值....我还有另一个表,我从中获取用户名转换为全名&分支相同。

Create TRIGGER [dbo].[tasklistdelete]
ON [dbo].[Tasklists] 
After delete
AS
declare
@Task_Id    int,
@TaskName   nvarchar,
@Discription    nvarchar,
@Frequency  nvarchar,
@Branch int,
@Duedate    date,
@status nvarchar,
@Performer  int,
@Active nvarchar,
@Recurrence nvarchar,
@comments   nvarchar,
@modifiedby nvarchar;

select @Task_Id=i.Task_Id from deleted i; 
select @TaskName=i.TaskName from deleted i; 
select @Discription=i.Discription from deleted i; 
select @Frequency=i.Frequency from deleted i; 
select @Branch=i.Branch from deleted i; 
select @Duedate=i.Duedate from deleted i; 
select @status=i.status from deleted i; 
select @Performer=i.Performer from deleted i; 
select @Active=i.Active from deleted i; 
select @Recurrence=i.Recurrence from deleted i; 
select @comments=i.comments from deleted i;
select @modifiedby=i.modifiedby from deleted i;

BEGIN
INSERT INTO Tasklistslog(Task_Idlog,TaskNamelog,Discriptionlog,Frequencylog,Branchlog,Duedatelog,statuslog,Performerlog,Activelog,Recurrencelog,commentslog,lastupdatedby,auditaction,logtime)
select Task_Id,TaskName,Discription,Frequency,Legislation,
(select Branch from Branch where Branch_id = @Branch),
@Duedate,status,
(select Displayname from Users where User_id = @Performer),
Active,
Recurrence,
comments,
@modifiedby,'Task Deleted',GETDATE()
from deleted ; 
END;

0 个答案:

没有答案