SQL查询添加备用空白记录

时间:2011-09-28 16:03:10

标签: sql-server-2005

我正在使用以下查询来显示来自SQL SERVER的记录

select * from orders

目前此查询显示数据库中的所有100条记录,我需要的是100而不应该显示200条记录(所以备用空白记录很好)有可能这样做吗?

1 个答案:

答案 0 :(得分:3)

-- sample table
declare @Order table
( 
  orderid int,
  qty int
)

-- add some data
insert into @Order
select 1, 10 union all
select 2, 20 union all
select 3, 30

-- cross join the query against two rows       
select case D.N when 1 then O.orderid end as orderid,
       case D.N when 1 then O.qty end as qty
from @Order as O
  cross join (select 1 union all select 2) as D(N)
order by O.orderid, D.N  

结果:

orderid     qty
----------- -----------
1           10
NULL        NULL
2           20
NULL        NULL
3           30
NULL        NULL