Sql 用查询中的值替换部分字段

Sql 用查询中的值替换部分字段,sql,oracle,Sql,Oracle,我有一个字段,其中填充了一系列特定格式的标准: CRITERIA $1 = True $2 > $3 标准信息存储在一个表中 CRIT_ID CRIT_DESCRIPTION 1 Example 1 2 Example 2 3 Example 3 最终输出应该如下所示 CRITERIA Example 1 = True Example 2 > Example 3 有人能建议如何最好地实现这一点吗?到目前为止,我已经尝试过REPLACE并尝试过

我有一个字段,其中填充了一系列特定格式的标准:

CRITERIA
$1 = True
$2 > $3
标准信息存储在一个表中

CRIT_ID CRIT_DESCRIPTION
1       Example 1
2       Example 2
3       Example 3
最终输出应该如下所示

CRITERIA
Example 1 = True
Example 2 > Example 3

有人能建议如何最好地实现这一点吗?到目前为止,我已经尝试过REPLACE并尝试过REGEXP_REPLACE…

在下面的查询中,我使用第一个空格作为id和等式之间的分隔符(并假设id总是从第二个字符开始)


也许不是最有效的,但它是递归工作的(也就是说,如果
crit\u description
本身包含“占位符”,那么这些占位符也会被展开。(第一个解决方案,比下面所示更简单,没有执行此递归步骤。)请参阅我添加的第三个示例输入。如果我能进一步清理它,我将稍后再次发布

注意:这假设所有的“占位符”实际上都在
标准信息
表中找到;我没有测试如果找不到它们会发生什么。请点击说明需求

with
     inputs ( criteria ) as (
       select '$1 = True' from dual union all
       select '$2 > $3'   from dual union all
       select '$1 = $4'   from dual
     ),
     criteria_info ( crit_id, crit_description ) as (
       select 1, 'Example 1' from dual union all
       select 2, 'Example 2' from dual union all
       select 3, 'Example 3' from dual union all
       select 4, '$2 + $3'   from dual
     ),
     rec ( criteria, new_str ) as (
       select  criteria, criteria
         from  inputs        
       union all       
       select  r.criteria, 
               regexp_replace(r.new_str, '\$\d+', c.crit_description, 1, 1)
         from  rec r inner join criteria_info c
               on to_number(regexp_substr(r.new_str, '\$(\d+)', 1, 1, null, 1)) = c.crit_id
         where regexp_substr(r.new_str, '\$\d+') is not null
     )
select criteria, new_str
from   rec
where  regexp_substr(new_str, '\$\d+') is null
;


CRITERIA  NEW_STR
--------- ------------------------------------
$1 = True Example 1 = True
$2 > $3   Example 2 > Example 3
$1 = $4   Example 1 = Example 2 + Example 3

3 rows selected.

谢谢Neria,但这不允许在字段中出现多个条件(请参见第2行)。它在运行时也会失败,出现“无效数字”异常。发布代码之前是否测试了代码?@mathguy谢谢,我修复了WHERE's
SUBSTR
中的最后一个参数。
with
     inputs ( criteria ) as (
       select '$1 = True' from dual union all
       select '$2 > $3'   from dual union all
       select '$1 = $4'   from dual
     ),
     criteria_info ( crit_id, crit_description ) as (
       select 1, 'Example 1' from dual union all
       select 2, 'Example 2' from dual union all
       select 3, 'Example 3' from dual union all
       select 4, '$2 + $3'   from dual
     ),
     rec ( criteria, new_str ) as (
       select  criteria, criteria
         from  inputs        
       union all       
       select  r.criteria, 
               regexp_replace(r.new_str, '\$\d+', c.crit_description, 1, 1)
         from  rec r inner join criteria_info c
               on to_number(regexp_substr(r.new_str, '\$(\d+)', 1, 1, null, 1)) = c.crit_id
         where regexp_substr(r.new_str, '\$\d+') is not null
     )
select criteria, new_str
from   rec
where  regexp_substr(new_str, '\$\d+') is null
;


CRITERIA  NEW_STR
--------- ------------------------------------
$1 = True Example 1 = True
$2 > $3   Example 2 > Example 3
$1 = $4   Example 1 = Example 2 + Example 3

3 rows selected.