How to create new user for a database? | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

How to create new user for a database?

How do i create a user specifically for a database ?
i need to assign password too.
With SQL Manager Studio you can do it.
Security, Logins, rigth click new user.
Luis Martin
Moderator All in Love is Fair
Stevie Wonder
All postings are provided “AS IS” with no warranties for accuracy.
SQL Server has two tier security architecture (a) Server Level
(b) Database Level You create login for server level access. But if you have login you have only access to the server but do not have access to any database. You need to create A database user in the particular database and map that user to login to have database access. Read more about Login and User in BOL Madhu
If you are still looking to create with script: Use DatabaseName
GO if not exists (select * from master.dbo.syslogins where loginname = N’some_user’)
declare @logindb nvarchar(132), @loginlang nvarchar(132) select @logindb = N’DatabaseName’, @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’some_user’, ‘complexpassword’, @logindb, @loginlang
GO if not exists (select * from dbo.sysusers where name = N’some_user’)
EXEC sp_grantdbaccess N’some_user’, N’some_user’
GO exec sp_addrolemember N’db_datareader’, N’some_user’
GO exec sp_addrolemember N’db_datawriter’, N’some_user’
GO Thanks, Name
Dilli Grg (1 row(s) affected)