--为了效率,所以要一个辅助表配合
select top 8000 id=identity(int,1,1) into 序数表 
from syscolumns a,syscolumns b
alter table 序数表 add constraint pk_id_序数表 primary key(id)
gocreate function f_split(
@str varchar(8000), --要分拆的字符串
@splitchar varchar(10) --分隔符
)returns table
as
return(
select re=substring(@str,id,charindex(@splitchar,@str+@splitchar,id)-id)
from 序数表 
where id<=len(@str)+1 and charindex(@splitchar,@splitchar+@str,id)-id=0
)
go
declare @s varchar(200)
select @s=class from 表A where [name]='00'select a.id,a.class,b.[name] from 表B a left join (select '00' as [name],* from f_split(@s,',')) b on a.class=b.re

解决方案 »

  1.   

    declare @a table (id int,name varchar(10),class varchar(20))
    insert into @a 
    select  1 ,        '00',        '1,2,3' union all
    select  2 ,        '01',        '2,3' union all
    select  3 ,        '02',        '3,4,5'
    declare @b table (id int,class varchar(20))
    insert into @b 
    select 1,1 union all
    select 2,2 union all
    select 3,3 union all
    select 4,4 union all
    select 5,5 select b.*,a.name
    from @b b
    left join (select * from @a where id=1) a
    on charindex(','+b.class+',',','+a.class+',')>0
      

  2.   

    631799(杭州工人) ,xluzhong(Ralph) 两位的方法我都test了,都可以实现;xluzhong的方法更简洁,631799的split函数用处很多啊。请问xluzhong,在charindex(','+b.class+',',','+a.class+',')>0,为什么要加','呢?不用效果也一样啊,求教?
    今晚结贴!
      

  3.   

    不好意思,本来应该当天结贴,但是有事耽误了,今天补上。631799(杭州工人):50;xluzhong(Ralph) :60。请查收,谢谢了!
      

  4.   

    请xluzhong(Ralph)到http://community.csdn.net/Expert/topic/4082/4082510.xml?temp=.7459223接剩余分数!