avz2611 avz2611 - 1 year ago 85
SQL Question

how to insert data into table based on conditions on the other table

i have to insert values in one of my columns in table based on restrictions on another table . these are the conditions. i have two tables A and B . A has a column a , and values in column a is dependent on values in table B. lets say there are b,c,d columns in B. now conditions are these if value of d='Y' , value in a is 1 , if value in b&c are same value of a is 2 and else value is 3 . so i wrote this code . A and B have same primary key values thus same number of rows

select b ,c,d from B case when d='Y' then insert into A(a) values(1);
else case when b=c then insert into A(a) values(2); else insert into
A(a) values(3); end case end case ;

now there seems to be a syntax error , but i feel my fundamentals are weak in this topic. i think this can easily be done using cursor or loops , but data sets are huge so i don't think this would be feasible

Answer Source

Try this way:

insert into A(a)
select case 
          when d = 'Y' then 1
          when b = c then 2
          else 3
from B 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download