我的目的是解析第一个表,检查第二个表中是否存在ID,然后进行一些更新。
第一个表在循环中运行并解析第二个表中的每一行以更新或给出输出。
这里是我使用的代码:
DECLARE
CURSOR c1 FOR (SELECT pnr,agrnumber,pnrcreatedate FROM test2_view_table);
r1 c1%ROWTYPE;
CURSOR c2 FOR (SELECT pnr,ano,pcdt FROM sdh_ticket_test2_update);
r2 c2%ROWTYPE;
BEGIN
FOR r1 IN c1
LOOP
FOR r2 IN c2
LOOP
IF (r1.pnr = r2.pnr and r2.pnrcreatedate is null and
r1.agrnumber=r2.ano)
THEN
c++
else if (r1.pnr = r2.pnr and r2.agrnumber is null and
r1.pcdt=r2.pnrcreatedate)
THEN
a++ -- continue to the next iteration of the outer loop
-- as we have a match
GOTO continue;
END IF;
END LOOP;
-- we can only reach that point if there was no match
DBMS_OUTPUT.PUT_LINE(TO_CHAR(c));
<<continue>>
NULL;
END LOOP;
END;
如上所述在redshift中声明游标。它给我发了以下错误。
[Amazon](500310) Invalid operation: syntax error at or near "c1"
如果有任何解决方案,请告诉我。
答案 0 :(得分:0)
这种逐行处理不适用于任何现代数据库。只需在SQL查询(或查询)中定义逻辑,让DB找出如何进行实际工作。
在这种情况下,您可以一步完成整个比较。实际上,您可以将其重写为单个UPDATE
。
BEGIN
UPDATE sdh_ticket_test2_update
SET pnrcreatedate = c1.pcdt
FROM test2_view_table c1
LEFT JOIN sdh_ticket_test2_update c2
ON c1.pnr = c2.pnr
AND c1.agrnumber = c2.ano
WHERE c2.pnrcreatedate IS NULL
;
UPDATE sdh_ticket_test2_update
SET ano = c1.agrnumber
FROM test2_view_table c1
LEFT JOIN sdh_ticket_test2_update c2
ON c1.pnr = c2.pnr
AND c1.pcdt = c2.pnrcreatedate
WHERE c2.agrnumber IS NULL
;
COMMIT