Oracle:创建一个带有 Auto Increment id 列的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14229056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle: Create a View with Auto Increment id column
提问by Bishan
I have created a viewthat fills data from different tables. I used 10 select statementsand combine the results of those select statements using UNION ALL.
我创建了一个view填充来自不同表的数据。我使用10 select statements并组合了这些 select 语句的结果UNION ALL。
I want to add primary key columnto my view. because I have to create XMLfile using data in this view. so I need a primary key columnfor some process in my XMLbuilding application.
我想补充primary key column一下我的观点。因为我必须XML使用这个中的数据创建文件view。所以我需要primary key column在我的XML建筑应用程序中使用一些流程。
I have add rownumto all my select statements. But it returned duplicate ids. because rownumin each select statements start from 1.
我已经添加rownum到我所有的选择语句中。但它返回了重复的 ID。因为rownum在每个 select 语句中都是从 1 开始的。
Then I have created a sequence and tried use nextval. But I can't use sequence because my select statements has group byand order by.
然后我创建了一个序列并尝试使用nextval. 但我不能使用序列,因为我的选择语句有group by和order by。
Is there any way to do that ?
有没有办法做到这一点?
回答by ryudice
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

