?
Solved

Join the output of a stored procedure with a table

Posted on 2013-11-29
3
Medium Priority
?
358 Views
Last Modified: 2013-11-29
I have created a procedure that splits a multi-line street address like
Suite 1
Tower 2
345 Sixth Street
into Address1, Address2, Address3.

The inputs to the procedure are @EntityId and @CompoundAddress

I want to join the result with a temporary table that contains
EntityId, City, StateOrProvince, PostalCode, Country, and the compound address

I want to create a SELECT statement that joins the two tables and presents all the fields

Can someone help me figure out how to do that?

Here is the code I wrote that SQL Server doesn't like:
SET @ThisRow = 14
WHILE @ThisRow <= 14
BEGIN
SET @InAddress = (SELECT BillingStreet FROM @Source WHERE RowNum = @ThisRow)
SELECT A.Address2, A.Address2, A.Address3, E.City, E.StateProvince, E.PostalCode, E.Country FROM
@Source AS E INNER JOIN
(EXEC upSplitCompoundAddress @EntityId, @InAddress) AS A ON A.EntityId = E.EntityId
SET @ThisRow = @ThisRow + 1
END
0
Comment
Question by:Douglass MacLean
[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
3 Comments
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 800 total points
ID: 39685309
either you need to write the "procedure" as a function to split the address into its component address lines outputting as a table... and join to that

or else the split code should be placed directly into the underlying sql...

i don't find your example code very illuminating...

the other way is to execute your stored procedure capturing the output to a temp table
(or writing to the temp table in the sp)  and then join to that ...

you can join to a stored procedure directly... as you're attempting to do...
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1200 total points
ID: 39686191
For best performance, re-write the procedure code as an inline-table-valued function, then use CROSS APPLY to get its results.  For example, something like this:


SELECT
    A.Address2, A.Address2, A.Address3,
    E.City, E.StateProvince, E.PostalCode, E.Country
FROM @Source AS E
CROSS APPLY dbo.upSplitCompoundAddress ( E.EntityId, E.BillingStreet ) AS A
0
 

Author Closing Comment

by:Douglass MacLean
ID: 39686371
Thanks to you both. I appreciate the good advice.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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