I need to clone table rows but am struggling with this syntax. My train of thought went like this:
DECLARE @myPk Uniqueidentifier
SET @myPK = NewID()
INSERT INTO my.table (pk, fullname, two, three)
SELECT (@myPK, "Clone of " + fullname , two, three) FROM my.table where pk= @pk
but of course the syntax is incorrect. I’ve also tried
INSERT INTO my.table (pk, fullname, two, three)
SELECT (@myPK = newId(), "Clone of " + fullname , two, three) FROM my.table where pk= @pk
(I hope you can see where I’m trying to go :-/)
Short of declaring variables for each table column (not practical due to high number of columns) or using #tempTables (not permitted in this code shop) is there a neat syntax for doing this?
Thanks for reading.
Don’t enclose your
SELECTed fields in parentheses.Try:
When you enclose everything in parentheses in a
SELECTstatement the engine tries to make that one value, which is not possible.For instance, try
SELECT (1,2,3)which will throw a syntax error also, whileSELECT 1,2,3works fine.