SQL server 2005 表中某一字段的所有值逗号分开的列表

--SQL server 2005 表中某一字段的所有值逗号分开的列表Geovin Du 塗聚文
select * from Production.Product

declare @names varchar(50)
set @names=''
select @names=@names+s.Name+' , '
from Production.Product s
order by s.Name

select @names
go
--sql server 2000 如何逐条读出表中某一字段的所有值 Geovin Du 塗聚文
declare @allstring varchar(8000)
set @allstring=''
select @allstring=@allstring+convert(varchar(10),PublicHolidayID)+';' from PublicHolidayRest 
print @allstring
GO

 

--查询字符中包函的结果
declare @sql varchar(500),@in varchar(500),@where varchar(800),@allstring varchar(8000) 
set @sql='select * from intranet.dbo.v_lc '
set @allstring=''

select @allstring=@allstring+''''+BranchNO+''''+',' from  intranet.dbo.v_lc  where ServiceCardNo='008888'
set @allstring=left(@allstring,len(@allstring)-1)
set @where=@allstring
set @in=' where  BranchNO in ('+@where+')'
set @sql=@sql+@in
print @sql
print @allstring 
exec (@sql)
GO 

 

posted @ 2011-07-08 08:37  ®Geovin Du Dream Park™  阅读(406)  评论(0编辑  收藏  举报