使用外键插入数据

时间:2015-03-13 15:19:44

标签: sql sql-server-2012

我有两个表连接外键约束。每个表都有一个主键(ProductID,BaseProductID),它是一个自动增量标识列。 每个BaseProduct在导入过程中只有1个产品。

BaseProduct
- BaseProductID
- BaseProductName

Product
- ProductID
- BaseProductID
- ProductName

我尝试从具有相同表但具有不同种子值的不同数据库中将行插入到两个表中。我不想保留源数据库中的ID。

有没有办法在一个声明中这样做?

修改

选择将是

Select ProductName FROM @SourceProduct 
WHERE Not ProductName  in (
select ProductName   FROM @TargetBaseProduct
)

我尝试了Giorgi的解决方案,但是匹配

tgt.BaseProductID = src.BaseProductID
如果目标已经有一个BaseProductID = 1的条目,则

导致产生BaseProductID = 1的源行的结果。

DECLARE @SourceBaseProduct TABLE
    (
      BaseProductID INT ,
      BaseProductName NVARCHAR(MAX)
    )
DECLARE @SourceProduct TABLE
    (
      ProductID INT ,
      BaseProductID INT ,
      ProductName NVARCHAR(MAX)
    )

DECLARE @TargetBaseProduct TABLE
    (
      BaseProductID INT IDENTITY(1, 1) ,
      BaseProductName NVARCHAR(MAX)
    )
DECLARE @TargetProduct TABLE
    (
      ProductID INT IDENTITY(1, 1) ,
      BaseProductID INT ,
      ProductName NVARCHAR(MAX)
    )


INSERT  INTO @SourceBaseProduct
VALUES  ( 1, 'BaseProduct1' ),
        ( 2, 'BaseProduct2' ),
        ( 4, 'BaseProduct3' )
INSERT  INTO @SourceProduct
VALUES  ( 3, 1, 'Product1' ),
        ( 9, 2, 'Product2' ),
        ( 27, 4, 'Product3' )

INSERT  INTO @TargetBaseProduct
(

      BaseProductName 
      )
VALUES  ( 'Existing Product Base' )

INSERT  INTO @TargetProduct(
 BaseProductID  ,
      ProductName)
VALUES  (  @@IDENTITY, 'Existing Product' )

MERGE INTO @TargetBaseProduct tgt
USING
    ( SELECT    sbp.BaseProductID ,
                sbp.BaseProductName ,
                sp.ProductName
      FROM      @SourceBaseProduct sbp
                JOIN @SourceProduct sp ON sp.BaseProductID = sbp.BaseProductID
    ) AS src
ON tgt.BaseProductID = src.BaseProductID
WHEN NOT MATCHED BY TARGET THEN
    INSERT VALUES ( src.BaseProductName )
OUTPUT
    Inserted.BaseProductID ,
    src.ProductName
    INTO @TargetProduct(BaseProductID, ProductName);


SELECT  * FROM    @TargetBaseProduct
SELECT  * FROM    @TargetProduct

输出

BaseProductID   BaseProductName
1               Existing Product Base
2               BaseProduct2
3               BaseProduct3


ProductID   BaseProductID   ProductName
1           1               Existing Product
2           2               Product2
3           3               Product3

预期结果

BaseProductID   BaseProductName
1               Existing Product Base
2               BaseProduct1
3               BaseProduct2
4               BaseProduct3


ProductID   BaseProductID   ProductName
1           1               Existing Product
2           2               Product1
3           3               Product2
4           4               Product3

修改

使用

ON 1 = 0
像Giorgi建议的那样给出了正确的结果

1 个答案:

答案 0 :(得分:2)

如果我说得对,那就有MERGE

DECLARE @SourceBaseProduct TABLE
    (
      BaseProductID INT ,
      BaseProductName NVARCHAR(MAX)
    )
DECLARE @SourceProduct TABLE
    (
      ProductID INT ,
      BaseProductID INT ,
      ProductName NVARCHAR(MAX)
    )

DECLARE @TargetBaseProduct TABLE
    (
      BaseProductID INT IDENTITY(1, 1) ,
      BaseProductName NVARCHAR(MAX)
    )
DECLARE @TargetProduct TABLE
    (
      ProductID INT IDENTITY(1, 1) ,
      BaseProductID INT ,
      ProductName NVARCHAR(MAX)
    )


INSERT  INTO @SourceBaseProduct
VALUES  ( 1, 'BaseProduct1' ),
        ( 2, 'BaseProduct2' ),
        ( 4, 'BaseProduct3' )
INSERT  INTO @SourceProduct
VALUES  ( 3, 1, 'Product1' ),
        ( 9, 2, 'Product2' ),
        ( 27, 4, 'Product3' )



MERGE INTO @TargetBaseProduct tgt
USING
    ( SELECT    sbp.BaseProductID ,
                sbp.BaseProductName ,
                sp.ProductName
      FROM      @SourceBaseProduct sbp
                JOIN @SourceProduct sp ON sp.BaseProductID = sbp.BaseProductID
    ) AS src
ON tgt.BaseProductID = src.BaseProductID
WHEN NOT MATCHED BY TARGET THEN
    INSERT VALUES ( src.BaseProductName )
OUTPUT
    Inserted.BaseProductID ,
    src.ProductName
    INTO @TargetProduct(BaseProductID, ProductName);


SELECT  * FROM    @TargetBaseProduct
SELECT  * FROM    @TargetProduct

输出:

BaseProductID   BaseProductName
1               BaseProduct1
2               BaseProduct2
3               BaseProduct3


ProductID   BaseProductID   ProductName
1           1               Product1
2           2               Product2
3           3               Product3

编辑:如果您也想插入现有行,请更改

ON tgt.BaseProductID = src.BaseProductID

ON 1 = 0