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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create db user - transact sql

Status
Not open for further replies.

tek2002

Programmer
Aug 21, 2001
67
US
i would like to do the following and was wondering of anyone could direct me to code on the web that would help me accomplish the task:

we maintain several databases on a sql server 7.0
--lets call them db1, db2, db3
db1 is the primary db we use and we add users there and then associate them with lets say usergroup1.

its too much work for someone to go and add those users to db2 and db3 as well, so i need to write a script that will check db1 for new users then check db2 and db3 to make sure the users are not there then create those users on db2 and db3 - when i create them on db2 and db3, i will need to associate them the usergroup1 on each db as well.

i will create a job to execute the script nightly so this is done on a daily basis.

any direction you could give would be appreciated.

thanks

 
To quote BOL

sp_grantdbaccess
Adds a security account in the current database for a Microsoft® SQL Server™ login or Microsoft Windows NT® user or group, and enables it to be granted permissions to perform activities in the database.

Syntax
sp_grantdbaccess [@loginame =] 'login'
[,[@name_in_db =] 'name_in_db' [OUTPUT]]

Arguments
[@loginame =] 'login'

Is the name of the login for the new security account in the current database. Windows NT groups and users must be qualified with a Windows NT domain name in the form Domain\User, for example LONDON\Joeb. The login cannot already be aliased to an account in the database. login is sysname, with no default.

[@name_in_db =] 'name_in_db' [OUTPUT]

Is the name for the account in the database. name_in_db is an OUTPUT variable with a data type of sysname, and a default of NULL. If not specified, login is used. If specified as an OUTPUT variable with a value of NULL, @name_in_db is set to login. name_in_db must not already exist in the current database.

EXEC sp_grantdbaccess 'Corporate\GeorgeW', 'Georgie'


HTH

Rob
 
thanks so much - i just did some researching
and the following sp works : sp_adduser
(it executes sp_grantdbaccess)

thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top