Groups | Blog | Home
all groups > sql server programming > may 2004 >

sql server programming : Checking if a log in exists in the SQL Server



TomTom
5/22/2004 9:03:15 PM
I am trying to add a log in my SQL DB and ran the procedure below in Query
Analyzer.

exec sp_addlogin 'tomtom','mypassword'

The problem is I get the warning below when I ran this procedure for the
second time.

The login 'tomtom' already exists.

Can someone let me know how I can check if a login already exists in the
server? I looked in several tables in the master database, but couldn't find
the login names there.

Thanks!
Tomtom

TomTom
5/22/2004 10:51:55 PM
Thanks! I was able to find the table and view. Somehow they are invisible
and that's why I couldn't find it.

Tomtom

[quoted text, click to view]

TomTom
5/22/2004 11:41:39 PM
That looks concise! Thanks!

[quoted text, click to view]

TomTom
5/23/2004 7:17:56 AM
I don't have issues. Both solutions works good to me. Again, thank you for
your help.

[quoted text, click to view]

Uri Dimant
5/23/2004 8:55:40 AM
Tom
IF EXISTS(SELECT * FROM master..SYSLOGINS WHERE [name]=.....)
BEGIN
......
END
ELSE
......
[quoted text, click to view]

Hari Prasad
5/23/2004 10:49:38 AM
Hi,

All the logins will be stored as a record in the system table "SYSXLOGINS"
in master database. Instead of querying the system table
directly you can query the view "SYSLOGINS". Use the below query to find the
login existence:-

if (select count(*) from master..syslogins where name='tomtom')=1
select 'Login exists'
else
select 'No Login with name'

(You can also execute the below procedure to get all the detals of the
login) :-

sp_helplogins 'tomtom'



Thanks
Hari
MCDBA


[quoted text, click to view]

Hari Prasad
5/23/2004 2:38:55 PM
Hi Uri,

Both our statements are supposed to give identical results.. Mine works
perfectly to me. I dont know why it is giving issues to Tomtom.

Thanks
Hari
MCDBA



[quoted text, click to view]

AddThis Social Bookmark Button