日期:2014-05-17  浏览次数:20659 次

ORACLE语法能不实现这样的功能
能否实现这样的功能:

select cola from table where flag='a' if (存在字段colb) and colb='b'

就思就是如果存在字段colb,则语句相当于是

select cola from table where flag='a' and colb='b'

如果不存在colb,则语句相当于是:

select cola from table where flag='a'

------解决方案--------------------
SQL code

--可以根据此判断列是否存在后,动态执行语句
select count(*) from user_tab_columns 
    where table_name=upper('table') and column_name=upper('colb');

------解决方案--------------------
可以是可以,就是麻烦点。
写个过程,首先打开个游标
select column_name from user_tab_columns where table_name='YOURTABLENAME'
然后检查COLB是否在返回的结果集中,
然后IF ... ELSE ...END IF。
------解决方案--------------------
select count(*) from user_tab_columns 
where table_name=upper('table') and column_name=upper('colb');