Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me!

->I want to create a new sqlserver login programmatically. My own database is not create
in this server.
->How can I write program to acheive this.
->I hope your answer.

Thanks all.

Wynne C
Posted

 
Share this answer
 
SQL
Declare @LoginName nvarchar(100)
Declare @Password nvarchar(100)
Declare @SQL nvarchar(max)
Begin
    Set @LoginName = 'TestAdmin'
    Set @Password = 'pass@word1'
    -- Create user
    Set @SQL = 'Create Login ' + @LoginName + ' with Password = ''' + @Password + ''''
    EXEC sp_executesql @SQL
    -- Adding user to sysadmin role
    EXEC sp_addsrvrolemember @LoginName, 'sysadmin'
    -- This will give you list of SQL Users
    select * from sys.sql_logins
End
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900