--准备一个序数表,辅助处理,top 1000中的1000表示最多可以处理1000个单位.第一次使用的时候创建,以后就不用再建了.
select top 1000 id=identity(int,1,1) into 序数表
from(select id from sysobjects) a,(select id from sysobjects) b
go
alter table 序数表 add constraint PK_序数表 PRIMARY KEY CLUSTERED (id)
go
--查询处理的存储过程
create proc p_qry
@dw varchar(8000) --要查询的单位列表,单位之间用,分隔
as
select a.*
from 数据表 a join(
select 单位名称=substring(name,b.id,charindex(',',name+',',b.id)-b.id)
from (select name=@dw) a,#tb b where substring(','+name,b.id,1)=','
) b on a.单位名称=b.单位名称
go