Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

SQL statements to insert one record to multiply tables

$
0
0

I have a form which contains a lot of information will be inserted into several related tables:

table1:  table1ID, table1col2, table1col2, table2col3, c_id.
table2: table1col3, table2col2
table3: table1ID, table1col2, table3col3
table4: table1ID, table1col2, table4col3
table5: table1ID, table5col2
table6: table5col2, table6col2, table6col3
table7: c_id, table7col2, c
table8: table1col2, table1ID.

the relationship: 
table1 and table7 related by c_id.
table1 and table2 is related by table1col3.
table1 and tabl33 is related by table1ID and table1col2
table1 and table4 is related by table1ID and table1col2
table1 and table5 is related by table1ID
table5 and table6 is related by table5col2.
in table5: table1ID and table5col2 has relationship 1:m
in table8: table1col2 and table1ID has relationship: m:1

Now I want to insert a record into the tables:
insert:  table1ID, table1col2, table1col2, table2col3 into table1
insert: table7col2, c  and it will generate c_id and this c_id will also be the same value as table1 c_id field in the record I just inserted.
insert:  table1col3 into table2 only when it does have this number
insert: table6col2, table6col3 only when it does not have this values(firstN, lastN) and it will automatically created table5col2 in table6 and this field (table5col2) will also be updated in table5 after insert table1ID in the table5.

How to write these statements? 

Thanks.


Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>