Insert row with values ​​from another row except one?

I am trying to create a stored procedure that will take a row with columns a, b, c, d where id = @id and insert a new row with the same values ​​for a, b, c except d will be different.

In this case, there are about 50 columns, although only one column should be different.

+3
source share
2 answers

Assuming what dis INT, and you want to insert, say, 15instead of what's in dbo.oldtable, then:

INSERT dbo.newtable(a,b,c,d) 
  SELECT a,b,c,d = 15
  FROM dbo.oldtable
  WHERE id = @id;

This is most likely a variable, therefore:

INSERT dbo.newtable(a,b,c,d) 
  SELECT a,b,c,d = @whatever
  FROM dbo.oldtable
  WHERE id = @id;

, , " , d"... . , . " ", , , , node :

enter image description here

d .

+5

, -

INSERT INTO tablename
(a, b, c, d)
VALUES
(SELECT col1, col2, col3, 'newcolvalue'
 FROM tablename
 WHERE id = @id);
+1

All Articles