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

后端 未结 11 1240
青春惊慌失措
青春惊慌失措 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:52

    If you want the actions to be more or less atomic, I would make sure to wrap them in a transaction. That way you can be sure both happened or both didn't happen as needed.

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

    You might create a View selecting the column names required by your insert statement, add an INSTEAD OF INSERT Trigger, and insert into this view.

    0 讨论(0)
  • 2020-11-22 17:03

    Before being able to do a multitable insert in Oracle, you could use a trick involving an insert into a view that had an INSTEAD OF trigger defined on it to perform the inserts. Can this be done in SQL Server?

    0 讨论(0)
  • 2020-11-22 17:06

    The following sets up the situation I had, using table variables.

    DECLARE @Object_Table TABLE
    (
        Id INT NOT NULL PRIMARY KEY
    )
    
    DECLARE @Link_Table TABLE
    (
        ObjectId INT NOT NULL,
        DataId INT NOT NULL
    )
    
    DECLARE @Data_Table TABLE
    (
        Id INT NOT NULL Identity(1,1),
        Data VARCHAR(50) NOT NULL
    )
    
    -- create two objects '1' and '2'
    INSERT INTO @Object_Table (Id) VALUES (1)
    INSERT INTO @Object_Table (Id) VALUES (2)
    
    -- create some data
    INSERT INTO @Data_Table (Data) VALUES ('Data One')
    INSERT INTO @Data_Table (Data) VALUES ('Data Two')
    
    -- link all data to first object
    INSERT INTO @Link_Table (ObjectId, DataId)
    SELECT Objects.Id, Data.Id
    FROM @Object_Table AS Objects, @Data_Table AS Data
    WHERE Objects.Id = 1
    

    Thanks to another answer that pointed me towards the OUTPUT clause I can demonstrate a solution:

    -- now I want to copy the data from from object 1 to object 2 without looping
    INSERT INTO @Data_Table (Data)
    OUTPUT 2, INSERTED.Id INTO @Link_Table (ObjectId, DataId)
    SELECT Data.Data
    FROM @Data_Table AS Data INNER JOIN @Link_Table AS Link ON Data.Id = Link.DataId
                    INNER JOIN @Object_Table AS Objects ON Link.ObjectId = Objects.Id 
    WHERE Objects.Id = 1
    

    It turns out however that it is not that simple in real life because of the following error

    the OUTPUT INTO clause cannot be on either side of a (primary key, foreign key) relationship

    I can still OUTPUT INTO a temp table and then finish with normal insert. So I can avoid my loop but I cannot avoid the temp table.

    0 讨论(0)
  • 2020-11-22 17:06
    -- ================================================
    -- Template generated from Template Explorer using:
    -- Create Procedure (New Menu).SQL
    --
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below.
    --
    -- This block of comments will not be included in
    -- the definition of the procedure.
    -- ================================================
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    ALTER PROCEDURE InsetIntoTwoTable
    
    (
    @name nvarchar(50),
    @Email nvarchar(50)
    )
    
    AS
    BEGIN
    
        SET NOCOUNT ON;
    
    
        insert into dbo.info(name) values (@name)
        insert into dbo.login(Email) values (@Email)
    END
    GO
    
    0 讨论(0)
提交回复
热议问题