我有一个产品表,我想在使用触发器更新记录时将机器名或域用户名设置到[Updated By]
字段。我想将域用户名存储到[Updated By]
我试图设置[Updated By]=nt_username
但不工作我怎样才能获得网络信息
CREATE TRIGGER [dbo].[UPDATED_DATE_UPDATED_BY]
ON [dbo].[tableName]
AFTER UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE T1
SET [Updated Date] = GETDATE(),
[Updated By]=nt_username
FROM TIRES T1
INNER JOIN inserted i ON T1.[Auto PN] = i.[Auto PN]
END
答案 0 :(得分:1)
SQL Server有一个内置函数,用于返回与当前系统用户关联的用户名。
CREATE TRIGGER [dbo].[UPDATED_DATE_UPDATED_BY]
ON [dbo].[tableName]
AFTER UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE T1
SET [Updated Date] = GETDATE(),
[Updated By]=suser_sname()
FROM TIRES T1
INNER JOIN inserted i ON T1.[Auto PN] = i.[Auto PN]
END
此处有关此功能的更多信息:https://docs.microsoft.com/en-us/sql/t-sql/functions/suser-sname-transact-sql