Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Currently I have connected database using windows authentication (default allow my windows creds). Ex: Server/User1
I need to create database using other windows credentials. Ex: Server/User2
The new database DBO rights should be Server/User2

Can help to achieve this scenario.

Let me know the SQL Query on this scenario.

Scenario explanation below:-
Step1:
Customer login system using them own user name and password (Ex:User Name: System\User1).
Step2:
Running one of the C# windows application exe. Application showing the DB Servername, DB Name, Login, Password.
Step3:
Enter Database server name Ex: (local)\SQLExpress
Enter Database name Ex: MyDatabase
Enter Database user name Ex: System\DBAdmin
Enter Database password Ex: **********
Note: System\DBAdmin already added localhost SQL Login (Security -> Login) with Sysadmin permission.
Step4:
Then successfully it should to create database for the login (System\DBAdmin)

Below should be expected after created database:-
1. Go to the SQL Sever Management Studio
2. Connect (local)\SQLExpress & Windows Authentication
3. Expand Databases -> MyDatabase should be created
4. Click MyDatabase and check below things:-
MyDatabase -> Security -> Users -> dbo -> General -> User type -> Windows User
MyDatabase -> Security -> Users -> dbo -> General -> Login name -> System\DBAdmin


Regards,

Vasanth

What I have tried:

I have added user under Login, tried to create database but no luck.
Posted
Updated 30-May-20 3:23am
v2
Comments
ZurdoDev 21-May-20 12:55pm    
This is not a code issue so you may get better results in a technical support forum, but all you need to do is create a new login and password and assign to the db. You can do this in Sql Server Management Studio under the Security folder.
vasanthkumarmk 21-May-20 12:59pm    
I need to do from query. If I logout my user (User1) and login to another user (User2) I can able to create database and DB right also corresponding user.
But my case connected windows authentication in User1, but need to create database for User2.
phil.o 21-May-20 14:13pm    
Credentials are managed at the connection level, not at the query level. You cannot issue a query to change the login of a connection.

 
Share this answer
 
Comments
vasanthkumarmk 25-May-20 1:45am    
Thanks... I am looking to create DB using query. We can do create database use run as different user in SSMS.
It sounds like you are trying to do this CREATE DATABASE SCOPED CREDENTIAL (Transact-SQL) - SQL Server | Microsoft Docs[^] or CREATE CREDENTIAL (Transact-SQL) - SQL Server | Microsoft Docs[^]

The only way to create a database "using" the other windows credentials is to be logged on using those credentials. I suspect it's just a terminology problem and you are trying to change the owner of a database - have a look at How to Change Owner of Database in SQL SERVER? [^]
 
Share this answer
 
Comments
vasanthkumarmk 25-May-20 1:44am    
Thanks but no luck.
CHill60 26-May-20 6:25am    
Instead of just saying "no luck" can you tell us specifically what you did and what happened?
vasanthkumarmk 27-May-20 2:27am    
USE master
GO
\\SERVER\Login1 is my windows default login account
GRANT IMPERSONATE ON LOGIN:: [SERVER\Login1] TO [SERVER\Login2]
GO
CREATE DATABASE TESTING9
GO

I would like to expect create database TESTING9 for using login account [SERVER\Login2]. So TESTING9 -> Security -> Users -> dbo -> Login name -> should be '[SERVER\Login2]'
CHill60 27-May-20 6:09am    
I'm not sure why you would expect create database to do all of that without the additional steps to change the owner... try
ALTER AUTHORIZATION ON DATABASE::TESTING9 TO Login2;
vasanthkumarmk 27-May-20 10:14am    
Sorry for confusion. Below I explain what customer expecting:-
Subject: Create (Windows Authentication) database using others Active directory login.
Explanation: Imagine employees using multiple systems in a office, those have single DB Administrator, DB Admin have only rights to install database for all employees in the office. So DB Admin has already added particular permissions for all employees machines (Security -> Logins -> Server/DBadmin) (also added Server Roles permission as sysadmin).
Then DB Admin has given one particular windows active directory user id (Server/DBadmin) and password (******) for all employees to install database. Employees using one c# windows application providing the DB Admin user name, password, Servername, database name then it will create (Windows Authentication) database. So once created database, then database should be TESTING9 -> Security -> Users -> dbo -> Login name -> should be '[Server/DBadmin]'

---- This is my expectation------

I am trying impersonate user before create database using SQL Query. But unable to achieve.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900