SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to inner join a result set from stored procedure without creating temp table


How to inner join a result set from stored procedure without creating temp table

Author
Message
halifaxdal
halifaxdal
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4628 Visits: 1768
Hello,

I wonder how can I inner join the running result from my sp in my sql query:
e.g. select * from tableA A inner join (result of stored procedure) B on A.ID = B.ID

I might be able to create a temp table and dump the result of sp over there, do I have to use temp table? or there is an easy way to approach this?


Thanks.
Aaron Ingold
Aaron Ingold
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2647 Visits: 222
You're going to want to dump it into a temporary table, table variable or if possible change the stored procedure to be a table-valued function (not always possible). Then you can join to it.
RonKyle
RonKyle
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7002 Visits: 3621
Dumping it into a temp table isn't hard, but you could also consider using derived tables. The derived table is calculated within your parenthesis. I've included a link to a page for you to review. http://www.sqlteam.com/article/using-derived-tables-to-calculate-aggregate-values



halifaxdal
halifaxdal
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4628 Visits: 1768
Thank you guys for responding.

I am having more and more trouble:

To do an experiment, I created a temp table, and tried to inner join that table. Unfortunately that sp is on a linked server, it seems there is some restriction on getting data from a linked server, here is the error I encountered:
Query:
create table #t(observanceid int, observancetime datetime, observancetype int)
insert #t(observanceid, observancetime, observancetype) exec [MyServer].MyDB.dbo.listdata

Error1: MSDTC on server 'MyServer' is unavailable

Following a suggestion from http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx

I got error 2:
The operation could not be performed because the OLE DB provider 'SQLOLEDB' was unable to begin a distributed transaction.
[OLE/DB provider returned message: New transaction cannot enlist in the specified transaction coordinator. ]
OLE DB error trace [OLE/DB Provider 'SQLOLEDB' ITransactionJoin::JoinTransaction returned 0x8004d00a].

What the hell is going on? How can I resolve this issue?

All I need is:

I need to do some data analysis on a production server, so I don't have write permission to create sp directly on that server. Instead, what I am thinking is:

1. link the server to my shell db/server
2. write sp on my shell db/server

I have new tables created on my shell db/server, which I will need to join with data result returned from the sp on the linked server.



Or, considering the frequency of polling data, is there a way to create a kind of "back up" or view on shell db/server using that stored procedure on the linked server?

That's all my tasks.

Thank you very much.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search