Answer the question
In order to leave comments, you need to log in
Is it possible to write an sql query that, when different conditions are met, will write data to different fields?
Is it possible to write an sql query that, when different conditions are met, will write data to different fields?
For example:
case
when t1.abc = 'A' then insert t1.abc into t1 (letter) -- если один результат - вставить в одно поле
when t1.abc = '1' then insert t1.abc into t1 (num) -- если другой результат - вставить в другое поле
end
Answer the question
In order to leave comments, you need to log in
First, you should form a query that returns data of different types in different columns, and insert its results.
insert into tb
select case when cond1 then 1 else null num,
case when not cond1 then 'a' else null str
from ...
create table test_a(num number, str varchar2(100));
insert into test_a
with
generator as
(select case
when dbms_random.value() > 0.5 then dbms_random.string('p', 10)
else to_char(trunc(dbms_random.value(0, 100)))
end str
from dual
connect by level <= 50),
classificator as
(select str, case
when translate(str, chr(10)||'0123456789', chr(10)) is null then 'number'
else 'string'
end type
from generator)
select case
when type = 'number' then to_number(str)
else null
end num,
case
when type = 'string' then str
else null
end str
from classificator;
commit;
select * from test_a;
Classic:
update table
set
filed_n = case when условие1 then значение/выражение else field_n end
filed_m = case when условие2 then значение/выражение else field_m end
where ….
True Oracle, but I think there should be a similar approach.
Here is a piece from my merge:
WHEN MATCHED THEN
UPDATE
SET DATE_E = CASE WHEN (DATE_B = MERGE_SUBQUERY.DATE_B$1)
THEN MERGE_SUBQUERY.DATE_E$1
ELSE DATE_E
END
You can update both fields according to different conditions for the desired value or for yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question