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

后端 未结 11 1239
青春惊慌失措
青春惊慌失措 2020-11-22 16:22

My database contains three tables called Object_Table, Data_Table and Link_Table. The link table just contains two columns, the identi

相关标签:
11条回答
  • 2020-11-22 16:41

    It sounds like the Link table captures the many:many relationship between the Object table and Data table.

    My suggestion is to use a stored procedure to manage the transactions. When you want to insert to the Object or Data table perform your inserts, get the new IDs and insert them to the Link table.

    This allows all of your logic to remain encapsulated in one easy to call sproc.

    0 讨论(0)
  • 2020-11-22 16:42

    //if you want to insert the same as first table

    $qry = "INSERT INTO table (one, two, three) VALUES('$one','$two','$three')";
    
    $result = @mysql_query($qry);
    
    $qry2 = "INSERT INTO table2 (one,two, three) VVALUES('$one','$two','$three')";
    
    $result = @mysql_query($qry2);
    

    //or if you want to insert certain parts of table one

     $qry = "INSERT INTO table (one, two, three) VALUES('$one','$two','$three')";
    
    
      $result = @mysql_query($qry);
    
     $qry2 = "INSERT INTO table2 (two) VALUES('$two')";
    
     $result = @mysql_query($qry2);
    

    //i know it looks too good to be right, but it works and you can keep adding query's just change the

        "$qry"-number and number in @mysql_query($qry"")
    

    I have 17 tables this has worked in.

    0 讨论(0)
  • 2020-11-22 16:43

    You still need two INSERT statements, but it sounds like you want to get the IDENTITY from the first insert and use it in the second, in which case, you might want to look into OUTPUT or OUTPUT INTO: http://msdn.microsoft.com/en-us/library/ms177564.aspx

    0 讨论(0)
  • 2020-11-22 16:46

    I want to stress on using

    SET XACT_ABORT ON;
    

    for the MSSQL transaction with multiple sql statements.

    See: https://msdn.microsoft.com/en-us/library/ms188792.aspx They provide a very good example.

    So, the final code should look like the following:

    SET XACT_ABORT ON;
    
    BEGIN TRANSACTION
       DECLARE @DataID int;
       INSERT INTO DataTable (Column1 ...) VALUES (....);
       SELECT @DataID = scope_identity();
       INSERT INTO LinkTable VALUES (@ObjectID, @DataID);
    COMMIT
    
    0 讨论(0)
  • 2020-11-22 16:48

    Insert can only operate on one table at a time. Multiple Inserts have to have multiple statements.

    I don't know that you need to do the looping through a table variable - can't you just use a mass insert into one table, then the mass insert into the other?

    By the way - I am guessing you mean copy the data from Object_Table; otherwise the question does not make sense.

    0 讨论(0)
  • 2020-11-22 16:51

    In one statement: No.

    In one transaction: Yes

    BEGIN TRANSACTION
       DECLARE @DataID int;
       INSERT INTO DataTable (Column1 ...) VALUES (....);
       SELECT @DataID = scope_identity();
       INSERT INTO LinkTable VALUES (@ObjectID, @DataID);
    COMMIT
    

    The good news is that the above code is also guaranteed to be atomic, and can be sent to the server from a client application with one sql string in a single function call as if it were one statement. You could also apply a trigger to one table to get the effect of a single insert. However, it's ultimately still two statements and you probably don't want to run the trigger for every insert.

    0 讨论(0)
提交回复
热议问题