在oracle中构建动态查询

时间:2014-03-29 16:37:50

标签: oracle build merge query-optimization

你能帮我建一个如下的查询吗?

TABLE_A

id品牌颜色尺码型号yn_buy
1 A蓝色M A -
2灰色X C -
3 B红色X B -
4 C blue S C -

表-B

品牌标准 颜色=灰色,尺寸= X
B颜色=红色
C size = M


我想构建一个更新表A的查询,答案应该是:

TABLE_A

id品牌颜色尺码型号yn_buy
1 A蓝色M A N
2灰色X C Y
3 B red X B Y
4 C blue S C N


正如您所看到的那样,“批评”一栏上的数据应该是买或不买的决定因素

我想使用单个合并,如下所示

合并到TABLE_A中 使用

 选择ID,品牌, 批判 时的情况>然后'Y'ELSE'N'END yn_buy
 TABLE_A a
 left join TABLE_B b ON a.brand = b.brand
)b
ON(a.id = b.id)
匹配然后更新设置a.yn_buy = b.yn_buy

可以这样做吗?也许使用执行立即,某种绑定...?

谢谢你

1 个答案:

答案 0 :(得分:0)

如果我在你的特定情况下不遗漏你不需要动态SQL - 你可以简单地改变TABLE_B结构并使用静态MERGE(因为我不知道你的标准有多复杂,这只是一个例子):

SQL> create table table_a (id, brand, color, size#, model#, tn_buy) as
  2  select 1,    'A',          'blue',             'M',           'A', cast(null as varchar2(3)) from dual union all
  3  select 2,    'A',         'grey',              'X',          'C', cast(null as varchar2(3)) from dual union all
  4  select 3,    'B',         'red',               'X',           'B', cast(null as varchar2(3)) from dual union all
  5  select 4,    'C',        'blue',              'S',           'C', cast(null as varchar2(3)) from dual
  6  /

Table screated.

SQL> create table TABLE_B (brand, color, size#)
  2  as
  3  select 'A',          'grey', 'X' from dual union all
  4  select 'B',           'red', null from dual union all
  5  select 'C',          null, 'M' from dual
  6  /

Table created.

SQL> merge into TABLE_A a USING (
  2     select a.id, a.brand, CASE
  3     WHEN a.color = nvl(a.color, a.color) and a.size# = nvl(b.size#,a.size#)
  4     THEN 'Y' ELSE 'N' END yn_buy FROM
  5       TABLE_A a
  6       left join TABLE_B b ON a.brand = b.brand
  7  ) b
  8  ON (a.id = b.id)
  9  WHEN MATCHED THEN UPDATE set a.yn_buy = b.yn_buy
 10  /

4 rows merged.

SQL> select * from table_a order by id;

        ID B COLO S M YN_                                                       
---------- - ---- - - ---                                                       
         1 A blue M A N                                                         
         2 A grey X C Y                                                         
         3 B red  X B Y                                                         
         4 C blue S C N             

但是你的标准很难用简单的静态条件实现,那么你可以使用动态SQL:

SQL> create table TABLE_B1 (brand, criteria)
  2  as
  3  select 'A',          q'[color='grey' and size# in ('X','M')]' from dual union all
  4  select 'B',           q'[color = 'red']' from dual union all
  5  select 'C',          q'[size#='M']' from dual
  6  /

Table created.

SQL> update table_a set yn_buy = null;

4 rows updated.

SQL> commit;

Committed.

SQL> begin
  2    for cur in (select brand, criteria from table_b1) loop
  3      execute immediate
  4      'update table_a set yn_buy = case when '||cur.criteria||
  5      q'[ then 'Y' else 'N' end where brand = :1]' using cur.brand;
  6    end loop;
  7  end;
  8  /

PL/SQL procedure completed.

SQL> select * from table_a;

        ID B COLO S M YN_                                                       
---------- - ---- - - ---                                                       
         1 A blue M A N                                                         
         2 A grey X C Y                                                         
         3 B red  X B Y                                                         
         4 C blue S C N