首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL中怎么把1笔资料重复显示成多笔,不要用循环

2013-03-12 
SQL中如何把1笔资料重复显示成多笔,不要用循环!假如有如下资料:ab4cd1ef2--------------我希望能查询出:ab

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'

/*
col1col2
ab
ab
ab
ab
cd
ef
ef*/

[解决办法]
select col1,col2 from tb,master.spt_values where type='p' and number between 1 and col3

热点排行