Sql 不管怎样,还是要获取最上面的行数
我的要求类似于从sql中选择前5行,如果它只包含2行,那么默认情况下,它会在其余3行中显示一些文本,如“无数据”。所有条件都相同。 例如,选择前5行,但它只包含3行,然后查询将返回 第1行 第2行 第3行 没有数据 没有数据请尝试:Sql 不管怎样,还是要获取最上面的行数,sql,sql-server,Sql,Sql Server,我的要求类似于从sql中选择前5行,如果它只包含2行,那么默认情况下,它会在其余3行中显示一些文本,如“无数据”。所有条件都相同。 例如,选择前5行,但它只包含3行,然后查询将返回 第1行 第2行 第3行 没有数据 没有数据请尝试: select top 5 Col from( select 0 srt, Col from YourTable union all select 1 srt, 'No Data' Col union all select 1 s
select top 5 Col
from(
select 0 srt, Col from YourTable
union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col
)x
order by srt
请尝试:
select top 5 Col
from(
select 0 srt, Col from YourTable
union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col union all
select 1 srt, 'No Data' Col
)x
order by srt
这将更有活力
declare @t TABLE (id int,TerminalID varchar(15))
declare @top int
set @top = 5
declare @c int --Holds Total
set @c = 0
while (@c < @top) begin
insert into @t (id, TerminalID) values (@c, 'No Data')
set @c = @c + 1
end
select top 5 * from
(
select top 5 TerminalID from AccessLog-- [where colo0 = ???]
union all
select TerminalID from @t
) x
这将更有活力
declare @t TABLE (id int,TerminalID varchar(15))
declare @top int
set @top = 5
declare @c int --Holds Total
set @c = 0
while (@c < @top) begin
insert into @t (id, TerminalID) values (@c, 'No Data')
set @c = @c + 1
end
select top 5 * from
(
select top 5 TerminalID from AccessLog-- [where colo0 = ???]
union all
select TerminalID from @t
) x
是的,我在想。我可以使用c编码,但我只希望使用sql查询。使用c编码返回消息行的结构是什么?在哪一列中应该说没有数据?它包含3列。在使用顶部和剩余的总行之后,它应该显示任何默认值。是的,我在想。我可以使用c编码,但我只希望使用sql查询。使用c编码返回消息行的结构是什么?在哪一列中应该说没有数据?它包含3列。在使用顶部和剩余的总行之后,它应该显示任何默认值。