Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a function that concatenates rows into a string. Say for example I have a column in a table called PACKID and its returns :-URN | NAME | person_id | PACKID | PRIMARY1 | Sample | 111111111 | 12 | 02 | Sample | 111111111 | 8 | 13 | Sample | 111111111 | 7 | 04 | Sample | 111111111 | 11 | 05 | Sample | 111111111 | 5 | 06 | Sample | 111111111 | 8 | 1If I want to concatenate into a string all the packids then I have a select statement which calls my function :-SELECT DISTINCT person_id, dbo.ConcatPackIds(person_id) as 'folder' into #temptableFROM tblDocumentswhere pack_id = 7This select statement will return12-8-7-11-8However I want it to return a distinct packId so I would get12-8-7-11i.e. no duplicate 8, however if I simply add a distinct statement to the front of my function (below) it only returns the first packid.My functionCREATE function dbo.ConcatPackIds(@person_id varchar(50))returns varchar(8000)asbegin declare @output varchar(8000) set @output = '' select @output = case @output when '' then convert(varchar(100), pack_id) else @output + '-' + convert(varchar(100), pack_id) end from tblDocuments where person_id = @person_id and [primary] = 1 and pack_id >= 7 order by pack_id return @outputendCan you help please
Kristen
Test
22859 Posts
Posted - 2006-04-05 : 08:41:24
This perhaps?
select @output = case @output when '' then convert(varchar(100), pack_id) else @output + '-' + convert(varchar(100), pack_id) endFROM(SELECT DISTINCT pack_idfrom tblDocumentswhere person_id = @person_id and [primary] = 1 and pack_id >= 7) AS Xorder by pack_id