Declare @userName varchar(50)
set @UserName='Ritesh123'
if exists(SELECT name FROM sysusers where name=@Username)
print 'exists'
else
print 'not exists'
Main Topics
Browse All TopicsFirst I creat a Login to SQL Server using
CREATE LOGIN NT AUTHORITY\NETWORK SERVICE FROM WINDOWS WITH DEFAULT_DATABASE="MyDB";
Here, NT AUTHORITY\NETWORK SERVICE is the name of the Login I created and MyDB is the DataBase I associate it with.
After creating it I need to change some settings of the created login. Following is the manual procedure I usually do.
1. Open SQL Server Management Studio
2. In the "Object Explorer", expand "Security" -> "Logins"
3. Right Click the newly created login "NT AUTHORITY\NETWORK SERVICE" -> Select "Properties"
4. In the "Select a page" section select "User Mapping"
5. In the right hand side, there is a section called "Users mapped to this login". There, under the "Map" section check the CheckBox relavent to MyDB, then under "Default Schema" click the button that appears.
6. In the appearing "Select Schema" windown, click "Browse", select MyDB from the new window that opens and click OK. Then press OK in the "Select Schema" window and return to the previous window. (Bear in mind that at the time of the creation of MyDB, I also create Schemas, so that I am able to select that from "Select Schema" window. In your PC, you may not be able to do so if you don't create a schema when you create a DB.)
7. There, in the part at the bottom which says "Database role membership for : MyDB", check "db_owner". ("public" is already checked, so leave it as it is)
8. Then click OK in the Login Properties and apply the above settings.
I need this process to be done using SQL Queries, so following is the query I use.
But not just that, I also want to check if the login and the users exist before they are created.
I manged to check for the login using the following:
IF EXISTS (select loginname from master.dbo.syslogins where name = N'NT AUTHORITY\NETWORK SERVICE' and dbname = 'MyDB')
That worked fine, but I don't know how I should look for the users if they exist or not.
Any ideas?
Help with code segments will be greatly appreciated.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
kerjemanov,
you gave the right solution but for checking login not use. I gave reference of SysUsers after reading
" but I don't know how I should look for the users if they exist or not" from Author's post.
if author wants to check for users than he should go for
Declare @userName varchar(50)
set @UserName='Ritesh123'
if exists(SELECT name FROM sysusers where name=@Username)
print 'exists'
else
print 'not exists'
and if he wants to check for logins than he should go for
Declare @LoginName varchar(50)
set @LoginName='Ritesh123'
if exists(SELECT name FROM sys.syslogins where name=@Username)
print 'exists'
else
print 'not exists'
Business Accounts
Answer for Membership
by: RiteshShahPosted on 2009-05-13 at 00:18:42ID: 24372076
you can check whether user exist or not from following query
user DataBaseName
SELECT name FROM sysusers