Another trick is to use OPENQUERY
. From my answer here:
It requires the use of OPENQUERY
and a loopback linked server with the 'DATA ACCESS'
property set to true. You can check sys.servers
to see if you already have a valid server, but let's just create one manually called loopback
:
EXEC master..sp_addlinkedserver
@server = 'loopback',
@srvproduct = '',
@provider = 'SQLNCLI',
@datasrc = @@SERVERNAME;
EXEC master..sp_serveroption
@server = 'loopback',
@optname = 'DATA ACCESS',
@optvalue = 'TRUE';
Now that you can query this as a linked server, you can use the result of any query (including a stored procedure call) as a regular SELECT
. So you can do this (note that the database prefix is important, otherwise you will get error 11529 and 2812):
SELECT * FROM OPENQUERY(loopback, 'EXEC db.dbo.procedure;') AS x;
Now you can join to your view.
But I honestly feel it will be better if you re-write the procedure as a TVF and stop using #temp tables in the logic. The above might work on your current instance but it won't work in SQL Server 2012 (metadata can't be determined because of the #temp table in the procedure) and it won't work if you have certain database- or server-level DDL triggers either (for the same type of reason).
Also see http://www.sommarskog.se/share_data.html#OPENQUERY for other info and limitations.