How do you test for the existence of a user in SQL Server?

前端 未结 3 1619
有刺的猬
有刺的猬 2020-12-30 00:17

I\'d like to drop a user in a SQL Server script but I\'ll need to test for existence first or I\'ll get script errors. When dropping tables or stored procs, I check the sys

相关标签:
3条回答
  • 2020-12-30 01:12

    The code below worked for me.

    IF EXISTS (SELECT * FROM sys.syslogins WHERE name = N'MyUserName') 
    DROP LOGIN [MyUserName]
    
    0 讨论(0)
  • 2020-12-30 01:13

    In SQL 2005:

    select * from sys.sysusers
    

    In SQL 2000:

    select * from sysusers
    
    0 讨论(0)
  • 2020-12-30 01:20

    SSMS scripts it in the following way:

    For SQL 2005/2008:

    IF  EXISTS (SELECT * FROM sys.database_principals WHERE name = N'username')
    DROP USER [username]
    

    For SQL 2000:

    IF  EXISTS (SELECT * FROM dbo.sysusers WHERE name = N'username')
    EXEC dbo.sp_revokedbaccess N'username'
    
    0 讨论(0)
提交回复
热议问题