日期:2014-05-18 浏览次数:20576 次
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table # (id int)
insert into #
select 1 union all
select 3 union all
select 4 union all
select 5 union all
select 7
declare @str varchar(100)
;
with gid(gid,id) as
(
select id - row_number()over(order by id), id from # -- where Model=@Model
), val(val) as
(
select ltrim(min(id)) + isnull('-'+ltrim(nullif(max(id),min(id))),'') from gid group by gid
)
select @str = isnull(@str+',','')+val from val
select @str --> 1,3-5,7