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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment each record returned by 1

Status
Not open for further replies.

DH

Programmer
Dec 8, 2000
168
0
0
Hello,

I am writing a query to return results to use in a export file. One of the columns is I need to manually create is 'assetID' and per the specs, I need to:

"Starting with the value of 1, increment 'assetID' for each record created.

For simple example the following:

Code:
USE Northwind
SELECT FirstName
FROM Employees
WHERE title = 'Sales Representative'

Returns the following results:

FirstName
Nancy
Janet
Margaret
Michael
Robert
Anne

How can I insert a column named 'AssetID' to the left of FirstName and then increment by 1 for each record returned?

AssetID FirstName
1 Nancy
2 Janet
3 Margaret
4 Michael
5 Robert
6 Anne

Thank you,

Dave





 
The simplest way is to create a table variable that has a feld defined as integer identity.

Code:
USE Northwind

SET NOCOUNT ON

declare @Temp Table(AssetId Integer Identity(1,1), FirstName VarChar(50)

Insert into @temp(FirstName)
SELECT FirstName
FROM Employees
WHERE title = 'Sales Representative'

Select * from @temp

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top