增强SQL Server 2012中WHILE循环的性能

问题描述

我为sql Server 2012创建了以下脚本,希望对它的性能增强提出任何建议。

DECLARE @dbname varchar(50)
DECLARE @dbuser varchar(50)
DECLARE @statement nvarchar(max)
DECLARE @db_cursor CURSOR

SET @dbname = 'AdventureWorks2012'

SET @db_cursor = CURSOR FOR
SELECT name
FROM sys.server_principals
WHERE TYPE = 'U' and name like '%DESKTOP-B2341N%'

OPEN @db_cursor

FETCH NEXT FROM @db_cursor INTO @dbuser

WHILE @@FETCH_STATUS = 0
BEGIN
    /* This statement will be executed to grant the privilege read only */
    SELECT @statement = 'USE ['+@dbname+']; ' + 'ALTER ROLE [db_owner] DROP MEMBER ['+@dbuser+'];' +
                        'USE ['+@dbname+']; ' + 'ALTER ROLE [db_datareader] ADD MEMBER ['+@dbuser+'];'

    EXEC (@statement)

    FETCH NEXT FROM @db_cursor INTO @dbuser
END 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)