简体   繁体   English

SQL游标语法问题

[英]SQL Cursor Syntax Issue

Im getting this error Msg 102, Level 15, State 1, Procedure CostTest_02, Line 43 Incorrect syntax near ';'. 我收到此错误Msg 102,级别15,状态1,过程CostTest_02,第43行';'附近的语法不正确。

With "GO" underlined in red I've been looking for the error for a while and I can't find! 用红色下划线标记“ GO”后,我一直在寻找错误,但我找不到! I'm not very experienced with cursors( this is the first one I've written) If anyone can spot it or any other errors I'd be very thankful 我对游标不是很有经验(这是我写的第一个)。如果有人可以发现它或任何其他错误,我将非常感谢

http://imgur.com/9k40O <----- picture is a lot more clear(recommended) 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

My guess is that the line FETCH NEXT FROM cost_cursor near the bottom of the script should be FETCH NEXT FROM cost_cursor into @Issue_ID, @Master_id, @Issue_table, @Price . 我的猜测是脚本底部附近的 FETCH NEXT FROM cost_cursor行应该是 FETCH NEXT FROM cost_cursor into @Issue_ID, @Master_id, @Issue_table, @Price

Sorry, on second look this line: 抱歉,第二行显示:

Select count(*) from @Issue_Table

does not make sense since @Issue_table is not actually a table, it's a nvarchar. 因为@Issue_table实际上不是一个表,所以它是没有意义的,它是一个nvarchar。
In runtime, that is transferred to: 在运行时,将其传输到:

Select count(*) from 'Value of issue table field'

which, to put it simply, does not work. 简单地说,这是行不通的。 If you want to dynamically get the count from a table, take a look at this seminal article 如果您想从表中动态获取计数,请看这篇开创性的文章

You do not have an END statement for your stored procedure. 您的存储过程没有END语句。 Just add that before GO 只需在GO之前添加

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

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