gpt4 book ai didi

oracle - 如何使用oracle在循环中设置变量?

转载 作者:行者123 更新时间:2023-12-01 09:29:05 27 4
gpt4 key购买 nike

我有一个循环和一个变量 v_rownum我想为它设置值:

        FOR donvi_rows IN v_donvi
LOOP
DECLARE
v_rownum number;
SELECT r
INTO v_rownum
FROM
(SELECT ROWNUM AS r, k.Id
FROM don_vi k
WHERE k.ParentId = 1 )
WHERE Id = donvi_rows.Id;

END LOOP;

但它抛出一个异常:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:

begin function pragma procedure subtype type <an identifier>
<a double-quoted delimited-identifier> current cursor delete
exists prior
The symbol "begin" was substituted for "SELECT" to continue.

我目前正在学习甲骨文。我对它了解不多。请帮我解决我的问题。

最佳答案

使用 Oracle,您需要在块的顶部声明所有变量,在 BEGIN 之前。 :

DECLARE
... variable declarations
BEGIN
... program logic
END;

你的代码应该是这样的:
DECLARE 
v_rownum number;
CURSOR v_donvi IS
SELECT * FROM whatever;
... any other declarations
BEGIN
FOR donvi_rows IN v_donvi
LOOP
SELECT r
INTO v_rownum
FROM (
SELECT ROWNUM AS r, k.Id
FROM don_vi k
WHERE k.ParentId = 1
)
WHERE Id = donvi_rows.Id;
END LOOP;
END;

关于oracle - 如何使用oracle在循环中设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18139418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com