日期:2014-05-17 浏览次数:20496 次
-------------以下是测试数据-----------------:
use Tempdb
go
if not object_id(N'Tempdb..#1') is null
drop table #1
Go
create table [#1](ID int, name varchar(3), RowNumber int)
insert [#1]
select 1,'ij',3 union all
select 2,'as',4 union all
select 3,'asd',1
--------------开始查询--------------------------
select ID,a.name,c.number
from
[#1] a
left join
master..spt_values c
on
c.number<=a.[RowNumber]
and
c.number>0
and
c.[type]='p'
-------------------------------------------------
--效果如下
/*
ID name number
1 ij 1
1 ij 2
1 ij 3
2 as 1
2 as 2
2 as 3
2 as 4
3 asd 1
*/
with tb(ID, name,RowNumber )
as(
select 1,'ij',3 union all
select 2,'as',4 union all
select 3,'asd',1)
select tb.* from tb,master..spt_values a where a.type='p'
and number between 1 and RowNumber
-------------以下是测试数据-----------------:
use Tempdb
go
if not object_id(N'Tempdb..#1') is null
drop table #1
Go
create table [#1](ID int, name varchar(3), RowNumber int)
insert [#1]
select 1,'ij',3 union all
select 2,'as',4 union all
select 3,'asd',1
--------------开始查询--------------------------
select ID,a.name,a.[RowNumber]
from
[#1] a
left join
master..spt_values c
on
c.number<=a.[RowNumber]
and
c.number>0
and
c.[type]='p'
-------------------------------------------------
--效果如下
/*
ID name RowNumber
1 ij