简体   繁体   中英

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 (eg 1,2,3,...n) for each row?

The result would be something like...

  • 1, Product1, Price1
  • 2, Product2, Price2
  • 3, Product3, Price3

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

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM