简体   繁体   English

如何在SQL Server 2005中使用动态SQL更改登录名

[英]How to use dynamic SQL in SQL Server 2005 to alter login

I have a script that works fine in SQL Server 2008R2 but it couldn't work in SQL Server 2005. Is there any way that can work for both version? 我有一个脚本可以在SQL Server 2008R2中正常运行,但不能在SQL Server 2005中运行。有什么办法可以同时适用于两个版本?

declare @SQL nvarchar(max)  = ''

select @SQL = @SQL + 'ALTER LOGIN ' + QUOTENAME(DisabledName) + ' DISABLE;'
from [SysAdmin].[dbo].[DisabledAccountHistory]
where DisabledName in (select name 
                       from sys.server_principals 
                       where is_disabled = 0)
group by DisabledName

exec sp_executesql @SQL

The error I get is: 我得到的错误是:

Msg 139, Level 15, State 1, Line 0 Cannot assign a default value to a local variable. 消息139,级别15,状态1,行0无法将默认值分配给本地变量。 Msg 137, Level 15, State 2, Line 3 Must declare the scalar variable "@SQL". 消息137,级别15,状态2,第3行必须声明标量变量“ @SQL”。 Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword 'group'. 消息156,级别15,状态1,第6行关键字“ group”附近的语法错误。 Msg 137, Level 15, State 2, Line 8 Must declare the scalar variable "@SQL". 消息137,级别15,状态2,第8行必须声明标量变量“ @SQL”。

The declaration including an assignment of an initial value on a single line is a new feature in SQL Server 2008 - you cannot do this in 2005. 声明包括在一行中分配初始值是SQL Server 2008中的一项新功能 -您不能在2005年执行此操作。

Change your line to: 将行更改为:

declare @SQL nvarchar(max) 
set @SQL  = N''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM