基於集合的插入到具有 1 到 0-1 關係的兩個表中 (Set based insert into two tables with 1 to 0-1 relation)


問題描述

基於集合的插入到具有 1 到 0-1 關係的兩個表中 (Set based insert into two tables with 1 to 0-1 relation)

I have two tables, the first has a primary key that is an identity, the second has a primary key that is not, but that key has a foreign key constraint back to the first table's primary key.

If I am inserting one record at a time I can use the Scope_Identity to get the value for the pk just inserted in table 1 that I want to insert into the second table.

My problem is I have many records coming from selects I want to insert in both tables, I've not been able to think of a set based way to do these inserts.

My current solution is to use a cursor, insert in the first table, get key using scope_identity, insert into second table, repeat.

Am I missing a non-cursor solution?


參考解法

方法 1:

Yes, Look up the output clause in Books online.

方法 2:

I had this problem just this week: someone had introduced a table with a meaningless surrogate key into the schema where naturally keys are used. No doubt I'll fix this soon :) until then, I'm working around it by creating a table of data to INSERT from: this could be a permanent or temporary base table or a derived table (see below), which should suit your desire for a set-based solution anyhow. Use a join between this table and the table with the IDENTITY column on the natural key to find out the auto-generated values. Here's a brief example:

CREATE TABLE Test1 
(
 surrogate_key INTEGER IDENTITY NOT NULL UNIQUE, 
 natural_key CHAR(10) NOT NULL CHECK (natural_key NOT LIKE '%[^0-9]%') UNIQUE
);

CREATE TABLE Test2
(
 surrogate_key INTEGER NOT NULL UNIQUE
    REFERENCES Test1 (surrogate_key), 
 data_col INTEGER NOT NULL
);

INSERT INTO Test1 (natural_key)
SELECT DT1.natural_key
  FROM (
        SELECT '0000000001', 22
        UNION ALL 
        SELECT '0000000002', 55
        UNION ALL 
        SELECT '0000000003', 99
       ) AS DT1 (natural_key, data_col);

INSERT INTO Test2 (surrogate_key, data_col)
SELECT T1.surrogate_key, DT1.natural_key
  FROM (
        SELECT '0000000001', 22
        UNION ALL 
        SELECT '0000000002', 55
        UNION ALL 
        SELECT '0000000003', 99
       ) AS DT1 (natural_key, data_col)
       INNER JOIN Test1 AS T1
          ON T1.natural_key = DT1.natural_key;

(by automaticHLGEMonedaywhen)

參考文件

  1. Set based insert into two tables with 1 to 0-1 relation (CC BY-SA 3.0/4.0)

#identity #sql-server-2008 #SQL #Insert






相關問題

基於集合的插入到具有 1 到 0-1 關係的兩個表中 (Set based insert into two tables with 1 to 0-1 relation)

在客戶端打開網頁的硬件識別 (hardware identification to be open a web page on cliet side)

如何生成矩陣 M,其協方差為單位矩陣 (How can I generate a matrix M, where its covariance is the Identity matrix)

在 SQL Server CE 中使用自動增量時出現主鍵錯誤 (Primary key error in using auto increment in SQL Server CE)

VBA中的兩個對象何時相同? (When are two objects the same in VBA?)

在SQL Server中,執行插入操作時是否可以獲得記錄的“ id”? (In SQL Server is it possible to get "id" of a record when Insert is executed?)

sql server 2005:使用@@identity 安全嗎? (sql server 2005:is it safe to use @@identity?)

無法對錶執行創建、更新或刪除操作,因為它沒有主鍵 (Can't perform Create, Update or Delete operations on Table because it has no primary key)

如何將數據傳遞到具有標識 aspnetuserTable 外鍵的自定義表中 (How to pass data into a custom table having foreign key of identity aspnetuserTable)

可以在python中對包含-5到256之間的整數的變量強制唯一的ID嗎? (Possible to force unique IDs to variables containing integers between -5 and 256 in python?)

ASP.NET Identity CreateAsync 返回錯誤“名稱不能為空或為空。” (ASP.NET Identity CreateAsync returning Error "Name cannot be null or empty.")

嘗試激活 Identity.IdentityUserManager 時無法解析 Identity.IdentityUserStore 類型的服務 (Unable to resolve service for type Identity.IdentityUserStore while attempting to activate Identity.IdentityUserManager)







留言討論