向后兼容的SQL查询与桌子相对于桌子的查询,并重命名和添加列
令t
是带有列的Oracle 11G表a
,b
。 B
列后来将其重命名为c
,并添加了新的d
列。
如何开发针对两个数据库版本的SQL查询? “作品”表示:
- 返回列
a
,c
,d
- 行为
选择A,C,D。在新版本上
- 以
的方式选择A,B为C,NULL为d
在旧版本上的d,
这与那个(实际上我不想将它们混合在一起)。 基于cte 中的列选择 *
子句变化。
我能发现的唯一解决方案是基于XML和关系模型之间的转换(请参阅自我答复)。这对于我的情况不大的情况就足够了。无论如何,我很好奇是否存在某种纯粹的关系解决方案(基于连接或Oracle专有构造,例如pivot
s,model
子句等)。
还请注意,此问题不是关于从SQL查询中返回动态列数。列的数量是固定的,只有基础数据模式更改。允许使用all_tab_cols
。
Let t
be an Oracle 11g table with columns a
, b
. The b
column is later renamed to c
and new d
column is added.
How to develop an SQL query which works against both database versions? "Works" means:
- returns columns
a
,c
,d
- behaves as
select a, c, d from t
on new version - behaves as
select a, b as c, null as d from t
on old version
This is similar problem to that one (actually I didn't want to mix them into single question). The CTE-based solution used there is unfortunately not possible here because the number of columns in CTE declaration is fixed while the number of columns in select *
clause varies.
The only solution I could figure out is based on conversion between XML and relational model (see self-answer). This is sufficient for my case where table is not large. Anyway I am curious if some pure relational solution (based on joins or Oracle-proprietary constructs such as pivot
s, model
clause etc.) exists.
Please also note this question is not about returning dynamic number of columns from SQL query. The number of columns is fixed, only the underlying data schema changes. Using all_tab_cols
is allowed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
到目前为止,我唯一可以找到的解决方案是基于转换为XML,该转换可选地调整到某些归一化形式,然后转换回关系:
db小提琴在两个不同的表上演示。
The only solution I could figure out so far is based on conversion to XML which is optionally tweaked to some normalized form and then converted back to relations:
Db fiddle demonstration on two different tables.
您可以检查Colum是否存在,然后运行适当的选择。
You can check if the colum exists and then run the appropriate `SELECT