我有一张桌子tblAccount
。我想从表中获得前4行。如果没有记录,我想得到4个空行。
select *
from tblAccount
o/p
----
AccountID AccountNo
1 #101
2 #102
NULL NULL
NULL NULL
如果有两条记录,上面应该是结果。
答案 0 :(得分:0)
这应该有效。您可以使用临时表执行相同的操作,只需为其提供正确数量的字段和行。
with meganull(a,b) as (
select CAST(null as int),
CAST(null as varchar(max))
union all
select *
from meganull
)
select top 4 *
from (
select *
from tblAccount
union all
select *
from meganull) as sq
答案 1 :(得分:0)
select TOP 4 AccountID,AccountNo
from
(
select 0 as srt,AccountID,AccountNo from tblAccount
union all
select 1 as srt,NULL as AccountID, NULL as AccountNo
union all
select 2 as srt,NULL as AccountID, NULL as AccountNo
union all
select 3 as srt,NULL as AccountID, NULL as AccountNo
union all
select 4 as srt,NULL as AccountID, NULL as AccountNo
) as t
order by srt,AccountID
答案 2 :(得分:0)
试试这个......
Select * FROM (VALUES (1),(2),(3),(4)) AS value(tmpID) left join
(
select Row_Number() over (order by AccountID) as RowCtr, *
from tblAccount
) accountTable on tmpID = accountTable.RowCtr
当我想要一种方法强制在SSRS报告中显示一定数量的行时,我使用了类似的技术,其中空白行代表物理机架中的空白点。