所有这些需要在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能判断出字符串里有几对括号,然后将这些查询结果统一到一个表里?
select (length('abcdabgjhdhfjkgab')-length(replace('abcdabgjhdhfjkgab','ab')))/length('ab') from dual;
EXECUTE IMMEDIATE '(select * from table where code =abc)union all(select * from table where code =bcd)union all(select * from table where code =cde)'否则,我想最好是做个存储过程,或者是函数,来返回你最终要的一条语句了!思路:1、先从这个字符串找到第一个“)”括号,再从这个右括号开始向左找,自右开始找的第一个左括号,这就是你要的第一条语句了,把这条语句先存到别处,不过要记录它在这个字符串中的起始位置;
2、从最原始的字符串中清掉刚才那条语句,然后接着以此类推找第二条、第三条语句,直到全部找完!
sum(abc)+avg(bcd)
sum(abc)+avg(bcd)+sqrt(cde)
存储过程输出一个结果:游标
/*sql处理代码*/这个地方吧。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;
比如:“sum(abc)+avg(bcd)+sqrt(cde)”这个字符串里的()也许没有,也许1个或2个,但是最多2个。
然后分别得到下面3个或2个或1个查询的结果,并返回(select * from table where code =abc)(select * from table where code =bcd)(select * from table where code =cde)
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
(select * from table where code =bcd)
UNION ALL
(select * from table where code =cde)想不出,你这个不能用一条语句的情况!如果真的不能,那是为什么不能啊?