Saturday, February 25, 2012

Defaulting the User's ID in a column

Hello,

I'm new to SQL Server and I'm wondering how I can default the User's Id in to a column when they create/modify a record.

Better yet, is there a way to get a list of allowable functions for the Default Parm?

thx so much.

Use the system_user.

ALTER TABLE MyTable
ADD ChangeUser varchar(100) DEFAULT system_user

Most of the system functions are available as DEFAULTS. Check Books Online.

|||

if you want to know the database username then system_user will not be the one. sometimes, you can have many database user names mapped to single login. try this and understand.

drop table TestuserDefault

create table TestuserDefault (userid int)

ALTER TABLE TestuserDefault

ADD systemuser varchar(100) DEFAULT system_user

ALTER TABLE TestuserDefault

ADD dbuser varchar(100) DEFAULT current_user

ALTER TABLE TestuserDefault

ADD dbuser1 varchar(100) DEFAULT user

ALTER TABLE TestuserDefault

ADD dbuser2 varchar(100) DEFAULT user_name()

insert into TestuserDefault(userid) select 1

select *from TestuserDefault

Madhu

|||

And of course, if you have multiple users mapped to the same login, there is no way that SQL Server can provide you the User identification information -you will have to do that in the application code, that is, if your application even tracks UserID information.

However, in those situations where IntegratedSecurity is being used, SYSTEM_USER will provide the correct UserName.

No comments:

Post a Comment