?
Solved

Stringing results together

Posted on 2013-01-17
5
Medium Priority
?
205 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

800 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