Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query to increment by 1 1

Status
Not open for further replies.

wfairban

IS-IT--Management
Mar 13, 2006
11
US
I need to increment by 1 for each record added. There are over 60,000 records I need to add. Any suggestions?

If I do this
INSERT INTO PRODUCTSBYCATEGORY(productid, categoryid, sequence)
(Select productid, 8, (select (max(sequence) + 1)
from productsbycategory
where categoryid = 8)
from product
where (partnumber not like 'ITE%') and
(partnumber not like 'NPO%') and
(partnumber not like 'ENG%')

It will set the sequence to 2, but then it set all records to 2, instead of 3, 4, 5, etc.
 
Could you elaborate? I would normally do this in Oracle with a cursor.
 
If you create an Identity column on the table, then each time you insert a row, the Identity column will increment by the seed you specify, the default is one. When you create your insert statment, just don't specify that column.
Example:
Once you create an Identity column, lest call is sequence as you have called it.. then change your Insert to this:


INSERT INTO PRODUCTSBYCATEGORY(productid, categoryid)

The sequence column will increment automatically.
 
And when I already have one identity in my table! SQL Server doesn't accepts two identitys in same table...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top