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
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Creating Users With A Script

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-01-19 : 09:52:14
James writes "Hi There,

I saw your article on creating users using transact-sql commands, but was wondering how to create an SQL user and give them access to that database?

Thanks
James"

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-01-19 : 11:39:47
sp_AddLogin will add SQL users, sp_GrantDbAcess will then add the user to a database.


Raymond
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-01-19 : 12:49:13
Here is an example script:



USE GT
GO

if not exists (select * from master.dbo.syslogins where loginname = N'GTXSP')
BEGIN
declare @logindb nvarchar(132), @loginlang nvarchar(132) select @logindb = N'master', @loginlang = N'us_english'
if @logindb is null or not exists (select * from master.dbo.sysdatabases where name = @logindb)
select @logindb = N'master'
if @loginlang is null or (not exists (select * from master.dbo.syslanguages where name = @loginlang) and @loginlang <> N'us_english')
select @loginlang = @@language
exec sp_addlogin N'GTXSP', 'somepassword', @logindb, @loginlang
END
GO

if not exists (select * from dbo.sysusers where name = N'GTXSP' and uid < 16382)
EXEC sp_grantdbaccess N'GTXSP', N'GTXSP'
GO


exec sp_addrolemember N'GT_Apps', N'GTXSP'
GO



Tara
Go to Top of Page
   

- Advertisement -