我想通过sql查询得到固定行数据,不足的用空行填补。
如:数据库a表(3列)有3行数据,我要得到一个5行的数据集。不足的用空行填补,
结果传到表中为(不一定传表中只是举例子,要得是符合要求的数据集),结果应为
aaaaaa 322 342
bbbbbb 213 213
cccccc 323 352
“空空”“空”“空”
“空空”“空”“空”怎样才能得到上述结果(用sql sever)
如:数据库a表(3列)有3行数据,我要得到一个5行的数据集。不足的用空行填补,
结果传到表中为(不一定传表中只是举例子,要得是符合要求的数据集),结果应为
aaaaaa 322 342
bbbbbb 213 213
cccccc 323 352
“空空”“空”“空”
“空空”“空”“空”怎样才能得到上述结果(用sql sever)
(
select * from a where col1 like '%空%' and col2 like '%空%' and col3 like '%空%'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
) t
order by col1
insert into tb values('aaaaaa' , '322' , '342' )
insert into tb values('bbbbbb' , '213' , '213' )
insert into tb values('cccccc' , '323' , '352' )
goselect top 5 * from
(
select * from tb where col2 = '322'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
) t
order by col1drop table tb/*
col1 col2 col3
---------- ---------- ----------
aaaaaa 322 342
空空 空 空
空空 空 空
空空 空 空
空空 空 空(所影响的行数为 5 行)*/
go
create table #(a varchar(10),b varchar(10),c varchar(10))
insert # select 'aaaaaa' ,322 , 342
insert # select 'bbbbbb' , 213, 213
insert # select 'cccccc' ,323 ,352
if (select count(*) from #)<5
select * from #
union all
select '“空空”','“空”','“空”'
select '“空空”','“空”','“空”'
/*---------- ---------- ----------
aaaaaa 322 342
bbbbbb 213 213
cccccc 323 352
“空空” “空” “空”
-------- ------ ------
“空空” “空” “空”*/
INSERT @TB
SELECT 'aaaaaa', '322', '342' UNION ALL
SELECT 'bbbbbb', '213', '213' UNION ALL
SELECT 'cccccc', '323', '352'DECLARE @MAX INT
SET @MAX=3
WHILE @MAX<5
BEGIN
INSERT @TB SELECT '','',''
SET @MAX=@MAX+1
ENDSELECT * FROM @TB
/*
COL COL2 COL3
---------- ---------- ----------
aaaaaa 322 342
bbbbbb 213 213
cccccc 323 352
(5 row(s) affected)
*/
from
(
select * from a
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
union all
select '空空' , '空' , '空'
)t
select *,idx=0 from tb
union all
select null,null,null,idx=1 from sysobjects
order by idx
set rowcount 0
if object_id('tempdb..#')is not null drop table #
go
create table #(a varchar(10),b varchar(10),c varchar(10))
insert # select 'aaaaaa' ,322 , 342
insert # select 'bbbbbb' , 213, 213
insert # select 'cccccc' ,323 ,352
if (select count(*) from #)<5
select top 5 * from (
select * from #
union all
select '空空','空','空'
union all
select '空空','空','空'
union all
select '空空','空','空'
union all
select '空空','空','空'
union all
select '空空','空','空' )t
else
select top 5 * from #
/*a b c
---------- ---------- ----------
aaaaaa 322 342
bbbbbb 213 213
cccccc 323 352
空空 空 空
空空 空 空*/