Pergunta

Could you help me building a query as below?

TABLE_A

id   brand color              size       model      yn_buy
1    A          blue             M           A             -
2    A         grey              X          C              -
3    B         red               X           B               -
4    C        blue              S           C             -

TABLE_B

brand   critery
A          color=grey and size=X
B          color=red
C          size=M


I want to build a query to update table A and the answer should be:

TABLE_A

id   brand color              size       model      yn_buy
1    A          blue             M           A             N
2    A         grey              X          C              Y
3    B         red               X           B               Y
4    C        blue              S           C             N


As you can see the data on the column "critery" should be the decisor to buy or not

I'd like to use a single merge, like the following

merge into TABLE_A a
using
(
    select id, brand, CASE WHEN critery THEN '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)
WHEN MATCHED THEN UPDATE set a.yn_buy = b.yn_buy

Is it possible to do something like this? maybe using execute immediate, some kind of bind...?

thank you

Foi útil?

Solução

If I don't miss something in your particular case you don't need dynamic SQL - you can simply change TABLE_B structure and use static MERGE (because I don't know how complex your criteria can be this is just an illustration):

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             

But of your criteria are difficult enough to be implemented using simple static condition, then you can use dynamic 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           
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top