游标声明中不允许使用变量赋值

时间:2014-09-10 13:33:26

标签: sql cursor

我正在尝试在SQL中使用游标并且在游标声明中不允许进行"变量赋值。" &安培; "关键字附近的语法错误' as' "错误。以下是我的询问:

 DECLARE @descriptor NVARCHAR(420)
 DECLARE cur_descriptor CURSOR 
 STATIC FOR
   SELECT @descriptor = descriptor FROM (SELECT * from IX_Mesh WHERE terms = 'tumors') as CHILD
   SELECT Terms FROM IX_Mesh WHERE descriptor = @descriptor  

   OPEN cur_descriptor
   FETCH NEXT
      FROM cur_descriptor INTO @descriptor
       WHILE @@FETCH_STATUS = 0

      BEGIN 
       PRINT @descriptor
       FETCH NEXT 
         FROM cur_descriptor INTO @descriptor
     END
 CLOSE cur_descriptor
 DEALLOCATE cur_descriptor 

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

错误消息有什么用?这是游标声明:

DECLARE cur_descriptor CURSOR STATIC FOR
   SELECT @descriptor = descriptor
----------^
   FROM (SELECT * from IX_Mesh WHERE terms = 'tumors') as CHILD

这项任务是禁忌。只是做:

DECLARE cur_descriptor CURSOR STATIC FOR
   SELECT descriptor
   FROM (SELECT * from IX_Mesh WHERE terms = 'tumors') as CHILD;

或者,更好的是:

DECLARE cur_descriptor CURSOR STATIC FOR
   SELECT descriptor from IX_Mesh WHERE terms = 'tumors';