zoukankan      html  css  js  c++  java
  • sql server 删除 user,存储过程(IS2120@BG57IV3)

    //z 2013-02-27 14:57:08 IS2120@BG57IV3.T1466497816.K[T218,L2950,R90,V3189]
    --通过login name 删除 user 
    
    create procedure [dbo].[sp_DropLoginFromAllDatabases](@LoginName sysname)
    as
    begin
      set nocount on
    
      declare @DBName   sysname
      declare @SQL      nvarchar(4000)
      declare @ErrorMsg nvarchar(4000)
      declare @Enter    nvarchar(2)
    
      set @Enter = char(13) + char(10)
     
      set @LoginName = lower(rtrim(isnull(@LoginName, '')))
    
      if @LoginName = ''
      begin
        set @ErrorMsg = '@LoginName cannot be null or empty'
        goto Error
      end
    
      if @LoginName = 'dbo'
      begin
        set @ErrorMsg = 'User [dbo] cannot be deleted'
        goto Error
      end
    
      if @LoginName = 'guest'
      begin
        set @ErrorMsg = 'User [guest] cannot be deleted'
        goto Error
      end
    
      declare DatabasesCursor cursor fast_forward
      for select name
          from master.dbo.sysdatabases
          where (status & 32) = 0 and		--Loading
                (status & 512) = 0 and		--offline
                (status & 1024) = 0 and		--read only
                (status & 32768) = 0 and		--emergency mode
                (name <> 'master')
          order by name
    
      open DatabasesCursor
    
      fetch next from DatabasesCursor
      into @DBName
    
      while (@@fetch_status = 0)
      begin
        set @SQL = 'if exists(select 1' + @Enter + 
                   '          from ' + @DBName + '.dbo.sysusers' + @Enter + 
                   '          where islogin = 1 and' + @Enter + 
                   '                lower(name) = N''' + @LoginName + ''')' + @Enter + 
                   'begin' + @Enter + 
                   '  print(''Deleting user ['+@LoginName+'] from [' + @DBName + '] database...'')' + @Enter + 
                   '  exec ' + @DBName + '.dbo.sp_dropuser N''' + @LoginName + '''' + @Enter + 
                   'end'
        exec(@SQL)
    
     
        fetch next from DatabasesCursor
        into @DBName
      end
    
      close DatabasesCursor
      deallocate DatabasesCursor
    
      if exists(select *
                from master.dbo.syslogins
                where name = @LoginName)
      begin
        print('Deleting system login ['+@LoginName+']')
        set @SQL = 'drop login ['+@LoginName+']'
        exec(@SQL)
      end
    
    OK:
      return 0
    Error:
      set @ErrorMsg = 'Error in stored procedure sp_DropLoginFromAllDatabases:' + @Enter +
                      isnull(@ErrorMsg, '')
      raiserror(@ErrorMsg, 16, 10)
      return -1
    end
  • 相关阅读:
    PAT:循环-12. 打印九九口诀表(15) AC
    PAT:循环-07. 爬动的蠕虫(15) 错两个
    PAT:循环-01. 求整数段和(15) AC
    PAT:分支-16. 计算分段函数(10) AC
    PAT:分支-10. 计算个人所得税(10) AC
    PAT:分支-08. 高速公路超速处罚(15) AC
    UIToolBar
    iOS 代码实现获得应用的版本号(Version/Build)
    IOS开发之 ---- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
    IOS开发中的CGFloat、CGPoint、CGSize和CGRect
  • 原文地址:https://www.cnblogs.com/IS2120/p/6745765.html
Copyright © 2011-2022 走看看