存储过程中要求从一个参数表中取出参数名称和参数值来组成一个sql语句,执行这个SQL,返回游标类型
参数表tb_para:
paraID paraName paraValue
1 name zhangdan
1 age 30
2 name lisi
3 name wangwu
如果传入参数为1,那么sql语句就是:select * from tb_table where name='zhangdan' and age='30'
因为是用JAVA调用,不太会写存储过程,大概存储过程如下
CREATE OR REPLACE PROCEDURE pro_query
(
paraID IN int,
p_cus OUT TYPES.cursorType --我定义的返回游标,用这个返回给JAVA调用端
)
AS
vs_lx VARCHAR2(200);
BEGIN
--根据传入的参数paraID ,去tb_para循环取得参数名和参数值
--组合SQL语句,比如传入参数是1,那组合成的语句就是select * from tb_table where name='zhangdan' and age='30'
--返回游标参数赋值
OPEN p_cus FOR --刚才那个组合成的语句,最后返回的就是这个类型
End;
HELP.....
------解决方案--------------------
- SQL code
CREATE OR REPLACE PROCEDURE pro_query( paraID IN int, p_cus OUT TYPES.cursorType --我定义的返回游标,用这个返回给JAVA调用端)AS vs_lx VARCHAR2(200); v_paraname varchar2(100); v_paravalue varchar2(100); v_sql varchar2(1000); cursor c_getpar(para_id number) is select paraname,paravalue from tb_para where paraid = :para_id;BEGIN --根据传入的参数paraID ,去tb_para循环取得参数名和参数值 v_sql := 'select * from tb_table where 1=1 '; open c_getpar(paraID); fetch c_getpar into v_paraname,v_paravalue; while c_getpar%found loop --组合SQL语句,比如传入参数是1,那组合成的语句就是select * from tb_table where name='zhangdan' and age='30' v_sql := v_sql || ' ' || v_paraname || ' = ' || v_paravalue; fetch c_getpar into v_paraname,v_paravalue; end loop; --返回游标参数赋值 OPEN p_cus FOR v_sql; --刚才那个组合成的语句,最后返回的就是这个类型End;
------解决方案--------------------
- SQL code
create or replace procedure GetResultProc(in_paraid in varchar2, out_ResultSql out varchar2 ) as sSql varchar2(2000); sSql1 varchar2(2000); type v_Curtype is ref cursor; v_cur v_Curtype;-- v_ResultCur v_curtype; s_paraname varchar2(20); s_paravalue varchar2(20); iCount number;begin if in_paraid is null then return; end if; sSql := 'select paraname,paravalue from tb_para where paraid='||in_paraid; iCount := 0; open v_cur for sSql; loop fetch v_cur into s_paraname,s_paravalue; exit when v_cur%notfound; if iCount = 0 then sSql1 := 'select * from tb_table where '||s_paraname||'='''||s_paravalue||''''; else sSql1 := sSql1||' and '||s_paraname||'='''||s_paravalue||''''; end if; iCount := iCount+1; end loop; close v_cur; if sSql1 is null then return; end if; -- open v_ResultCur fro sSql1; out_ResultSql := sSql1;end;