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

怎么判断1段字符串里有几对括号,并用每个括号里的值做1次查询,最后将所有的查询结果组合在一起?
所有这些需要在1个sql里完成。

比如:“sum(abc)+avg(bcd)+sqrt(cde)”这个字符串里的()也许没有,也许1个或2个,但是最多2个。

然后(select * from table where code =abc)union all(select * from table where code =bcd)union all(select * from table where code =cde)


现在的问题是我用什么样的sql能判断出字符串里有几对括号,然后将这些查询结果统一到一个表里?

------解决方案--------------------
直接一条语句,全部执行完不行么?

如 
SQL code
EXECUTE IMMEDIATE '(select * from table where code =abc)union all(select * from table where code =bcd)union all(select * from table where code =cde)'

------解决方案--------------------
这条是找括号位置的:

SQL code
SELECT INSTR('(SELECT * FROM A WHERE CODE IN (SELECT CODE FROM B))', ')') AS POINTLEFT FROM DUAL)

------解决方案--------------------
用存储过程吧:

传入存储过程一个参数:你的SQL语句字符串!
存储过程输出一个结果:游标
------解决方案--------------------
没太看懂你要的结果,给出个过程的结构,自己把逻辑填写在
 /*sql处理代码*/这个地方吧。
SQL code

create or replace procedure test(result_message OUT VARCHAR2) is
result_sum varchar2(10);
result_avg varchar2(10);
result_sqrt varchar2(10);

begin
  /*sql处理代码*/
  
  result_message:=result_sum+result_avg+result_sqrt;
end test;

------解决方案--------------------
整个游标
SQL code
select substrb(rn,instr(rn,'(',1)+1)from
(select regexp_substr('sum(abc)+avg(bcd)+sqrt(cde)','[^)]+',1,rownum) rn from dual connect by rownum<=length(regexp_replace('sum(abc)+avg(bcd)+sqrt(cde)','[^)]+')) 
)

------解决方案--------------------
这个直接一条语句不就返回了吗?加 UNION ALL 啊?

SQL code
(select * from table where code =abc)
UNION ALL
(select * from table where code =bcd)
UNION ALL
(select * from table where code =cde)

------解决方案--------------------
给来个最简单的 
SQL code

select instr('1111112','1',-1)  from dual