Oracle 在循环外部设置一个变量,并在循环的每一项中提取它

Oracle 在循环外部设置一个变量,并在循环的每一项中提取它,oracle,stored-procedures,oracle10g,Oracle,Stored Procedures,Oracle10g,我的oracle版本:oracle数据库10g企业版10.2.0.4.0-64bi版 我试图在循环外设置一个变量,并使其值随着循环的每一项而改变。(就像我们通常在jsp、php中所做的那样…) 但结果是 aaa, 8 bbb, 8 --`v_variable` stays the same ... v_变量不变。 请纠正我的程序。试试这个 create or replace PROCEDURE TEST2 AS -- VARIABLE v_variable number; --cursor

我的oracle版本:
oracle数据库10g企业版10.2.0.4.0-64bi版

我试图在循环外设置一个变量,并使其值随着循环的每一项而改变。(就像我们通常在jsp、php中所做的那样…)

但结果是

aaa, 8
bbb, 8 --`v_variable` stays the same
...
v_变量
不变。 请纠正我的程序。

试试这个

create or replace PROCEDURE TEST2 AS 
-- VARIABLE
v_variable number; 
--cursor c2 is
--select round(dbms_random.value() * 8) + 1 AS temp_key from dual; -- temp_key is a random value

BEGIN  
--OPEN c2;
FOR SRC IN (SELECT * FROM TB_MASTER_TEMP2) -- it has many rows
 LOOP
    --fetch c2 into v_variable; -- v_variable need to change for every row
    select round(dbms_random.value() * 8) + 1 into v_variable from dual; 
    Dbms_Output.Put_Line(SRC.MAS_ENTRY_NM || ', ' ||v_variable); --test
 END LOOP;

END TEST2;
试试这个

create or replace PROCEDURE TEST2 AS 
-- VARIABLE
v_variable number; 
--cursor c2 is
--select round(dbms_random.value() * 8) + 1 AS temp_key from dual; -- temp_key is a random value

BEGIN  
--OPEN c2;
FOR SRC IN (SELECT * FROM TB_MASTER_TEMP2) -- it has many rows
 LOOP
    --fetch c2 into v_variable; -- v_variable need to change for every row
    select round(dbms_random.value() * 8) + 1 into v_variable from dual; 
    Dbms_Output.Put_Line(SRC.MAS_ENTRY_NM || ', ' ||v_variable); --test
 END LOOP;

END TEST2;

除非有人玩弄了它,否则,
dual
只有一行,因此
c2
返回的结果集也只有一行。任何超出该结果集末尾的获取尝试都将一次又一次地返回最后一行(也是唯一一行)


如果要在循环的每次迭代中检索不同的随机值,则需要执行
SELECT。。。每次循环时从dual
开始,就像在@Utsav的代码中一样。

除非有人用dual玩了愚蠢的bug,否则dual只有一行,因此
c2
返回的结果集也只有一行。任何超出该结果集末尾的获取尝试都将一次又一次地返回最后一行(也是唯一一行)

Hello i have slightly tweaked your code. It may help you. Since i dont have workspace with me so plz bear with any syntax errors.

CREATE OR REPLACE PROCEDURE TEST2
AS
  -- VARIABLE
  v_variable NUMBER;
BEGIN
  --  OPEN c2; -- Not required
  FOR SRC IN
  (SELECT t2.*,
    ROUND(dbms_random.value() * 8) + 1 AS temp_key
  FROM TB_MASTER_TEMP2 t2
  ) -- it has many rows
  LOOP
    -- v_variable need to change for every row
    Dbms_Output.Put_Line(SRC.MAS_ENTRY_NM || ', ' ||src.temp_key); --test
  END LOOP;
END TEST2;

如果要在循环的每次迭代中检索不同的随机值,则需要执行
SELECT。。。每次循环时,从dual
开始,就像@Utsav的代码一样。

现在我明白了。我想每次调用
fetch
时,它都会执行
select
并提取到变量中。现在我明白了。我想每次调用
fetch
时,它都会执行
select
并提取到变量中。
Hello i have slightly tweaked your code. It may help you. Since i dont have workspace with me so plz bear with any syntax errors.

CREATE OR REPLACE PROCEDURE TEST2
AS
  -- VARIABLE
  v_variable NUMBER;
BEGIN
  --  OPEN c2; -- Not required
  FOR SRC IN
  (SELECT t2.*,
    ROUND(dbms_random.value() * 8) + 1 AS temp_key
  FROM TB_MASTER_TEMP2 t2
  ) -- it has many rows
  LOOP
    -- v_variable need to change for every row
    Dbms_Output.Put_Line(SRC.MAS_ENTRY_NM || ', ' ||src.temp_key); --test
  END LOOP;
END TEST2;