SQL Server Trigger after Update Record

时间:2017-03-02 23:33:52

标签: c# sql-server triggers

Hey there so I'm trying to create a trigger in SQL Server to automatic insert getdate() and SYSTEM_USER in a column after a record has been updated instead of including in a Update Query.

DB: Company

Table:  dbo.Lincensee

Column: DateModified -> I want to Insert: getdate()
Column: ModifiedBy - > I want to insert: SYSTEM_USER

After a specific Record has been updated from a Query Running in C#.

Query for example:

string strQuery = @"UPDATE dbo.Licensee SET LNAME = @LastName, 
                                FNAME = @FirstName,
                                MIDNAME = @MiddleName,
                                FIRMNAME = @OrganizationName,
                                SSN = @SSN,
                                WHERE LICNUM = @licenseToUpdate";
SqlCommand updateCommand = new SqlCommand(strQuery, connUpdateLicense);
...

PS: I can Hard code it inside the Set query, but I rather have it in a Trigger.

Any Ideas? Thank you.

1 个答案:

答案 0 :(得分:0)

我按照Suresh Link使用After Update找到了T-SQL代码。并使用我的数据库修改:

create trigger [INS].[licpro_modby_dateby]
on [INS].[LICPRO]
after insert, update
as
begin
update a
set modifiedby = system_user,
    datemodified = getdate()
from ins.licpro as a
join inserted as b 
on a.licnum = b.licnum; 

谢谢你们,对不起我迟到的回复感到抱歉。