日期:2014-05-17  浏览次数:20791 次

MSSQL怎样将查询的每一条记录显示成两条?
MSSQL怎样将查询的每一条记录显示成两条?比如:
表A:
A01   A02  A03
--------------
1     2    3
11    22   33
..

select * 想要得到这个效果?
1   2   3
1   2   3
11  22  33
11  22  33
...

------解决方案--------------------

select * from
(select * from 表A
 union all
 select * from 表A) t
order by t.A01,t.A02,t.A03

------解决方案--------------------
----------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-04-24 14:49:13
-- Version:
--      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) 
-- Jun 17 2011 00:54:03 
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go 
create table [A]([A01] int,[A02] int,[A03] int)
insert [A]
select 1,2,3 union all
select 11,22,33
--------------开始查询--------------------------
if object_id('[B]') is not null drop table [B]
go 
create table [B](B INT
)
insert [B]
select 1 union all
select 2
select A.* from [A] CROSS JOIN B 
ORDER BY A01
----------------结果----------------------------
/* 
A01         A02         A03
----------- ----------- -----------
1           2           3
1           2           3
11          22          33
11          22          33
*/

------解决方案--------------------
create table [A]([A01] int,[A02] int,[A03] int)
insert [A]
select 1,2,3 union all
select 11,22,33


select * from A
union all
select * from A

A01         A02         A03
----------- ----------- -----------
1           2           3
11          22          33
1           2       &nb