简体   繁体   中英

Introduce dummy row in a sybase query

I need to introduce a dummy row if my query 1 fails to fetch result.

select column1,column2,... from <ActualTable> Where condition='abc'... (1)
Union
select "dummy col1","dummy col2"..... from <dummy table> where col1 NOT IN (select column1 from
<ActualTable> where condition = 'abc'..)                               (2)

With the above query if query 1 fetches result query 2 wont. If query 1 has no result then i would get a dummy row.

Is there any other way to achieve the same result in Sybase?

Temp table if the "ActualTable" gives >1 row

select column1,column2,... INTO #temp
from <ActualTable>
Where condition='abc'... (1)

IF @@ROWCOUNT = 0
    select "dummy col1","dummy col2"..... INTO #tmp
    from <dummy table>

-- #temp will exist now

INSERT #temp
select column1,column2,...
from <AnotherTable>
Where condition='abc'... (1)

IF @@ROWCOUNT = 0
    INSERT #temp
    select "dummy col1","dummy col2".....
    from <dummy table>

... 
SELECT * FROM #tmp

Try this:

select top 1 * from
(
select 0,column1,column2,... from <ActualTable> Where condition='abc'... (1)
Union
select 1,"dummy col1","dummy col2"..... from <dummy table> 
order by 1
)

If first query is successful, you will get it, if not, then the second will be returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM