question

Souchie avatar image
Souchie asked

Insert multiple rows and update Primary key

Hi All I have multiple rows to insert in the same table all the data is the same except for the Pk field this needs to be incremented by one as this is then needed to be inserted into another table along with an additional feild. In this instance there is a fixed number of rows that I can retrive using a select statement. thanks in advance for any help reagrds Mratin
insert
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
I'm not completely sure what you're going for here, but have you looked at the [ROW_NUMBER][1] function? That will give you the ability to SELECT data and simultaneously create numbers. Or, do you need to create an [IDENTITY][2] column in your table? That increments automatically as you insert data. If you use the [OUTPUT][3] clause you can capture all the values as they get created. [1]: http://msdn.microsoft.com/en-us/library/ms186734.aspx [2]: http://msdn.microsoft.com/en-us/library/ms186775.aspx [3]: http://msdn.microsoft.com/en-us/library/ms177564.aspx
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Souchie avatar image Souchie commented ·
I need to insert 88 rows all have the same information as thery are concessionsn and each task needs a seperate concession the PK is the identifer for the table that joins the concession iformation and task information so I am thinking along thje lines of : Insert into Table A, (col b, col c, Col d .... Col N) Values (a, b, c, d.....z) With a loop or counter of some sort to use the PK and increment by 1 each time into Col A Regards Martin
0 Likes 0 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
OK, then you should modify TableA to use an IDENTITY column. It will do that work for you.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.