I have a stored procedure which performs a number of select statements, and joins the results using a UNION
, eg
OPEN myItems FOR
SELECT column1, columns2 ... FROM table1 WHERE myCondition
UNION
SELECT column1, column2 ... FROM table2 WHERE myCondition
UNION
SELECT column1, column 2....
END
and so on.
I need to be able to uniquely identify the data being returned, and so I was hoping to be able to assign a row number to the rows that are returned.
I imagine there's an easy way to do this, but unfortunately I haven't been able to find it!
Thanks
SELECT ROW_NUMBER() OVER (ORDER BY column1) AS row_num, t.*
FROM (
SELECT column1, columns2 ... FROM table1 WHERE myCondition
UNION
SELECT column1, column2 ... FROM table2 WHERE myCondition
UNION
SELECT column1, column 2....
) t;
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.