Oracle: Create a View with Auto Increment id column

泪湿孤枕 提交于 2019-12-18 22:38:09

问题


I have created a view that fills data from different tables. I used 10 select statements and combine the results of those select statements using UNION ALL.

I want to add primary key column to my view. because I have to create XML file using data in this view. so I need a primary key column for some process in my XML building application.

I have add rownum to all my select statements. But it returned duplicate ids. because rownum in each select statements start from 1.

Then I have created a sequence and tried use nextval . But I can't use sequence because my select statements has group by and order by.

Is there any way to do that ?


回答1:


You can do a select over the union, for example:

SELECT rownum(),*
FROM (SELECT * FROM tableA UNION ALL SELECT * FROM tableB)

UPDATED

SELECT rownum, t.*
FROM (SELECT * FROM tableA UNION ALL SELECT * FROM tableB) t


来源:https://stackoverflow.com/questions/14229056/oracle-create-a-view-with-auto-increment-id-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!