如何使用sql语句返回在存储过程中的声明中填充pl sql中的变量

时间:2014-10-29 12:46:13

标签: sql database oracle stored-procedures

所以我在商店程序的声明上有一个变量。它必须具有在sql语句返回时填充的默认值。问题是该变量用于游标查询的声明,这就是为什么需要在声明上填充它。我该怎么做?

create or replace
PROCEDURE PROCESAR  AS

ultimaejecucion date := select fecha from table where rownum<=1;

--I know I have to use the word INTO but throws me error when i compile. 

cursor cursor_licencias is
select  lic.campo1
from  lic lic
where lic.licfhing >= ultimaejecucion


BEGIN
open cursor
.
.
.

END PROCESAR;

3 个答案:

答案 0 :(得分:1)

你的问题很不清楚。如果您的光标始终依赖于另一个表中的值,则应该包括此表以进行查询,如同Multisync建议的那样。如果光标还必须依赖于该表的不同行,则可以使用游标参数来选择该行:

create or replace
PROCEDURE PROCESAR  AS

cursor cursor_licencias (cursor_parameter number default 1234) is
  select  lic.campo1
  from  lic lic
  where lic.licfhing >= (select fecha from table where column = cursor_parameter);

BEGIN
  open cursor cursor_licencias;  -- using with default value

  open cursor cursor_licencias (5678);  -- using with value '5678'

END PROCESAR;

Example in oracle documentation.

答案 1 :(得分:0)

您可以将其移动到游标声明中:

create or replace
PROCEDURE PROCESAR  AS

cursor cursor_licencias is
select  lic.campo1
from  lic lic
where lic.licfhing >= (select fecha from table where rownum<=1);


BEGIN
open cursor
.
.
.

END PROCESAR;

如果你在其他地方需要这个字段,你可以在BEGIN之后立即将值分配给 ultimaejecucion

答案 2 :(得分:0)

我会做类似的事情:

declare

  l_date date;

  -- the value of l_date does not matter at this point
  cursor sel_cur is
  select 'It works' as val
  from dual
  where sysdate > l_date;

begin
    -- populate l_date using query
    select sysdate - 1
    into l_date
    from dual;

    -- open cursor (implicitly).  
    -- here the value of l_date is used in the execution of the query
    for rec in sel_cur
    loop
        dbms_output.put_line(rec.val);
    end loop;


end;