Solved

Join the output of a stored procedure with a table

Posted on 2013-11-29
3
349 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
3 Comments
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 200 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 300 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
query execution hang 5 29
SQL SELECT query help 7 37
What's wrong with this T-SQL Foreign Key? 7 43
Tsql query 6 21
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

776 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