我是存储过程的新手,我创建了这个光标,它将在每晚的午夜运行,如果没有停止,则翻转最后几天的记录。执行时我每次都会收到错误。我不确定是什么造成的。任何帮助将非常感激。
消息102,级别15,状态1,程序nt_rollover_charge,第38行 'nt_cursor'附近的语法不正确。
CREATE PROCEDURE nt_rollover_charge
AS
BEGIN
SET NOCOUNT ON;
DECLARE @id VARCHAR(20);
DECLARE nt_cursor CURSOR FOR
SELECT DISTINCT log_id
FROM log_book
WHERE test_id = '3'
AND nt_dc_status = 'FALSE'
AND nt_current_date = 'TRUE';
OPEN nt_cursor
FETCH NEXT FROM nt_cursor INTO @id
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE log_book
SET nt_current_date = 'False'
WHERE log_id = @id
INSERT INTO log_book
(mrn,
fin,
ref_dr,
tech,
equip_id,
hookup_id,
indication,
abnormality,
location_id,
test_id,
charged,
start_date,
nt_rollover_date,
nt_dc_status,
nt_current_date)
(SELECT mrn,
fin,
ref_dr,
tech,
equip_id,
hookup_id,
indication,
abnormality,
location_id,
test_id,
NULL,
start_date,
getdate(),
'FALSE',
'TRUE'
FROM log_book
WHERE @id = log_id)
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM nt_cursor INTO @id
END
CLOSE nt_cursor
DEALLOCATE nt_cursor
END
答案 0 :(得分:1)
首先,您不应该使用游标,因为这会破坏使用SQL和数据库的目的,这是基于集合的操作。只需写下你的陈述:
Update Log_book set
nt_current_date = 'False'
from
Log_book inner join
Log_book L2
on L2.log_id = log_book.log_id
where
L2.test_id = '3' and L2.nt_dc_status = 'FALSE';
INSERT INTO log_book(mrn, fin, ref_dr, tech, equip_id, hookup_id, indication, abnormality, location_id, test_id, charged, start_date, nt_rollover_date, nt_dc_status, nt_current_date)
SELECT log_book.mrn, log_book.fin, log_book.ref_dr, log_book.tech, log_book.equip_id, log_book.hookup_id, log_book.indication, log_book.abnormality, log_book.location_id, log_book.test_id, null, log_book.start_date, getdate(), 'FALSE', 'TRUE'
FROM log_book
inner join
Log_book L2
on L2.log_id = log_book.log_id
where
L2.test_id = '3' and L2.nt_dc_status = 'FALSE';
至于您的实际问题,您发布的内容只有33行,但错误发生在第38行。您的存储过程结束时是否有END语句?