Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to insert the rows from table2 to table1 using where clause on field

eg.

table1 columns

date voucherno cashbalance bankbalance narration



table2 columns

date voucherno ledgerhead balance narration


i would like to insert the date on date, voucherno on voucherno, if ledgerhead name is cash then insert the balance to cashbalance else 0,if ledgerhead name is bank then insert the balance to bankbalance else 0, and narration to narration from table 2 to table1.

Answers appreciated......
Posted

you can use SQL INSERT INTO SELECT [^]statement
Example:
SQL
INSERT INTO Customers (CustomerName, Country)
SELECT SupplierName, Country FROM Suppliers
WHERE Country='Germany';
 
Share this answer
 
You may try with:
SQL
INSERT INTO table1 (date, voucherno, cashbalance, bankbalance, narration)
SELECT date,voucherno
,Case ledgerhead when 'cash' then balance else 0 end as cashbalance
,Case ledgerhead when 'bank' then balance else 0 end as bankbalance
,narration FROM table2
 
Share this answer
 
Comments
Nirav Prabtani 16-Jun-14 8:59am    
e ha ho

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900