• sp获取用户角色,打印出批量给用户分配角色的语句


    ALTER PROCEDURE [dbo].[usp_ShowUserRole]
    AS
    BEGIN
    DECLARE @px INT = 0
    DECLARE @dbname VARCHAR(max)
    DECLARE @sql VARCHAR(MAX)
    IF OBJECT_ID('tempdb..#dbs', 'U') IS NOT NULL
    DROP TABLE #dbs
    IF OBJECT_ID('tempdb..#userRole', 'U') IS NOT NULL
    DROP TABLE #userRole
    IF OBJECT_ID('tempdb..#execsql', 'U') IS NOT NULL
    DROP TABLE #execsql
    CREATE TABLE #userRole
    (
    id INT IDENTITY(1, 1) ,
    dbname VARCHAR(60) ,
    [user] VARCHAR(60) ,
    userRole VARCHAR(60)
    )
    CREATE TABLE #execsql
    (
    id INT IDENTITY(1, 1) ,
    esql VARCHAR(200)
    )
    SELECT id = IDENTITY( INT ,1,1 ),
    name
    INTO #dbs
    FROM master.sys.databases db
    WHERE db.database_id > 4
    AND db.name NOT IN ( 'dbcenter','ReportServer','ReportServerTempDB','TargetDB','InitiatorDB' )
    WHILE ( @px < ( SELECT MAX(id)
    FROM #dbs
    ) )
    BEGIN
    SELECT TOP 1
    @dbname = #dbs.name
    FROM #dbs
    SET @sql = 'use ' + @dbname + ' SELECT ' + '''' + @dbname
    + ''''
    + ' as ''dbname'', a.name as ''user'' ,c.name as ''userRole'' from '
    + @dbname + '.sys.sysusers a inner join ' + @dbname
    + '.sys.sysmembers b ON a.uid = b.memberuid inner join '
    + @dbname
    + '.sys.database_principals c ON b.groupuid = c.principal_id where a.name not in (''dbo'',''sys'')'
    INSERT INTO #userRole
    EXEC ( @sql
    )

    SET @px = @px + 1
    DELETE #dbs WHERE name = @dbname
    END
    --SELECT * FROM #userRole
    INSERT INTO #execsql
    SELECT 'use ' + dbname + ' exec sp_addrolemember '
    + userRole + ',' + [user]
    FROM #userRole
    SELECT *
    FROM #execsql

    END

  • 相关阅读:
    二元查找树的后序遍历结果
    CFileDialog设置多选时的一个问题
    KanRSS.com
    由shuttle这个单词想起的一个小故事
    Sun Java moved to the Partner repository
    Sun Java moved to the Partner repository
    扩展std::string功能的几个做法
    NetBeans 时事通讯(刊号 # 99 Apr 16, 2010)
    NetBeans IDE 6.9 Beta 发布
    KanRSS.com
  • 原文地址:https://www.cnblogs.com/sandra/p/4835071.html
Copyright © 2020-2023  润新知