0

For Example, Ihave 1 table table_source :

ID Price Date
Data 1 Price 1 1Nov2022
Data 2 Price 2 9Nov2022

I want to insert into 2 table ,

Table_Header:

Number_trans Id Date
1 Data 1 1Nov2022
2 Data 2 9Nov2022

And Table_Detail

Number_trans COA Debit Credit
1 aaaa xx 0
1 bbbb 0 xx
1 cccc yy 0
1 dddd 0 yy
2 aaaa zz 0
2 bbbb 0 zz
2 cccc qq 0
2 dddd 0 qq

don't worry about xx, yy, zz and qq --it's count with some formulas from price and other thing's.. For example : xx is price 1, yy is price 1 x 11%, zz is price 2, qq is price 2 x 11%.

How can I do insert into 1 row (in table_header) and 4 rows (in table_detail) from 1 single row in table_source? Can I do it with single sql statement?

insert sql into multi tables with one statement

  • How are getting data for columns COA, Debit & Credit in the details table? Is it from any other table? Please provide details of that table. Also please mention the database name. – sankar Nov 09 '22 at 04:08
  • COA is fix , first row ='aaaa', 2nd = 'bbbb', 3rd = 'cccc',4th='dddd Debit and credit are from some formulas For example : xx = price 1, yy = price 1 * 11%, zz = price 2, qq = price2 * 11% – Nugroho Moristianto Nov 09 '22 at 04:44

1 Answers1

0

You can't insert multiple tables in one statement but you can use one transaction in a single call

Here is the reference you may find useful

SQL Server: Is it possible to insert into two tables at the same time?