在Oracle 10G中搜索和替换字符串中的方括号

在Oracle 10G中搜索和替换字符串中的方括号,oracle,oracle10g,replace,Oracle,Oracle10g,Replace,我目前正在开发一个函数,用于执行动态创建的SQL语句。这是通过连接列并通过游标获取它们来实现的。问题是,当函数的参数之间有逗号时,concat会连接包含的函数的内容 是否可以使用REGEXP\u SUBTR或REGEXP\u REPLACE跳过字符串中每个括号的内容 非常感谢您的及时和友好的建议 -- strips out the select list src_str := REGEXP_SUBSTR(v_sql, 'SELECT ([[:graph:]]+\ ?){1,1000000}/?

我目前正在开发一个函数,用于执行动态创建的SQL语句。这是通过连接列并通过游标获取它们来实现的。问题是,当函数的参数之间有逗号时,concat会连接包含的函数的内容

是否可以使用REGEXP\u SUBTR或REGEXP\u 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:=('demo@demo.com","2001")

目前,它返回

v_query := SELECT A, B, C FROM DEMO WHERE USERID = :USERID AND YEAR = 2001
并跳过列表中的第一个用户ID。
非常感谢您预期的帮助

如果我正确理解您的要求,这样做应该可以:

-- multiple replacements to accomodate for functions with more
-- than two parameters (and accordingly more than one comma)
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');

-- replace the left-over commas
src_str := replace(src_str, ', ', p_dot);

-- turn commas within function call back to commas:
src_str := replace(src_str, '##comma-in-function##', ',');

您是否考虑过使用DBMS_SQL,它应该解析SQL并允许您绑定变量

请参阅这些链接以进一步阅读


请参见上面编辑的问题。谢谢。请提供您的
v_sql
、当前结果和所需结果?它是一个函数,用于生成传递的任何sql语句的报告,其中包含动态sql语句,这些sql语句在选择列表中可能有函数,也可能没有函数。我已经能够绕过它,使所有sql语句中的所有函数在找到逗号后没有任何空格,这意味着只要在逗号后找到空格,replace就会被替换;但我认为应该有更好的办法。提前谢谢。非常感谢雷内,你的问题解决了,但我还有一个问题。是否可以循环使用regexp\u replace和regexp\u subtr函数?我已经编辑了以前的语法以包含它。在循环结束时,它跳过列表中的第一个用户ID。非常感谢您预期的帮助。我觉得您使用dbms_sql可能会更好,但这是因为我不太明白您想要实现什么。该函数是一个通用的报告工具,如果报告和版本需要任何sql语句,它将获取报告名、版本和变量。所有这些都将存储在一个表中,该表将由函数检索和处理,并以字符串形式返回,该字符串可在所有应用程序中使用。是的,我想到了DBMS_SQL,但由于性能问题,通常建议不要使用。我认为这种方法会更容易,但从表面上看;事实并非如此。再次非常感谢Rene,他将研究DBMS_SQL。祝你周末愉快。非常感谢保罗,我会调查的;虽然这种方法会更容易,但从表面上看,情况并非如此。
-- multiple replacements to accomodate for functions with more
-- than two parameters (and accordingly more than one comma)
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');
src_str := regexp_replace(src_str, '(\([^)]+),', '\1##comma-in-function##');

-- replace the left-over commas
src_str := replace(src_str, ', ', p_dot);

-- turn commas within function call back to commas:
src_str := replace(src_str, '##comma-in-function##', ',');