SQL SERVER中将某字段默认值设置为当前登录名
2013-08-20 14:04
573 查看
今天,让添加两个字段创建者,创建日期,创建者默认值设置为当前登录人,创建日期为当前时间。
在网上查了一下,当前登录人用SUSER_SNAME.
先备份一下表,再添加字段
--备份表
USE DBA;
SELECT * INTO DBA.DBO.SubjectDB_PRODEFINE_ProductInfoExtend_BAK_20130820
FROM SubjectDB.PRODEFINE.ProductInfoExtend
--添加字段
ALTER TABLE SubjectDB.PRODEFINE.ProductInfoExtend ADD CreateTime DATETIME DEFAULT GETDATE();
ALTER TABLE SubjectDB.PRODEFINE.ProductInfoExtend ADD Creator NVARCHAR(100) DEFAULT SUSER_SNAME();
以下是参考的SUSER_SNAME相关资料:
Returns the login identification name from a user's security identification number (SID).
Syntax
SUSER_SNAME(
[ server_user_sid ] )
Arguments
server_user_sid
Is the user security identification number.server_user_sid, which is optional, is
varbinary(85). server_user_sid can be the security identification number of any Microsoft® SQL Server™ login or Microsoft Windows NT® user or group. Ifserver_user_sid is not specified, information about the current user is
returned.
Return Types
nvarchar(256)
Remarks
When specifying a SQL Server login using SQL Server Authentication, the user must be granted permission to connect to SQL Server. Usesp_addlogin or SQL Server Enterprise Manager to
grant this permission. However, when specifying a Windows NT user or group using Windows Authentication, this user or group does not have to be granted permission to connect to SQL Server.
SUSER_SNAME can be used as a DEFAULT constraint in either ALTER TABLE or CREATE TABLE.
System functions can be used in the select list, in the WHERE clause, and anywhere an expression is allowed, and must always be followed by parentheses (even if no parameter is specified).
Examples
A. Use SUSER_SNAME
This example returns the login name for the security identification number with a value of 0x01.
B. Use SUSER_SNAME with a Windows NT user's security identification number
This example returns the login name for the Windows NT user's security identification number, obtained by using SUSER_SID.
C. Use SUSER_SNAME as a DEFAULT constraint
This example uses SUSER_SNAME as a DEFAULT constraint in a CREATE TABLE statement.
在网上查了一下,当前登录人用SUSER_SNAME.
先备份一下表,再添加字段
--备份表
USE DBA;
SELECT * INTO DBA.DBO.SubjectDB_PRODEFINE_ProductInfoExtend_BAK_20130820
FROM SubjectDB.PRODEFINE.ProductInfoExtend
--添加字段
ALTER TABLE SubjectDB.PRODEFINE.ProductInfoExtend ADD CreateTime DATETIME DEFAULT GETDATE();
ALTER TABLE SubjectDB.PRODEFINE.ProductInfoExtend ADD Creator NVARCHAR(100) DEFAULT SUSER_SNAME();
以下是参考的SUSER_SNAME相关资料:
Returns the login identification name from a user's security identification number (SID).
Syntax
SUSER_SNAME(
[ server_user_sid ] )
Arguments
server_user_sid
Is the user security identification number.server_user_sid, which is optional, is
varbinary(85). server_user_sid can be the security identification number of any Microsoft® SQL Server™ login or Microsoft Windows NT® user or group. Ifserver_user_sid is not specified, information about the current user is
returned.
Return Types
nvarchar(256)
Remarks
When specifying a SQL Server login using SQL Server Authentication, the user must be granted permission to connect to SQL Server. Usesp_addlogin or SQL Server Enterprise Manager to
grant this permission. However, when specifying a Windows NT user or group using Windows Authentication, this user or group does not have to be granted permission to connect to SQL Server.
SUSER_SNAME can be used as a DEFAULT constraint in either ALTER TABLE or CREATE TABLE.
System functions can be used in the select list, in the WHERE clause, and anywhere an expression is allowed, and must always be followed by parentheses (even if no parameter is specified).
Examples
A. Use SUSER_SNAME
This example returns the login name for the security identification number with a value of 0x01.
SELECT SUSER_SNAME(0x01)
B. Use SUSER_SNAME with a Windows NT user's security identification number
This example returns the login name for the Windows NT user's security identification number, obtained by using SUSER_SID.
SELECT SUSER_SNAME(0x010500000000000515000000a065cf7e784b9b5fe77c87705a2e0000)
C. Use SUSER_SNAME as a DEFAULT constraint
This example uses SUSER_SNAME as a DEFAULT constraint in a CREATE TABLE statement.
USE pubs GO CREATE TABLE sname_example ( login_sname sysname DEFAULT SUSER_SNAME(), employee_id uniqueidentifier DEFAULT NEWID(), login_date datetime DEFAULT GETDATE() ) GO INSERT sname_example DEFAULT VALUES GO
相关文章推荐
- SQL Server将字段默认值设置为当前时间
- SharePoint: 设置People or Group字段默认值为当前用户
- MySQL命令及使用技巧以及当前时间字段默认值设置
- MySQL设置字段的默认值为当前系统时间
- Mysql中如何设置日期字段默认值为当前日期
- SQL Server 字段设置默认值
- Mysql中如何设置日期字段默认值为当前日期
- sql server中datetime字段只取年月日如2006-04-21,默认值如何设置?getdate()得到的是包含时分秒的时间。
- 关于Mysql的日期类型字段设置默认值为当前日期的解决方案
- SQL 2008日期字段默认值设置为当前日期
- SQL Server 字段设置默认值
- SQL Server 设置字段初始默认值
- SQL Server中,给字段设置约束,设置默认值注意事项
- sql server中datetime字段只取年月日如2006-04-21,默认值如何设置?getdate()得到的是包含时分秒的时间。
- 设置允许SQL Server身份登录
- SQL Server 2008等登录用户只能看到自己的数据库设置办法
- MFC中将CEdit设置光标当前位置在编辑框最后
- 在Sql Server数据表中使用Guid类型的列及设置Guid类型的默认值
- SQL Server 2005里设置自增字段
- SQL Server 2008 如何将一个字段设置为自动增长型