Solved

Join the output of a stored procedure with a table

Posted on 2013-11-29
3
357 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 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

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…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

696 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