oracle 如何使用oracle在循环中设置变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18139418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set variables in a loop with oracle?
提问by LazyCatIT
I have a loop and a variable v_rownum
and I want to set value for it:
我有一个循环和一个变量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;
But it throw a exception:
但它抛出一个异常:
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.
I'm currently studying Oracle. I don't know much about it. Please help me to resolve my problem.
我目前正在学习甲骨文。我对此知之甚少。请帮我解决我的问题。
回答by Ed Gibbs
With Oracle you need to declare all your variables at the top of the block, before the BEGIN
:
使用 Oracle,您需要在块的顶部声明所有变量,在BEGIN
:
DECLARE
... variable declarations
BEGIN
... program logic
END;
Your code should go something like this:
你的代码应该是这样的:
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;
回答by Julian Kolodzey
How is your studying?;) I use this:
你的学习怎么样?;) 我用这个:
DECLARE
q NUMBER;
BEGIN
q := &x;
for x IN 1 .. q LOOP
回答by Ashi
Try this
尝试这个
FOR donvi_rows IN v_donvi
LOOP
DECLARE
v_rownum number;
BEGIN
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;
END LOOP;