cymorg
cymorg

Reputation: 554

What is the best way to add an index column to SQL Server view?

Given a SQL Server table (Table1) and view (View1) defined as SELECT * FROM Table1

What is the best way to add a column containing an index value (e.g. 1,2,3,...n) for each row?

The result would be something like...

Where the first column in the bullet list above is calculated/computed in the view.

Upvotes: 2

Views: 2364

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269703

Probably the simplest is row_number():

select row_number() over (order by (select null)) as index_value,
       . . .
. . .

If you have a particular ordering in mind, you can use that logic instead of (select null).

Upvotes: 4

Related Questions