Home > Enterprise >  Inserting data from one table to another table values based on column condition
Inserting data from one table to another table values based on column condition

Time:06-21

I want to know if there is a way to achieve this. I have two table, I want to move data from one table to another table.

TableA:
ID  Name  Option
----------------------
1  First   Add into box
2  Second  Don't Add
3  Third   Add into box

TableB
ID Name    Option          Status
--------------------------------
1  First   Add into box    Approved 
2  Second  Don't Add       Reject
3  Third   Add into box    Approved 

I want to insert data from TableA to tableB but tableB have one more column, where the value of it depends on option column data. If column value is Add into box then it should be inserted as approved into TableB else Reject has to be inserted.

CodePudding user response:

If you are using MSSQL, the below query would work.

Insert into TableB (Id,Name, Option, Status) 
select Id,Name, Option, CASE when Option = 'Add into box'  then 'Approved' ELSE 'Rejected' END
From TableA
  •  Tags:  
  • sql
  • Related