不能实现,可以在asp中对结果集进行split

解决方案 »

  1.   


    Create Function dbo.F_split
            (
            @subject nvarchar(50)
            )
    Returns @table table(科目 nvarchar(50),姓名 nvarchar(10))
    As
    Begin
    /* 获取姓名列表 */
    Declare @name_string nvarchar(10)
    Select @name_string=姓名
    From 表 Where 科目=@subject
    /* 循环取得姓名 */
    While(charindex(',',@name_string)<>0)
    Begin
            Insert @table Select @subject,left(@name_string,charindex(',',@name_string)-1)
            Select @name_string=stuff(@name_string,1,charindex(',',@name_string),'')
    End
    Insert @table Select @subject,@name_string
    Return
    End
    /* 测试 */
    select * from dbo.F_split('数学')
      

  2.   

    --测试数据
    create table [table](course varchar(20),name varchar(20)) 
    insert [table] select '数学','张三,李四' --创建过程
    create proc ptest 
    as begin
    set nocount on 
    declare @i as int
    declare @a varchar(20)
    declare @t table(course varchar(20),name varchar(20)) 
    declare @course varchar(20)
    declare @s varchar(20)
    set @course=''
    while @course is not null 
    begin
        select @course=min(course) from [table] where course>@course
        if @course is not null begin
           select @s=name from [table] where course=@course             
           set @i=charindex(',',@s) 
           while @i>0
           begin
        set @a=left(@s,@i-1)
        insert  @t values(@course,@a)
        set @s=right(@s,len(@s)-@i)
                set @i=charindex(',',@s)     
           end
           if len(@s)>0
           begin
                insert @t values(@course,@s)
           end
         end
    end
    select * from @t
    set nocount off
    end --调用
    exec ptest --结果
    course               name                 
    -------------------- -------------------- 
    数学                   张三
    数学                   李四
      

  3.   

    create table #t(科目 varchar(10),姓名 varchar(100))
    insert #t
    select '数学','张三,李四,王五,赵六'--查询
    select identity(int,1,1) ID into # from syscolumns a,syscolumns bselect a.科目
           ,substring(a.姓名,b.ID,charindex(',',a.姓名+',',b.ID)-b.ID) as '姓名'
    from #t a
    join #  b on substring(','+a.姓名,b.ID,1)=','--删除测试环境
    drop table #t,#--结果
    /*
    科目         姓名     
    ---------- --------
    数学         张三
    数学         李四
    数学         王五
    数学         赵六(所影响的行数为 4 行)
    */