nvarchar不是可识别的游标选项

时间:2012-11-15 21:56:42

标签: sql tsql

我正在运行一些sql,它从临时表中获取信息并将其放入永久表中。我是从3年前写的一步一步的指南中得到的,写这篇文章的人早就不见了。 它声明在这里使用这个sql。

declare @Password nvarchar(100); 
set @Password ='rewards';
if not exists(select 1 from sys.openkeys where key_name = 'Sym_UserPassData' 
and database_name = db_name())  OPEN SYMMETRIC KEY Sym_UserPassData DECRYPTION BY
CERTIFICATE UserPassTables with password='asbpass71509new';
INSERT INTO [User] (Username, [Password], AllowChange, ForceChange, FullName,
SalesRep, OpenLink, UserProfileID, LastUpdatedBy,UserEmail) 
(SELECT Username,EncryptByKey(Key_GUID('Sym_UserPassData'),@Password), 
AllowChange, ForceChange, FullName, SalesRep, [OpenLink ], UserProfileID,    
LastUpdateBy, UserEmail FROM TempUsers)

之后又说了以下

如果每行的密码都是唯一的,请设置@Password ='密码';关闭并用[密码]替换@Password。

所以起初我只是改变了第二行,所以它说了

declare @Password nvarchar(100);
set [Password]
...

但是这给了我一个密码列的错误,所以我把它改成了:

declare [Password] nvarchar(100); 
set [Password]
if not exists(select 1 from sys.openkeys where key_name = 'Sym_UserPassData' 
and database_name = db_name())  OPEN SYMMETRIC KEY Sym_UserPassData DECRYPTION BY
CERTIFICATE UserPassTables with password='asbpass71509new';
INSERT INTO [User] (Username, [Password], AllowChange, ForceChange, FullName,
SalesRep, OpenLink, UserProfileID, LastUpdatedBy,UserEmail) 
(SELECT Username,EncryptByKey(Key_GUID('Sym_UserPassData'),[Password]), 
AllowChange, ForceChange, FullName, SalesRep, [OpenLink ], UserProfileID,    
LastUpdateBy, UserEmail FROM TempUsers)

这就是给我错误的原因:

 nvarchar is not a recognized cursor option 

有谁知道我错过了什么?如果我能提供任何其他信息,我会尽我所能。

感谢能够提供帮助的任何人。

2 个答案:

答案 0 :(得分:25)

您必须在名称前使用@符号声明变量。所以这是正确的:

declare @Password nvarchar(100); 
set @Password ='rewards';

这是不正确的:

declare [Password] nvarchar(100); 
set [Password] ='rewards';

我认为问题在于您的变量声明。请参阅此文章:http://www.databasejournal.com/features/mssql/article.php/3087431/T-SQL-Programming-Part-1---Defining-Variables-and-IFELSE-logic.htm

答案 1 :(得分:1)

如果[密码]是TempUsers中的一列,则可能意味着这一点。

if not exists(select 1 from sys.openkeys where key_name = 'Sym_UserPassData' 
and database_name = db_name())  OPEN SYMMETRIC KEY Sym_UserPassData DECRYPTION BY
CERTIFICATE UserPassTables with password='asbpass71509new';
INSERT INTO [User] (Username, [Password], AllowChange, ForceChange, FullName,
SalesRep, OpenLink, UserProfileID, LastUpdatedBy,UserEmail) 
(SELECT Username,EncryptByKey(Key_GUID('Sym_UserPassData'),[Password]), 
AllowChange, ForceChange, FullName, SalesRep, [OpenLink ], UserProfileID,    
LastUpdateBy, UserEmail FROM TempUsers)