–将查询结果的某列按指定的符号连接起来
–AUTHOR:Mender.Z.J
ALTER procedure dbo.getColWithMerg (
 @preSymbol   varchar(256),
 @tailSymbol   varchar(256),
 @colName   varchar(50),
 @queryStr   varchar(1024),
 @retStr  varchar(8000) output
)  
as
BEGIN 
declare @count as decimal;
declare @sqlStr as varchar(1000);
declare @i as decimal;
–declare @retStr as varchar(8000);
declare @tt as varchar(255);
set @i=1;

set @sqlStr=’select ‘+ @colName +’ from (‘+@queryStr+’) as t1′ ;

 if exists(select * from dbo.sysobjects where xtype=’U’ and name=N’tempAbcdbcad’) 
 begin
 drop table tempAbcdbcad;
 end
 create table tempAbcdbcad(t varchar(255));  
insert into tempAbcdbcad(t) exec(@sqlStr);

declare CUR  CURSOR FOR select rtrim(t) from tempAbcdbcad;
open CUR
fetch next from CUR into @tt;
set @retStr=”;
while (@@fetch_status=0)
begin
  print(@retStr);
  set @retStr = @retStr + ” + @preSymbol + ” + rtrim(@tt) + ” +@tailSymbol;
  set @i=@i+1;
  fetch next from CUR into @tt;
end

close CUR
deallocate CUR

 if exists(select * from dbo.sysobjects where xtype=’U’ and name=N’tempAbcdbcad’) 
 begin
 drop table tempAbcdbcad;
 end 

END