SQL游标语法问题

时间:2013-01-10 13:26:01

标签: sql sql-server-2008 cursor

我收到此错误 消息102,级别15,状态1,过程CostTest_02,行43 ';'附近的语法不正确。

用“GO”加下划线红色我一直在寻找错误,我找不到了!我对游标不太熟悉(这是我写的第一个)如果有人能发现它或任何其他错误我会非常感激

http://imgur.com/9k40O< -----图片更清晰(推荐)

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE CostTest_02
AS
BEGIN

DECLARE @Issue_id int;
DECLARE @Master_id int;
DECLARE @Issue_table varchar(255);
DECLARE @price real;
DECLARE @rowcount bigint;


DECLARE cost_cursor cursor FOR
SELECT [Issue Id], [Master Id], [Issue Table], [Price]
from Adhoc_datafix..[Issue Table]
FOR UPDATE OF ADHOC_DATAFIX..[Issue Cost];

OPEN cost_cursor;
FETCH NEXT FROM cost_cursor into @Issue_ID, @Master_id, @Issue_table, @Price

WHILE @@FETCH_STATUS = 0
BEGIN

Select count(*) from @Issue_Table
set @Rowcount = @@rowcount

UPDATE ADHOC_DATAFIX..[Issue Cost]
set  [Issue Id] = @Issue_ID ,
[Master Issue Id] = @Master_ID ,
[Row Count] = (Select count(*) from @Issue_Table), --@Rowcount,
[Cost] = CAST(@Rowcount * @price as money)

WHERE CURRENT OF cost_cursor;
FETCH NEXT FROM cost_cursor
END

close cost_cursor;

DEALLOCATE cost_cursor;
GO

2 个答案:

答案 0 :(得分:1)

我的猜测是脚本底部附近的行FETCH NEXT FROM cost_cursor应为FETCH NEXT FROM cost_cursor into @Issue_ID, @Master_id, @Issue_table, @Price

对不起,第二眼看这行:

Select count(*) from @Issue_Table

没有意义,因为@Issue_table实际上不是一个表,它是一个nvarchar 在运行时,转移到:

Select count(*) from 'Value of issue table field'
简而言之,它不起作用。 如果您想从表中动态获取计数,请查看此seminal article

答案 1 :(得分:0)

您的存储过程没有END语句。只需在GO

之前添加