如何将具有多对多关系的多行从 'Table1' 插入到 'Table1'?

How do I insert multiple rows with many to many relations into 'Table1' from 'Table1'?

我正在尝试复制现有的飞船,包括其子 table 的所有内容(因为缺少更好的术语)及其关系。到目前为止,除了具有多对多关系的船的 table 之外,我已经完成了大部分复制。我目前的情况举例如下:

我有两个单独的 table 和第三个 table 连接它们:

设施

| FacilitiesId | ShipId |   fName    |
| ------------ | ------ | ---------- |
| 1            | 1      | Facility 1 |
| 2            | 1      | Facility 2 |
| 3            | 1      | Facility 3 |

甲板

| DeckId | ShipId | DeckLevel|
| ------ | ------ | -------- |
| 1      | 1      |     1    |
| 2      | 1      |     2    |
| 3      | 1      |     3    |

FacilitiesToDeck

| FacilitiesToDeckId | ShipId | FacilitiesId | DeckId |
| ------------------ | ------ | ------------ | ------ |
| 1                  | 1      | 1            | 1      |
| 2                  | 1      | 2            | 1      |
| 3                  | 1      | 3            | 2      |
| 4                  | 1      | 4            | 2      |
| 5                  | 1      | 5            | 3      |
| 6                  | 1      | 5            | 4      |

正如您在上面看到的,FacilitiesToDeck 可能有多行具有相同的 DeckId,因为在同一层甲板上可以有多个设施。 FacilitiesId 也是如此,因为可以有跨越多层甲板的设施。

现在,当我复制一艘船及其子实体时,我希望上面的 table 看起来像这样:

设施

| FacilitiesId | ShipId |   fName    |
| ------------ | ------ | ---------- |
| 1            | 1      | Facility 1 |
| 2            | 1      | Facility 2 |
| 3            | 1      | Facility 3 |
| 4            | 2      | Facility 1 |
| 5            | 2      | Facility 2 |
| 6            | 2      | Facility 3 |

甲板

| DeckId | ShipId | DeckLevel|
| ------ | ------ | -------- |
| 1      | 1      |     1    |
| 2      | 1      |     2    |
| 3      | 1      |     3    |
| 4      | 2      |     1    |
| 5      | 2      |     2    |
| 6      | 2      |     3    |

FacilitiesToDeck

| FacilitiesToDeckId | ShipId | FacilitiesId | DeckId |
| ------------------ | ------ | ------------ | ------ |
| 1                  | 1      | 1            | 1      |
| 2                  | 1      | 2            | 1      |
| 3                  | 1      | 3            | 2      |
| 4                  | 1      | 4            | 2      |
| 5                  | 1      | 5            | 3      |
| 6                  | 1      | 5            | 4      |
| 7                  | 2      | 6            | 5      |
| 8                  | 2      | 7            | 5      |
| 9                  | 2      | 8            | 6      |
| 10                 | 2      | 9            | 6      |
| 11                 | 2      | 10           | 7      |
| 12                 | 2      | 10           | 8      |

我的存储过程旨在将 FacilitiesToDeck 中的多个新数据行插入 FacilitiesToDeck,但是 ShipIdFacilitiesIdDeckId是我之前在存储过程中为 FacilitiesDeck 创建的新 ID 值。 但是,我没有看到(或在互联网上找到)关于如何执行此操作的解决方案,因为插入 FacilitiesToDeck 的每个新行的每个值都是不同的,似乎没有相关性(至少对我和我的同行)。

以下是我当前存储过程的片段:

ALTER PROCEDURE [dbo].[CopyShip]
    (@ShipId int,
     @ShipName nvarchar(150),
     @ShipCode nvarchar(8))
AS
BEGIN
    SET NOCOUNT ON

    DECLARE @CruiselineId int;

    SET @CruiselineId = (SELECT [CruiselineId] FROM [dbo].Ships 
                         WHERE ShipId = @ShipId);

---- CREATING COPY OF Ships

    INSERT INTO Ships (
      [CruiselineId], [Name], [ShipCode], [ClassId],
      [guestNumber], [staffNumber], [creationYear],
      [weight], [length], [passagerDeck], [handicapCabins], [nationality]
    )
    SELECT
      [CruiselineId], @ShipName, @ShipCode, [ClassId],
      [guestNumber], [staffNumber], [creationYear],
      [weight], [length], [passagerDeck], [handicapCabins], [nationality]
    FROM [dbo].Ships 
    WHERE ShipId = @ShipId;

    DECLARE @NewShipId int = SCOPE_IDENTITY();

---- CREATING COPY OF Deck

     INSERT INTO Deck (
     ShipId, DeckLevel, [Name],
     NameSE, NameNO
     )
     SELECT
     @NewShipId, DeckLevel, [Name],
     NameSE, NameNO
     FROM dbo.Deck dd
     WHERE ShipId = @ShipId;

---- CREATING COPY OF Facilities

INSERT INTO Facilities (
    ShipId, FacilityCategoryId, [Name],
    [Type], MinAge, MaxAge, PriceRange,
    [Description], DescriptionNo, DescriptionSe,
    NameSE, NameNO
    )
    SELECT
    @NewShipId, FacilityCategoryId, [Name],
    [Type], MinAge, MaxAge, PriceRange,
    [Description], DescriptionNo, DescriptionSe,
    NameSE, NameNO
    FROM dbo.Facilities ff
    WHERE ShipId = @ShipId;

---- CREATING COPY OF FacilitiesToDeck

    INSERT INTO FacilitiesToDeck (
      DeckId,
      FacilitiesId,
      ShipId
    )
    SELECT fd.DeckId,
           bse.FacilitiesId,
           @NewShipId 
    FROM FacilitiesToDeck fd
    JOIN dbo.Facilities f on fd.FacilitiesId = f.FacilitiesId 
    JOIN dbo.Facilities bse on f.[Name] = bse.[Name] and bse.ShipId = @NewShipId
    JOIN dbo.Deck d on d.ShipId = @NewShipId
    WHERE fd.ShipId = @ShipId;

END

这是我当前代码的结果:

FacilitiesToDeck

| FacilitiesToDeckId | ShipId | FacilitiesId | DeckId |
| ------------------ | ------ | ------------ | ------ |
| 1                  | 1      | 1            | 1      |
| 2                  | 1      | 2            | 1      |
| 3                  | 1      | 3            | 2      |
| 4                  | 1      | 4            | 2      |
| 5                  | 1      | 5            | 3      |
| 6                  | 1      | 5            | 4      |
| 7                  | 2      | 6            | 5      |
| 8                  | 2      | 6            | 6      |
| 9                  | 2      | 6            | 7      |
| 10                 | 2      | 7            | 5      |
| 11                 | 2      | 7            | 6      |
| 12                 | 2      | 7            | 7      |
| 13                 | 2      | 8            | 5      |
| 14                 | 2      | 8            | 6      |
| 15                 | 2      | 8            | 7      |
...
...

正如您希望在上面所说的那样,我当前的程序最终是在每个新甲板上插入所有新设施,而不是将新设施与预期的甲板一起插入。当我检查我的代码时,这个输出现在很有意义,但我仍然无法想出一个解决方案来做我打算做的事情。我希望新插入的行 FacilitiesToDeck 具有来自 FacilitiesDeck.

的新 ID

有人知道吗?

编辑: 添加了我自己的尝试及其结果输出。

这是一个例子:

drop table #Facilities;
create table #Facilities
( FacilitiesId int identity(1,1)
, ShipId integer
, fName varchar(10))
;

set identity_insert #Facilities on;
insert into #Facilities( FacilitiesId , ShipId ,   fName   )
 values
 ( 1,            1 ,     'Facility 1' )
,( 2,            1 ,     'Facility 2' )
,( 3,            1 ,     'Facility 3' )
,( 4,            1 ,     'Facility 4' )
,( 5,            1 ,     'Facility 4' )
;

set identity_insert #Facilities off;

drop table #Deck;
create table #Deck 
(DeckId int identity(1,1)
,ShipId int
,DeckLevel int)
;

set identity_insert #Deck on;
insert into #Deck(DeckId , ShipId , DeckLevel)
values
( 1      , 1      ,     1    )
,( 2      , 1      ,     2    )
,( 3      , 1      ,     3    )
,( 4      , 1      ,     4    )
;
set identity_insert #Deck off;


drop table #FacilitiesToDeck;
create table #FacilitiesToDeck
( FacilitiesToDeckId int identity (1,1)
 , ShipId int
 , FacilitiesId int, 
 DeckId int);

set identity_insert #FacilitiesToDeck on;
insert into #FacilitiesToDeck ( FacilitiesToDeckId, ShipId , FacilitiesId , DeckId )
values
  (1                  , 1      , 1            , 1      )
, (2                  , 1      , 2            , 1      )
, (3                  , 1      , 3            , 2      )
, (4                  , 1      , 4            , 2      )
, (5                  , 1      , 5            , 3      )
, (6                  , 1      , 5            , 4      )

set identity_insert #FacilitiesToDeck off;

------------------ Start 
-- These are hard coded here, but you can use the same insert...output technique to get the new Ship Id
declare @OldShipId int=1,
        @NewShipId int=2
;
--- These table variables will hold old-new id maps
declare 
  @FacilitiesIdMap table 
    (OldFacilitiesId int,
     NewFacilitiesId int);
declare 
  @DeckIdMap table 
    (OldDeckId int,
     NewDeckId int);

declare @ThisId int;

begin tran
    -- Copy facilities, and keep track of old-to-new ids
    select @ThisId=min(FacilitiesId) from #Facilities where ShipId=@OldShipId;
    while @ThisId is not null begin
        insert into #Facilities (ShipId, fName) 
        output @ThisId, inserted.FacilitiesId into @FacilitiesIdMap
        select @NewShipId, fName
        from #Facilities
        where FacilitiesId=@ThisId;
        -- Get the next id
        select @ThisId=min(FacilitiesId) from #Facilities where ShipId=@OldShipId and FacilitiesId > @ThisId;
    end;

    -- Copy decks, and keep track of old-to-new ids
    select @ThisId=min(DeckId) from #Deck where ShipId=@OldShipId;
    while @ThisId is not null begin--
        insert into #Deck (ShipId, DeckLevel) 
        output @ThisId, inserted.DeckId into @DeckIdMap
        select @NewShipId, DeckLevel
        from #Deck
        where DeckId=@ThisId;
        -- Get the next id
        select @ThisId=min(DeckId) from #Deck where ShipId=@OldShipId and DeckId > @ThisId;
    end;

    insert into #FacilitiesToDeck ( ShipId , FacilitiesId , DeckId )
    select @NewShipId, F.NewFacilitiesId, D.NewDeckId
    from @FacilitiesIdMap F
         inner join
         #FacilitiesToDeck F2D
         on F.OldFacilitiesId=F2D.FacilitiesId
         inner join
         @DeckIdMap D
         on D.OldDeckId=F2D.DeckId
    where F2D.ShipId=@OldShipId

    select * from #Facilities
    select * from #Deck
    select * from #FacilitiesToDeck

commit
------ End

我并不声称它是性能最好或最好的解决方案,但如果您的 xxxId 列是标识列,这将起作用。 我没有添加任何错误检查;理想情况下,你应该有一个 try-catch 块并回滚所有插入,如果有任何失败。 标识插入用于模拟您的示例数据,同时具有标识列。