微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决增强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 举报,一经查实,本站将立刻删除。