How to copy a line from a table to the same table and re-using the new line RecordID

时间:2017-06-12 17:07:14

标签: sql-server

I have table with a RecordID that is the PK, I'd like to copy one line, to a new line in the same table, but I'd like to use the number of this new line to create a key code I have in table.

I'm using

INSERT INTO mytable ([field 1], [field 2], [field 3], [field 4])
SELECT 'New Data for Field 1', [field 2]+[field 3] + [NewRecordID]
FROM mytable

I tried to use the Row_number function, but it always generate a 1 every time I run the query.

Any ideas of I could do to use the new auto-generated number in my query while I copy the data?

Edit: You'll find on the attached file, my table design, and some data.

The idea is to take the (Program Default) country, to copy it with to a new country. I would like to create, while copying existing data for a new country, take the new RecordID that'll be created when line will created and generate a KeyCovCountry including this new generated number.

enter image description here

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是将KeyCovCountry设为计算列。