关键字' CURSOR'附近的语法不正确+ @cursor

时间:2018-01-17 11:28:12

标签: sql-server

我尝试使用游标进行此查询,但我不断收到以下错误:

关键字' CURSOR'附近的语法不正确/ @cursor /附近的语法不正确 @cursor /附近的语法不正确 @cursor附近的语法不正确

有人能帮助我吗?

由于

DECLARE @maxObject INT
DECLARE @maxVcoObject INT
DECLARE @cursor CURSOR
DECLARE @pointerVcontract INT
DECLARE @aantalObjecten INT
SET @maxVcoObject = (SELECT max(p_vco_object) FROM vco_object)
SET @maxObject = (SELECT MAX(p_object) FROM object)
SET @aantalObjecten = (SELECT COUNT(*) FROM vcontract WHERE p_vcontract NOT IN(SELECT p_vcontract FROM vco_object))
SET @maxObject = @maxObject - @aantalObjecten
SET @cursor = CURSOR FOR
SELECT
p_vcontract
FROM
vcontract, vco_partcp WHERE vcontract.p_vcontract = vco_partcp.p_vcontract AND vcontract.p_vcontract NOT IN (SELECT p_vcontract FROM vco_object)
OPEN @cursor
FETCH NEXT FROM @cursor INTO @pointerVContract
WHILE @@FETCH_STATUS = 0
BEGIN 
SET @maxVcoObject = @maxVcoObject + 1
SET @maxObject = @maxObject + 1
INSERT INTO vco_object (p_vco_object, p_vcontract, p_object, create_date, create_user, last_date, last_user)
VALUES (@maxVcoObject, @pointerVContract, @maxObject, getDate(), "MIG", getDate(), "MIG")
FETCH NEXT FROM @cursor INTO @pointerVContract
END
CLOSE @cursor
DEALLOCATE @cursor

2 个答案:

答案 0 :(得分:1)

您不需要光标在第一时间执行此操作

img

答案 1 :(得分:0)

这里的问题是这一行:

VALUES (@maxVcoObject, @pointerVContract, @maxObject, getDate(), "MIG", getDate(), "MIG")

将其替换为:

VALUES (@maxVcoObject, @pointerVContract, @maxObject, getDate(), 'MIG', getDate(), 'MIG')

它应该有效。 TSQL语法仅支持字符串文字的单引号。

正如其他人建议的那样尽量避免使用游标。