Is it possible to return empty row from Sql Server

2019-06-26 09:04发布

问题:

Is this possible to return all columns in query as empty column (not null) or empty row, in case the actual query is returning no rows

回答1:

Generally, if you must have an empty row returned..

If your original query is

select a,b,c from tbl

You can turn it into a subquery

select t.a,t.b,t.c
from (select 1 as adummy) a
left join (
    select a,b,c from tbl  -- original query
) t on 1=1

Which ensures the query will always have a rowcount of at least one.



回答2:

If your objective is to return a query with no records, or with an empty recordset/dataset, the following should work without any previous knowledge on the original query:

SELECT * FROM (myOriginalQuery) as mySelect WHERE 0 = 1