By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,510 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,510 IT Pros & Developers. It's quick & easy.

How do I Insert Data into Multple Tables

P: 1
hi

i'm new to sql
i know some basics

i have to insert a value in multiple tables somthing like
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO table1(col1,col2)
  2. value(val1,val2)
  3. INSERT INTO table2(col1,col2)
  4. value(val1,val2)
and i now need to insert an id from table1 into table 2
the id in table 1 is an autonumber but not in table 2 int table

hopefuly its explaind enough

how do i do this in acces and sql server 2008
Feb 6 '12 #1
Share this Question
Share on Google+
3 Replies


NeoPa
Expert Mod 15k+
P: 31,186
The answer is fundamentally the same for SQL Server (T-SQL) as it is for Access. You need to be able to identify uniquely (and you don't give any such details in your question) the records that you've added with the first INSERT INTO SQL, then use this reference to filter these same records from the first table and append them into the second table.

INSERT INTO has an alternative format where data is provided from other tables rather than a value list, as in your example.
Feb 6 '12 #2

100+
P: 759
I can't understand how NeoPa had forgot to put this link
http://bytes.com/topic/access/insigh...ble-structures
for you. (you know me, NeoPa: I can't be all time very seriously :) )
That because I can't see any reason to store the same values in two different tables. Maybe I am wrong this time, but I don't think so.
Feb 7 '12 #3

NeoPa
Expert Mod 15k+
P: 31,186
It's worth mentioning Mihail, certainly. There are situations where this could be fully conformant with Normalisation theory though. After all, we have so little information to work on so we must be even more careful about making assumptions. Consider the possibility that all the data fields are FK links to other tables for instance.

Nevertheless, we can no more assume that, than that the data fields pertain to standard data such as names or other basic data, so the Normalisation point is worth including.
Feb 7 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.