Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

INSERT with mixed attributes

$
0
0

Hope I'm in the right Forum. I'm quite new to SQL Server and struggling with what may be a simple question.

I have a Stored Procedure that goes like this:

INSERT INTO History.dbo.Product (ProductCode, Size, Width, Height, ItemA, OldProductID)
SELECT ProductCode, Size, Width, Height, ItemA FROM  Current.dbo.Product 
WHERE ProductID = @ProductID

This query fails because OldProductID does not have a matching column name in Current.dbo.Product.

If I run the Procedure without OldProductID, it completes without error.
But I don't get the "Old ProductID"? Which is the PrimaryKey from the Current.dbo.Product Table that I want to save for future reference.

I tried experimenting with a DECLARE and a SELECT statement like this : 

DECLARE @ProductID AS Int
SELECT @OldProductID = (SELECT ProductID from Current.dbo.Product  WHERE ProductID = @ProductID)
INSERT INTO History.dbo.Product(@OldProductID)

Even if I know this is incorrect, syntaxically, my intuition tells me I'm on the right path…. 

I suspect it is possible to code the whole thing as one INSERT statement, with a mix of column values from the Current Product Table columns values combined with one Parameter but I don't mind doing two successive INSERTs if that's easier.

Any help is appreciated.


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>