Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi I come from Oracle and this is done with the CreateSequence function. I'm still a noob SQL SERVER, i'm trying to make an order sequence number in SQL SERVER 2008.
I have a query similar to:
SELECT NAME,
 ROW_NUMBER() OVER (ORDER BY NAMEID) + @var
FROM EMPLOYEE


After the query i want to
Update Table OrderNumber with last ROW_NUMBER() from query


RESULTS

JOHN    1
SCOTT   2
SOPHIA  3   

and i have another table 'OrderNumber' with one value which will be the last ROW_NUMBER() from the query... Everytime i run the query i want to pass this value to the @var, default number its 1 to start the sequence after that i want to update this table with the last value.

OrderNumber (default run)
1

After the run
OrderNumber (first run)
3

Etc
OrderNumber (etc)
X
Posted

SQL server has identity column which can be used for auto increment columns.

check the following tutorial for same:

http://www.sqlteam.com/article/understanding-identity-columns[^]
 
Share this answer
 
You can get the last # by the following query...

SQL
WITH CTE (Name, Position) AS (
SELECT NAME,
 ROW_NUMBER() OVER (ORDER BY NAMEID)
FROM EMPLOYEE
)
SELECT MAX(Position) FROM CTE -- Last number



If NameID is an integer and an auto-increment field then you can simply use the SELECT MAX(NameID) FORM EMPLOYEE to get the last row....

Please let me know if this isn't resolve your problem

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900