日期:2014-05-18  浏览次数:20409 次

求解,能不能将查询结果拆分
如select   *   from   table1,可以得到结果如下
name     value    
张三     2

我现在想把这个结果变成
name     value    
张三     1
张三     1

把value字段拆分成多条显示,每次都显示为1,SQLSERVER能不能实现如上

答者有分



------解决方案--------------------
先占位,再想
------解决方案--------------------
value 这个字段是什么类型的?
这个值是直接保存在表中的吗?
------解决方案--------------------
--建立测试环境
create table tb(name varchar(10),value int)
insert tb(name,value)
select '张三 ', '2 ' union all
select '李四 ', '3 '
go
--执行测试语句
create table #tb(name varchar(10),value int)

declare @name varchar(10),@value int

declare t_cursor cursor for
select t.name,t.value from tb t
open t_cursor
fetch next from t_cursor into @name,@value
while @@fetch_status = 0
begin
while @value > 0
begin
insert #tb select @name,1
set @value = @value - 1
end
fetch next from t_cursor into @name,@value
end
close t_cursor
deallocate t_cursor

select * from #tb
go
--删除测试环境
drop table tb,#tb
go
/*--测试结果
name value
---------- -----------
张三 1
张三 1
李四 1
李四 1
李四 1

(5 row(s) affected)
*/


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

create table #t(name varchar(20),value int)

declare @i int
select @i=0 from tb
while ((select count(*) from tb where value-@i> =1)> 0)
begin
insert into #t
select name as [name], '1 ' as value from tb where value-@i> 0
select @i=@i+1
end
select * from #t


drop table #t
------解决方案--------------------
没必要这样吧,交给前台处理,游标会很慢d

------解决方案--------------------
create table table1(name varchar(10),value int)
insert table1(name,value)
select '张三 ', '2 ' union all
select '李四 ', '3 '
go

set rowcount 10000
select identity(int,1,1) as id into # from sysobjects,syscolumns
set rowcount 0

select a.name,1 as [value] from table1 a,# b where a.[value]> =b.id order by a.name
go

/*
name value
---------- -----------
李四 1
李四 1
李四 1
张三 1
张三 1
*/

drop table #,table1
go
------解决方案--------------------
呵呵,楼上的简单
------解决方案--------------------
from sysobjects,syscolumns

-------------------------------------------

对于value超大的时候,这种处理方式是有问题的。