If i have to grant access to Database using T-SQL
and also verify if user already exist in SQL
,
If user don't exist then create account first and then grant access to database.
If user exist, just grant access to database.
I only create users in SQL
. Not in Windows.
What will be by T-SQL
Query to achieve above. ?
Try to create your login first, and the your user. This code first checks where all your users are assigned to which databases. Afterwards it checks if there is a login created and then it checks if the user exists. It is also set dynamically to you can just enter a DBName.
Example