我如何在oracle过程中运行多个select语句

时间:2013-01-31 14:22:42

标签: oracle plsql

我写了以下程序

create or replace procedure sp_abc_profile 
(
  f_symbol_in abc.colname%TYPE
)
is profile abc%rowtype;
is profile2 abc2%rowtype;
begin
  SELECT fname, lname,mname,age
    INTO profile
  FROM abc
  WHERE f_symbol = f_symbol_in;

  SELECT initiaiinvestment AS minInitialInvestment, pr as qt, class1 as clss
    into profile2
  FROM 
      abc2 
  WHERE f_symbol = f_symbol_in;
end;

执行上述后,我收到如下错误消息:

  

错误(7,3):PL / SQL:忽略SQL语句

     

错误(21,5):PL / SQL:ORA-00913:值太多

我不想选择两个表中的所有行。

如何在过程中编写多个select语句,以便过程中的每个select语句都返回一个结果集。

5 个答案:

答案 0 :(得分:2)

尝试以下方法:

is
profile abc.fname%type;
profile2 abc2.initiaiinvestment%type;

在过程中有多个select语句没有问题。它与所选列和PL / SQL类型不匹配。

除此之外,您的代码中似乎有太多is

有关使用select into的详情,请点击此链接:Oracle PL/SQL "select into" clause

答案 1 :(得分:0)

试试这个我们可以在程序中有多个select语句:

您的代码中存在太多价值问题。

create or replace procedure sp_abc_profile 
(
  f_symbol_in abc.colname%TYPE
)
is profile abc.fname%type;
is profile2 abc2.initiaiinvestment%type;
begin
  SELECT fname
    INTO profile
  FROM abc
  WHERE f_symbol = f_symbol_in;

  SELECT initiaiinvestment into profile2
  FROM 
      abc2 
  WHERE f_symbol = f_symbol_in;
end;

答案 2 :(得分:0)

正如其他人指出你有太多“是”的陈述。

执行select into a row变量时,需要选择所有内容:

select *
into profile
from abc
where f_symbol = f_symbol_in;

select *
into profile2
from abc2
where f_symbol = f_symbol_in;

当你在f_symbol_in上有多个匹配项时,你也冒着抛出异常的风险。您的代码可以捕获此异常,或者您可以限制行数(即rownum< = 1),或者查看集合以加载与参数匹配的所有行。

答案 3 :(得分:0)

解决方案是在oracle中使用CURSORS,以便过程中的每个select语句都返回一个结果集。

然后可以使用您首选的脚本语言遍历该结果集以获得所需的输出。

create or replace 
procedure sp_abc_profile
(
  symbol_in in tablename.fieldname%type,
  cursor1 out SYS_REFCURSOR,
  cursor2 out SYS_REFCURSOR,
)
as
begin


    open cursor1 for
         {your select statement here}


    open cursor2 for
        {your second select statement here}


end sp_abc_profile;

答案 4 :(得分:0)

  1. 删除多余的“IS”
  2. 添加:ROWNUM = 1
  3. create or replace procedure sp_abc_profile 
    (
      f_symbol_in abc.colname%TYPE
    )
    is profile abc%rowtype;
    /*is*/ profile2 abc2%rowtype;
    begin
      SELECT fname, lname,mname,age
        INTO profile
      FROM abc
      WHERE f_symbol = f_symbol_in
      AND ROWNUM = 1;
    
      SELECT initiaiinvestment AS minInitialInvestment, pr as qt, class1 as clss
        into profile2
      FROM 
          abc2 
      WHERE f_symbol = f_symbol_in
      AND ROWNUM = 1;
    end;