开发者

how to check if the log-in exists in sql server?

I am creating a new login in my SQL Server and I want to check if the login exists, if not, then I have to execute the following query to create a new login.

Can anyone help me in checking whether the login exists or not using qu开发者_开发知识库ery?

USE [master]
GO
CREATE LOGIN [Emsmgr1] WITH PASSWORD=N'welcome', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO


Try querying select name from master..syslogins


It is over 2 years since this question was asked but for anyone that comes across. An answer to this question can be found on http://www.thereforesystems.com/check-if-a-login-exists-in-sql-server-2008/.

It basically consist in using the appropiate systen tables to verify the existence of a login.

IF NOT EXISTS(SELECT name FROM sys.server_principals WHERE name = 'test_user')
BEGIN
    ...
END

or

IF NOT EXISTS(SELECT name FROM sys.sql_logins WHERE name = 'test_user')
BEGIN
    ...
END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜