表结构改一下,建一个class表,你上面这个表作为class的从表,一对多 的关系。

解决方案 »

  1.   

    不改结构的话就要用游标
    读出class来,分割字符串,处理好了再写回去具体函数查帮助吧
      

  2.   

    --写个自定义函数就行了
    create function f_str(
    @str varchar(8000)
    )returns varchar(8000)
    as
    begin
    declare @t table(word varchar(100))
    while @str>''
    begin
    insert @t select left(@str,charindex(';',@str+';')-1)
    set @str=stuff(@str,1,charindex(';',@str+';'),'')
    end
    select @str=@str+';'+word from @t group by word
    return(stuff(@str,1,1,''))
    end
    go--测试函数
    create table tb(id int,class varchar(20))
    insert tb select 1,'A;C;F;F;A;C'
    union all select 2,'B;B;B;C'
    union all select 3,'C;A;T;D'
    union all select 4,'A;A;D;C'
    go--调用函数实现处理
    update tb set class=dbo.f_str(class)--显示处理结果
    select * from tb
    go--删除测试
    drop table tb
    drop function f_str/*--测试结果id          class                
    ----------- -------------------- 
    1           A;C;F
    2           B;C
    3           A;C;D;T
    4           A;C;D(所影响的行数为 4 行)
    --*/
      

  3.   

    用函数
    create function test(@SourceSql varchar(8000))
    returns varchar(8000)
    as 
    begin
    declare @i int
    declare @j int
    declare @temp varchar(10)
    declare @ReturnSql varchar(8000)
    set @ReturnSql=''
    set @i=0
    set @j=0
    set @temp=''
    set @SourceSql=rtrim(ltrim(@SourceSql))
    set @i=charindex(';',@SourceSql)
    while @i>0
    begin
    set @temp=left(@SourceSql,@i-1)
    if charindex(@temp,@ReturnSql)<=0 
    begin
    set @ReturnSql=@ReturnSql+@temp+';'
    end
    set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
    set @i=charindex(';',@SourceSql)
    end
    set @ReturnSql=left(@ReturnSql,len(@ReturnSql)-1)
    return (@ReturnSql)
    endselect id,dbo.test(class) from tablename
      

  4.   

    --如果要保留原来的顺序,则改一下处理函数create function f_str(
    @str varchar(8000)
    )returns varchar(8000)
    as
    begin
    declare @t table(id int identity,word varchar(100))
    while @str>''
    begin
    insert @t select left(@str,charindex(';',@str+';')-1)
    set @str=stuff(@str,1,charindex(';',@str+';'),'')
    end
    select @str=@str+';'+word from @t group by word order by min(id)
    return(stuff(@str,1,1,''))
    end
    go
      

  5.   

    用函数
    create function test(@SourceSql varchar(8000))
    returns varchar(8000)
    as 
    begin
    declare @i int
    declare @temp varchar(10)
    declare @ReturnSql varchar(8000)
    set @ReturnSql=''
    set @i=0
    set @temp=''
    set @SourceSql=rtrim(ltrim(@SourceSql))
    set @i=charindex(';',@SourceSql)
    while @i>0
    begin
    set @temp=left(@SourceSql,@i-1)
    if charindex(@temp,@ReturnSql)<=0 
    begin
    set @ReturnSql=@ReturnSql+@temp+';'
    end
    set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
    set @i=charindex(';',@SourceSql)
    end
    set @ReturnSql=left(@ReturnSql,len(@ReturnSql)-1)
    return (@ReturnSql)
    endselect id,dbo.test(class) from tablename