1

I have the following query:

INSERT INTO dbo.Products 
        ( Code , 
          ProducerCode , 
          CustomerId , 
          EanCode , 
          ChangedDateTime , 
          ChangedById , 
          Deleted  
        ) 
SELECT  Code , 
        ProducerCode , 
        CustomerId , 
        EanCode , 
        GETDATE(), 
        GETDATE(),
        0 
FROM dbo.BulkProducts 
WHERE ProductId is NULL AND BulkProducts.BulkId = @BulkId

The Products table has an IDENTITY ID column, which is assigned automatically on insert. What I want to achieve is, after the insertion in Products, have the assigned ID of the product in the row from BulkProducts. I have read a bit about@@IDENTITY, IDENT_CURRENT and SCOPE_IDENTITY but I cannot seem to get it to work for my example. Any help appreciated. I am using SQL Server 2012.

iuliu.net
  • 6,666
  • 6
  • 46
  • 69

2 Answers2

0

You can achieve it using Output clause, like this -

DECLARE @OutProduct TABLE 
(
    ProductID INT,
    Code INT
)

INSERT INTO dbo.Products 
        ( Code , 
          ProducerCode , 
          CustomerId , 
          EanCode , 
          ChangedDateTime , 
          ChangedById , 
          Deleted  
        ) 
OUTPUT INSERTED.ID, INSERTED.Code INTO @OutProduct (ProductID, Code)
SELECT  Code , 
        ProducerCode , 
        CustomerId , 
        EanCode , 
        GETDATE(), 
        GETDATE(),
        0 
FROM dbo.BulkProducts 
WHERE ProductId is NULL AND BulkProducts.BulkId = @BulkId

So Now all your inserted data are in @OutProduct table. Now you can update your BulkProducts table using that temp table like below.

UPDATE bprod 
    SET productID = tmp.ProductID
FROM BulkProducts bprod
INNER JOIN @OutProduct tmp  
   ON bprod.Code = tmp.Code
WHERE bprod.ProductId is NULL AND bprod.BulkId = @BulkId
Krishnraj Rana
  • 6,516
  • 2
  • 29
  • 36
  • The `OUTPUT INSERTED.ID INTO @OutProduct (ProductID, BulkId)` will insert both the current just-inserted-ID and BulkId? – iuliu.net May 19 '16 at 13:26
  • Ok but as you can see the `BulkId` field isn't getting inserted, so I don't think is accessible from `INSERTED`.. I've tried `OUTPUT INSERTED.ID, BulkProducts.BulkId INTO @OutProduct (ProductID, BulkId)` but it doesn't seem to work. – iuliu.net May 19 '16 at 13:40
  • Ok, you changed it with `Code`, but I still need the `BulkId` :D – iuliu.net May 19 '16 at 13:41
  • @iuliu.net: I understand. See my update query again at the last. I have added `Where` clause. I think now no need to BulkID – Krishnraj Rana May 19 '16 at 13:44
0

Apparently this cannot be solved without a MERGE. The final solution looks like this:

MERGE INTO dbo.Products
USING dbo.BulkProducts AS src
    ON 1 = 0 -- Never match
WHEN NOT MATCHED THEN
    INSERT(EanCode, ChangedDateTime, ChangedById, Deleted)
    VALUES(src.EanCode, GETDATE(), GETDATE(), 0)
OUTPUT
    inserted.Id,
    src.Id
INTO @OutProduct;

And like this, my @OutProduct table contains tuples of BulkProductId and ProductId which I can then use to update my BulkProducts table.

iuliu.net
  • 6,666
  • 6
  • 46
  • 69