Create login and user for the Microsoft SQL Server database

Create login and user for MSSQL Server database

  1. Check the login present in the Microsoft SQL Server
USE MASTER
GO
select sp.name as login,sp.type_desc as login_type,sl.password_hash,
       sp.create_date,sp.modify_date,
       case when sp.is_disabled = 1 then 'Disabled'
            else 'Enabled' end as status
from sys.server_principals sp
left join sys.sql_logins sl
          on sp.principal_id = sl.principal_id
where sp.type not in ('G', 'R')
order by sp.name;
GO

2. Create a login for SQL Server:

-- create the user on the master database
USE [master] 
GO
CREATE LOGIN [LoginName] WITH PASSWORD=N'MyPassword'
Go

3. Check the user present for the database you want to create permission or access.

USE [DatabaseName]
GO
select name as username,
       create_date,
       modify_date,
       type_desc as type,
       authentication_type_desc as authentication_type
from sys.database_principals
where type not in ('A', 'G', 'R', 'X')
      and sid is not null
      and name != 'guest'
order by username;
GO

4. Create the user for a particular database and assign privilege DB owner:

USE [DatabaseName]
GO
CREATE USER [UserName] FOR LOGIN [LoginName]
Go

-- Assign DBOWNER Priviliges to username
USE [MyDatabaseName]
GO
ALTER ROLE [db_owner] ADD MEMBER [UserName]
GO

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.