Sunday 15 June 2014

sql - Another way to prevent/show no result sets -



sql - Another way to prevent/show no result sets -

if tab1 , tab2 homecoming no results want final select 0,0... returned.

select tab1.openedcnt, tab2.closedcnt, 'modulea' module (select count(*) openedcnt,'constant1' const 1=1) tab1 cross bring together (select count(*) closedcnt,'constant2' const 1=2 )tab2-- simulating no result set returned union select 0,0,'modulea'

results in...

openedcnt closedcnt module 0 0 modulea 1 0 modulea

i can't rid of cross join. statements purposes of example.

for example...if tab1 returns result , tab2 not i'm looking results of...

openedcnt closedcnt module 1 0 modulea

if tab1 , tab2 not homecoming results i'm looking results of...

openedcnt closedcnt module 0 0 modulea

thanks help.

hmmm. think can rather straight using union all , not exists. here idea:

with tab1 ( select count(*) openedcnt, 'constant1' const 1=1 ), tab2 ( select count(*) closedcnt, 'constant2' const 1=2 ) select tab1.* tab1 union select tab2.* tab2 union select 0, 0, 'modulea' not exists (select 1 tab1) , not exists (select 1 tab2);

note utilize of ctes create query easier write , read.

sql sql-server-2008 rdbms

No comments:

Post a Comment