1

我有一张桌子tblAccount。我想从表中获取前 4 行。如果没有记录,我想得到 4 个空白行。

select * 
from tblAccount

o/p
----
AccountID AccountNo
1          #101
2          #102
NULL       NULL
NULL       NULL

如果有两条记录,上面应该是结果。

4

3 回答 3

0

SQLFiddle demo

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
于 2013-01-25T07:00:32.870 回答
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
于 2013-01-25T04:48:17.250 回答
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 报告中显示一定数量的行时,我使用了类似的技术,其中空白行表示物理机架中的空白点。

于 2013-08-06T19:24:43.230 回答