Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 script to create user and login for database

Author  Topic 

shekhar_dba
Starting Member

39 Posts

Posted - 2007-01-09 : 12:33:01
Hi gurus i am new to sql server.... so plz help me out ... i need script to create new database user and login.i know how to create by enterprise manager but have no idea to create it by QA.
please help me out
thanks in advance
shekhar

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-01-09 : 12:46:32
do you know about BOL? you can find syntax for all T-SQL statements there.

http://msdn2.microsoft.com/en-us/library/ms173463.aspx
http://msdn2.microsoft.com/en-us/library/ms189751.aspx

EDIT: also, if you are that new to sql server you don't know how to create users, it might be a bit early for you to have the acronym "dba" in your user name.


www.elsasoft.org
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-01-09 : 12:52:59
[code]EXEC sp_addlogin 'MyUserName', 'MyPassword', 'MyDefaultDB'

EXEC sp_grantdbaccess @loginame = 'MyUserName'
, @name_in_db = 'AliasNameForThisDatabaseIfDifferent' -- Optional
[/code]
If the user has Windows NT permissions (rather than a SQL Server login) you should do:
[code]
EXEC sp_grantdbaccess 'DomainName\Username', 'Username'
or
EXEC sp_grantdbaccess 'DomainName\Username', 'SomeOtherAliasName'
[/code]
then you can add to Roles:
[code]
EXEC sp_addrolemember @rolename = 'RoleToAddTo', @membername = 'MyUserName'

e.g.

EXEC sp_addrolemember @rolename = 'db_datareader', @membername = 'MyUserName'
EXEC sp_addrolemember @rolename = 'db_datawriter', @membername = 'MyUserName'
[/code]
alternatively you can allocate permissions directly (bad idea though, better to grant the permissions to a Role, and then add the User to that Role)
[code]
GRANT SELECT, INSERT, UPDATE, DELETE ON MyTable TO UserName
GRANT SELECT, INSERT, UPDATE, DELETE ON MyTable TO MyRoleName
[/code]
Kristen
Go to Top of Page

shekhar_dba
Starting Member

39 Posts

Posted - 2007-01-09 : 15:12:20
thanks alot to all of u :)
bye until next time
Go to Top of Page
   

- Advertisement -