2

Based on following TableA

Data
--------
Dummy1
Dummy2
Dummy3
.
.
DummyN

is there a way to generate sequence number while selecting rows from the table.

something like select sequence() as ID,* from Data that will give

ID  Data    
---------
1  Dummy1
2  Dummy2
3  Dummy3
....
N  DummyN

Thanks.

2 Answers 2

5

Do you want to have a column in your table that is a sequence? Use INT IDENTITY.

Do you want to add a sequential number to a SELECT statement or a view?? Use the ROW_NUMBER() OVER(ORDER BY .....) method.

SELECT 
  ROW_NUMBER() OVER (ORDER BY Data) AS 'ID',
  Data
FROM 
  dbo.YourTable
Sign up to request clarification or add additional context in comments.

Comments

0

Use a computed column:

CREATE Table MyTAble
(
   ID int identity(1,1), 
   Data varchar(20) AS 'Dummy' + ID
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.