日期:2014-05-17  浏览次数:20803 次

求一存储过程的写法
想写一存储过程,实现如下功能:
求指点怎么把第一个SQL查询出的字段值,作为第二次查询的参数

  BEGIN
  select 字段1, 字段2, 字段3 from 表A where code='test'
--把字段1、字段2、字段3 作为参数 带入第二个SQL语句

open RC_P for select * from 表2 where rownum <=字段1 and code=字段2 order by 字段3 desc
   
  END HOT;

------解决方案--------------------
这涉及到PL/SQL中动态SQL语句的执行问题。
关于动态SQL语句,给你举个例子,请参考。

------解决方案--------------------
给你个例子
SQL code
DECLARE
   TYPE ref_cursor_type IS REF CURSOR;

   ref_cursor_type   RC_P;
   v_1               表A.字段1%TYPE;
   v_2               表A.字段2%TYPE;
   v_3               表A.字段3%TYPE;
BEGIN
   SELECT   字段1, 字段2, 字段3
     INTO   v_1, v_2, v_3
     FROM   表A
    WHERE   code = 'test'; --只能有一行记录被找到,否则会报错

   OPEN RC_P FOR
         'select * from 表2 where rownum <='
      || v_1
      || ' and code='''
      || v_2
      || ''' order by '
      || v_3
      || ' desc';
END;

------解决方案--------------------
SQL code
procedure HOT
is
filed1 table_A.字段1%type;
filed2 table_A.字段2%type;
filed3 table_A.字段3%type;
begin
select 字段1, 字段2, 字段3
into
filed1,filed2,filed3
from table_A where code='test';
open RC_P for select * from 表2 where rownum <=filed1 and code=filed2 order by filed3 desc;
end HOT

------解决方案--------------------
procedure HOT
is
filed1 table_A.字段1%type;
filed2 table_A.字段2%type;
filed3 table_A.字段3%type;
begin
select 字段1, 字段2, 字段3
into
filed1,filed2,filed3
from table_A where code='test';
open RC_P for select * from 表2 where rownum <=filed1 and code=filed2 order by filed3 desc;
end HOT
------解决方案--------------------
看见代码中的中文就晕。
------解决方案--------------------
procedure HOT
is
filed1 table_A.字段1%type;
filed2 table_A.字段2%type;
filed3 table_A.字段3%type;
begin
select 字段1, 字段2, 字段3
into
filed1,filed2,filed3
from table_A where code='test';
open RC_P for select * from 表2 where rownum <=filed1 and code=filed2 order by filed3 desc;
end HOT