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

SQL中如何把1笔资料重复显示成多笔,不要用循环!
假如有如下资料:
a    b     4
c    d     1
e    f     2
--------------
我希望能查询出:
a    b     
a    b
a    b
a    b
c    d
e    f
e    f 
这样的结果,谢谢大家的帮助~~~

------解决方案--------------------
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (col1 nvarchar(2),col2 nvarchar(2),col3 int)
insert into [TB]
select 'a','b',4 union all
select 'c','d',1 union all
select 'e','f',2

select * from [TB]


SELECT col1,col2
FROM dbo.TB
INNER JOIN master..spt_values M ON TB.col3>M.number AND M.type = 'P'

/*
col1 col2
a b
a b
a b
a b
c d
e f
e f*/

------解决方案--------------------
select col1,col2 from tb,master.spt_values where type='p' and number between 1 and col3