Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically creating users

Status
Not open for further replies.

DeanWilliams

Programmer
Feb 26, 2003
236
0
0
GB
Hi all,

Is there a way to dynamically create SQL users?

I am working on an ASP.NET web application and am using a modified version of the stored procedure which adds users to the aspnet roles database.

You can pass in comma separated lists of domain usernames and roles as parameters and it adds all the users to all the aspnet roles specified.

I would like to extend it to allow it to create the SQL logins and users, and then add them to the correct SQL roles.

So I was wondering if I can use an equivalent to: CREATE USER domain\username FOR LOGIN domain\username WITH DEFAULT_SCHEMA=[dbo], but using string variables for the names, as well as statements to add them to SQL roles, using string variables for the role names.

Hope this makes sense,
Thanks in advance.
Dean.
 
I have found the answer:


EXEC ('CREATE LOGIN [domain\username] FROM WINDOWS WITH DEFAULT_DATABASE=[databasename];')

EXEC ('CREATE USER [domain\username] FOR LOGIN [domain\username] WITH DEFAULT_SCHEMA=[dbo];')

EXEC sp_addrolemember N'sqlrolename', N'domain\username'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top