如select * from table1,可以得到结果如下
name  value  
张三  2我现在想把这个结果变成
name  value  
张三  1
张三  1把value字段拆分成多条显示,每次都显示为1,SQLSERVER能不能实现如上答者有分

解决方案 »

  1.   

    value 这个字段是什么类型的?
    这个值是直接保存在表中的吗?
      

  2.   

    回楼上,value是数字整形,如int型
    值是直接保存的
      

  3.   

    --建立测试环境
    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 intdeclare 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_cursorselect * from #tb
    go
    --删除测试环境
    drop table tb,#tb
    go
    /*--测试结果
    name       value       
    ---------- ----------- 
    张三         1
    张三         1
    李四         1
    李四         1
    李四         1(5 row(s) affected)
    */
      

  4.   


    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
      

  5.   

    --建立测试环境
    create table tb(name varchar(10),value int)
    insert tb(name,value)
    select '张三','2' union all
    select '李四','3'/*--测试结果
    name                 value       
    -------------------- ----------- 
    张三                   1
    李四                   1
    张三                   1
    李四                   1
    李四                   1
      

  6.   

    select identity(int,1,1) as id into # from sysobjects,syscolumnsselect a.name,1 as [value] from table1 a,# b where a.[value]>=b.id
      

  7.   

    create table table1(name varchar(10),value int)
    insert table1(name,value)
    select '张三','2' union all
    select '李四','3'
    goset rowcount 10000
    select identity(int,1,1) as id into # from sysobjects,syscolumns
    set rowcount 0select 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
      

  8.   

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

  9.   

    如果value超大,可以再嵌套几个表,预先分析table1的最大值,取一个上限即可。
      

  10.   

    能不能解读一下
    set rowcount 10000
    select identity(int,1,1) as id into # from sysobjects,syscolumns
    set rowcount 0