楼上的不行啊 比如 select aaa as test ,bbb from table 我想如果aaa字段不存在 test 值都为空,请问能实现吗
if exists(select * from syscolumns where id=object_id('tablename') and name='test') select test from table else select test='',bbb from table
if exists(select * from syscolumns where id=object_id('tablename') and name='test') select test from table else select 'test' test,bbb from table
sorry,看错需求了select nvl2((select 1 from user_tab_columns where TABLE_NAME='表名' and COLUMN_NAME='test'), test, 'aaa') From MYTEMPTABLE
select nvl2((select 1 from user_tab_columns where TABLE_NAME='表名' and COLUMN_NAME='test'), test, 'aaa') From MYTEMPTABLE nvl2起什么用作,是啥意思?
nvl2是个函数。 大体的含义是,如果第一个表达式的值为1,则返回紧接下来的值,否则返回后一个值。这样的话,如果“表名”表中存在test字段,则取test字段的值,否则取值'aaa'。 另外上面的写得有点错误,改正一下:)select nvl2((select 1 from user_tab_columns where TABLE_NAME='表名' and COLUMN_NAME='test'), test, 'aaa') From 表名
select nvl2((select count(*) from user_tables where TABLE_NAME='表名' and COLUMN_NAME='test'), test, 'aaa') From tablename
select test from table
else
select test='',bbb from table
select test from table
else
select 'test' test,bbb from table
nvl2起什么用作,是啥意思?
大体的含义是,如果第一个表达式的值为1,则返回紧接下来的值,否则返回后一个值。这样的话,如果“表名”表中存在test字段,则取test字段的值,否则取值'aaa'。
另外上面的写得有点错误,改正一下:)select nvl2((select 1 from user_tab_columns where TABLE_NAME='表名' and COLUMN_NAME='test'), test, 'aaa') From 表名