Dynamic numbering of individual sql lines in select statement

How would I dynamically count the rows in a query like this:

Select distinct name from @table where %rules 

When I add ROW_NUMBER () OVER (), I lose my individual property and return each item in the table with a unique row number.

 Select distinct ROW_NUMBER OVER(order by name), name from @table where %rules 

I do not want to create a temporary table, otherwise I would make a primary key for the temporary table and insert rows this way.

Thanks in advance!

+4
source share
1 answer

Use this.

 select ROW_NUMBER() OVER(order by name), * from (Select distinct name from @table where %rules) as mytable 
+4
source

All Articles