19

我正在运行一些 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)

然后它说以下

如果每一行的密码都是唯一的,则取 set @Password = 'Password'; 关闭并将@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 

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

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

4

2 回答 2

37

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

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

于 2012-11-15T22:05:55.753 回答
2

如果 [Password] 是 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)
于 2012-11-15T22:36:50.670 回答