我目前正在开发一个旨在执行动态创建的SQL语句的函数。这是通过连接列并通过光标获取它们来完成的。问题是当函数的参数之间有一个逗号时,concat会连接函数的内容。在Oracle 10G中搜索并替换字符串中的括号
是否可以使用REGEXP_SUBTR或REGEXP_REPLACE跳过字符串中找到的每个括号的内容?
非常感谢您的迅速和善意的建议。
-- strips out the select list
src_str := REGEXP_SUBSTR(v_sql, 'SELECT ([[:graph:]]+\ ?){1,1000000}/?');
-- Replace the commas in the select list with the concat symbol for concatenation
rep_str := REGEXP_REPLACE(src_str, ', ', p_dot);
-- Replace the select list with the replace string
v_query := REPLACE(v_sql, src_str, rep_str);
v_sql := select a, b, to_char(sysdate, 'dd/mm/yyyy') from demo;
p_dot := '||'',''||';
目前,它返回:
select a || ',' || b || ',' || to_char(sysdate || ',' || 'dd/mm/yyyy') from demo
但应该返回类似:
select a || ',' || b || ',' || to_char(sysdate, 'dd/mm/yyyy') from demo
非常感谢刘若英,您的查询工作,但我有一个问题,就是这样
for i in 1 .. p_arglist.count
loop
-- Search for : in the query
src_sym := REGEXP_SUBSTR(v_query, ':[[:graph:]]+\ ?', i,i);
-- Replace the : with each value of p_arglist passed
v_querymult := REGEXP_REPLACE(v_query, src_sym , p_arglist(i),i,i);
end loop;
return v_query;
其中p_arglist是一个VARCHAR2 VARRAY p_arglist:=( '[email protected]', '2001')
v_query := 'SELECT A, B, C FROM DEMO WHERE USERID = :USERID AND YEAR = :YEAR';
目前,它返回
v_query := SELECT A, B, C FROM DEMO WHERE USERID = :USERID AND YEAR = 2001
并且跳过在其列表中的第一用户标识。 非常感谢您的预期帮助,如果我理解正确的您的要求
帖子你这么远的东西。 – 2010-03-18 14:33:29
请参阅上面编辑的问题。谢谢 – Tunde 2010-03-18 14:38:02
你能否提供你的'v_sql',目前的结果和期望的结果呢? – 2010-03-18 14:41:00