cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
rguggisberg
Level 13

Finding SQL Users

This is more of a SQL question. I have a CA that adds a SQL user. How can I tell (vail a SQL script) if a particular user already exists?
Thanks,
Ron
Labels (1)
0 Kudos
(1) Reply
rguggisberg
Level 13

Looking back through my old posts I thought I'd post an answer in case anyone else has a similar problem. I figured out if a user already exists by using a bat file and SQLCMD to attempt to alter a login. Something like this:

REM This bat file will attempt to alter the login.
REM The alter is bogus in that it tries to enable a login that would already be enabled.
REM If it succeeds there is no message output from SQL and we will issue a warning that the username already exists.
REM If there is a message output from SQL we will assume it is an error indicating the username does not exist, which is OK.

SET TempFile=%TEMP%\%~n0.txt
IF EXIST "%TempFile%" DEL /q "%TempFile%"
sqlcmd -E -S "YourServername" -Q "ALTER LOGIN YourName ENABLE" > "%TempFile%"
FOR /F %%A in ('TYPE "%TempFile%"') DO (
IF NOT %%A=="" GOTO :Done
)
COLOR E0
ECHO.YourUserName already exists on YourServer.
ECHO.
PAUSE
:Done
IF EXIST "%TempFile%" DEL /q "%TempFile%"
0 Kudos