Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stringing results together

Posted on 2013-01-17
5
Medium Priority
?
208 Views
Last Modified: 2013-01-21
I have a function (SQL Server) I use to return values - see below


CREATE FUNCTION [dbo].[GetRelatedRecords] (@id bigint)
returns varchar(250)
as
BEGIN
declare @results varchar(250)
SELECT @results = 'Related Record: ' + R.recordid
FROM RECORDS R
left join LINKS L
      ON R.id = L.LinkID
WHERE L.id = @id
RETURN @results
END


Assuming the value or R.Recordid = 'XYZ' then this will return 'Related Record: XYZ' which is great, but there are sometimes more than one match for L.id = @id and I'd like them all (rather than the last one which is what is currently returned.

So if the results were XYZ and ABC I need to return ''Related Records: XYZ|Related Records: ABC'
0
Comment
Question by:QPR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:MMTadmin
ID: 38789309
Return a table?

Create FUNCTION SalesByStore(@storid varchar(30))
RETURNS TABLE
AS
RETURN (SELECT title, qty
        FROM dbo.sales s, dbo.titles t
        WHERE s.stor_id = @storid AND t.title_id = s.title_id)
GO

If you really want to return a string (strange) then insert into a table variable loop through it and return the string
0
 
LVL 29

Author Comment

by:QPR
ID: 38790205
This function will be part of a larger select which is why I need the value(s) returned as a single column - how would I loop through the table var to create the string? That would put me in the same situation as my initial question
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 38794148
this should do:

CREATE FUNCTION [dbo].[GetRelatedRecords] (@id bigint)
returns varchar(250)
as
BEGIN
declare @results varchar(250)
SELECT @results = coalesce(@results + '|' , '' ) + 'Related Record: ' + R.recordid
FROM RECORDS R
left join LINKS L
      ON R.id = L.LinkID
WHERE L.id = @id
RETURN @results
END

Open in new window

0
 
LVL 35

Expert Comment

by:David Todd
ID: 38795997
Hi QPR,

The functionality angelIII used is known as the Quirky Update. (You can do this in an update as well.

Sort answer is that the @results = ... expression is evaluated for each row and produces a delimited string with the column results.

HTH
  David
0
 
LVL 29

Author Closing Comment

by:QPR
ID: 38802687
Excellent (as always)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question