现在的表示
表名:ABC
AAA BBB CCC
--------------------------------------------
A01 B01 C01
A02 B02 C02
A03 B03 C03
想得到的结果是
AAA BBB CCC
--------------------------------------------
A01 B01 C01
空行1
空行2
A02 B02 C02
空行1
空行2
A03 B03 C03
空行1
空行2
必须得用游标循环表ABC,每循环一会出入数据后再插入两个空行吗
用一个select可以实现吗?
谢谢了
------解决思路----------------------
一个SELECT 不好弄,试下这个
declare @abc table
(
AAA varchar(10),
BBB varchar(10),
CCC varchar(10)
)
IF OBJECT_ID('TEMPDB..#CTE') > 0
DROP TABLE #CTE
INSERT INTO @abc VALUES
('A01', 'B01', 'C01'),
('A02', 'B02', 'C02'),
('A03', 'B03', 'C03');
WITH CTE AS
(
SELECT ROW_NUMBER()OVER(PARTITION BY AAA,BBB,CCC ORDER BY (SELECT 0)) ID,A.* FROM @abc A
CROSS JOIN (SELECT TOP 3 NAME FROM SYSOBJECTS)B
)
SELECT * INTO #CTE FROM CTE
UPDATE A SET AAA='',BBB='',CCC=''
FROM #CTE A WHERE ID >1
SELECT AAA,BBB,CCC FROM #CTE
------解决思路----------------------
上面的sql有点问题,可以试一下这句sql
select AAA,BBB,CCC from (
select AAA,BBB,CCC,AAA as col1,RIGHT(AAA,2) as col2 from ABC union all
select ' ',' ',' ',AAA,RIGHT(AAA,2)+1 from ABC union all
select ' ',' ',' ',AAA,RIGHT(AAA,2)+2 from ABC) a order by col1,col2
------解决思路----------------------
use tempdb
create table ABC(AAA VARCHAR(10),BBB VARCHAR(10),CCC VARCHAR(10))
INSERT INTO ABC
SELECT 'A01','B01','C01' UNION ALL
SELECT 'A02','B02','C02' UNION ALL
SELECT 'A03','B03','C03'
;with sel as(
select (row_number() over(order by getdate())-1)*3 as rn,* from abc
)
select a.* from sel a
right join (select number from master..spt_values where type='p') b
on a.rn=b.number
where b.number<=(select max(rn) from sel )
------解决思路----------------------
select AAA,BBB,CCC from (
select AAA,BBB,CCC,AAA as col1,RIGHT(AAA,2) as col2 from ABC union all
select ' ',' ',' ',AAA,RIGHT(AAA,2)+1 from ABC union all
select ' ',' ',' ',AAA,RIGHT(AAA,2)+2 from ABC) a order by col1,col2
------解决思路----------------------
--CTE是辅助表。就是个数字表复制2遍
with cte as
(select 1 as n union all
select 1 union all
select 1 union all
select 2 union all
select 2 union all
select 2 union all
select 3 union all
select 3 union all
select 3 ),
abc as
(select 'a01' as aaa,'b01' as bbb,'c01' as ccc union all
select 'a02' as aaa,'b02' as bbb,'c02' as ccc union all
select 'a03' as aaa,'b03' as bbb,'c03' as ccc )