Thursday, 21 June 2012

Return a comma seperated string in sql ?

create function funcationCommaSeparete(@myID int)
returns varchar(8000) as
begin
declare ps cursor for select columnName from TableName where columnName = @myID
declare @comma varchar(1),@res varchar(8000), @colvalue varchar(100)
set @res=''
set @comma=''
open ps
fetch next from ps into @colvalue
while @@fetch_status=0 begin
  set @res=@res+@comma+@colvalue
  set @comma=','
  fetch next from ps into @colvalue
end
close ps
deallocate ps
return @res
end

------------------- Using
select dbofuncationCommaSeparete(1)

No comments:

Post a Comment