Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

how to create a user by script with as administrator of my database?

Hello guys

How could I create a user for my database as administrator, the same privileges as 'sa'

thanks
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Pls try this-

NewUserName

Use [YourDBName]
GO

IF NOT EXISTS (SELECT TOP 1 1 FROM SYS.DATABASE_PRINCIPALS WHERE name = N'NewUserName')
BEGIN
    CREATE USER [NewUserName] FOR LOGIN [NewUserName]
    EXEC sp_addrolemember N'db_owner', N'[NewUserName]'
	
END
GO

Open in new window

You can also grant permissions if required.
Avatar of hidrau

ASKER

Hello Pawan

I did this:

USE [master]
GO
CREATE LOGIN [alexhaifa] WITH PASSWORD=N'aqz1973', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
EXEC master..sp_addsrvrolemember @loginame = N'alexhaifa', @rolename = N'sysadmin'
GO
USE [SAAS_05091973_HERMES_ALEXHAIFA]
GO
CREATE USER [alexhaifa] FOR LOGIN [alexhaifa]
GO


When I login in my database with my created login, I noticed that I have access with all databases, I only want to have access to this:

SAAS_05091973_HERMES_ALEXHAIFA

where should I change?  Default Database?
Change USE [master] to USE [SAAS_05091973_HERMES_ALEXHAIFA] and then try.
You can use this to drop the user -

EXEC sp_dropuser 'UserName'
Avatar of hidrau

ASKER

I did that, my default database = [SAAS_05091973_HERMES_ALEXHAIFA]

But when I login on the sql, I can open all database and not only [SAAS_05091973_HERMES_ALEXHAIFA] :(
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

nope :(
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

Thanks a lot