Alecu
Alecu

Reputation: 2708

Variable in select statement in sql server with increment

I am trying to execute a series of selects in a statement in sql server with a union between them. Now I want in a select statement to include a variable from the t-sql code and increment it with each new entry. How can I do this?

Upvotes: 0

Views: 84

Answers (1)

Madhivanan
Madhivanan

Reputation: 13700

You can use ROW_NUMBER() function instead of a variable

select row_number() over (order by keycol) as row_num, * from
(
Your UNION queries here
) as t

Upvotes: 1

Related Questions