How do I Insert Data into Multple Tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • duvelke
    New Member
    • Feb 2012
    • 1

    How do I Insert Data into Multple Tables

    hi

    i'm new to sql
    i know some basics

    i have to insert a value in multiple tables somthing like
    Code:
    INSERT INTO table1(col1,col2)
    value(val1,val2)
    INSERT INTO table2(col1,col2)
    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
    Last edited by NeoPa; Feb 6 '12, 10:53 AM. Reason: Added mandatory [CODE] tags for you
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    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.

    Comment

    • Mihail
      Contributor
      • Apr 2011
      • 759

      #3
      I can't understand how NeoPa had forgot to put this link

      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.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        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.

        Comment

        Working...