默认登录用户到SQL Server列(Default logged in user to SQL Server column)

编程入门 行业动态 更新时间:2024-10-24 06:27:49
默认登录用户到SQL Server列(Default logged in user to SQL Server column)

如何创建一个列,其中包含当前在表中创建行的用户的默认值?

我尝试了system_user ,但是显示了从表中选择时登录的用户。

How can I create a column with a default value of the user that created the row at the time in the table?

I tried system_user, but that shows who is logged in when selecting from the table.

最满意答案

尝试这样的事情:

CREATE TABLE DemoTable ( ID INT IDENTITY(1,1), SomeValue VARCHAR(50), CreatedBy VARCHAR(50) CONSTRAINT DF_DemoTable_CreatedBy DEFAULT(SUSER_NAME()) )

您基本上在其中一个列上创建了默认约束 ,并使用SUSER_NAME()函数将其填充到当前登录的用户。

然后,当您将值插入该表时:

INSERT INTO dbo.DemoTable(SomeValue) VALUES('Some Value')

并且您没有为CreatedBy指定显式值,该列填充了SUSER_NAME()函数中的值,您可以通过从表中选择来检查该值:

SELECT * FROM dbo.DemoTable

在TechNet上阅读有关SUSER_NAME()和一些相关函数 (如SUSER_ID() )的更多信息。

Try something like this:

CREATE TABLE DemoTable ( ID INT IDENTITY(1,1), SomeValue VARCHAR(50), CreatedBy VARCHAR(50) CONSTRAINT DF_DemoTable_CreatedBy DEFAULT(SUSER_NAME()) )

You basically create a default constraint on one of your columns, and you use the SUSER_NAME() function to populate it with the currently logged in user.

When you then insert values into that table:

INSERT INTO dbo.DemoTable(SomeValue) VALUES('Some Value')

and you don't specify an explicit value for CreatedBy, that column is filled with the value from the SUSER_NAME() function, which you can check by selecting from the table:

SELECT * FROM dbo.DemoTable

Read more about SUSER_NAME() and a few related functions (like SUSER_ID()) on TechNet.

更多推荐

本文发布于:2023-08-04 22:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Server   SQL   Default   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!