I have the following code:
declare @cQuery varchar(2000);
declare @cTSQL varchar(2000);
declare @cLinkServer varchar(20);
declare @cTable varchar(20);
set @cQuery = 'SELECT iln.product_nbr FROM axs.pub.ivc_line iln';
set @cLinkServer = 'RBDAX1';
set @cTable = '#tempInv';
if OBJECT_ID('tempdb..#tempInv') is not null
drop table #tempInv
--SELECT * INTO #tempInv FROM OPENQUERY(RBDAX1,'SELECT iln.product_nbr FROM axs.pub.ivc_line iln');
set @cTSQL =
'SELECT * '
+ ' INTO #tempInv '
+ ' FROM OPENQUERY(RBDAX1,''SELECT iln.product_nbr FROM axs.pub.ivc_line iln'')'
;
exec ( @cTSQL );
When I call exec ( @cTSQL ); the temp table has not been created.
When I run the commented out line:
SELECT *
INTO #tempInv
FROM OPENQUERY(RBDAX1, 'SELECT iln.product_nbr FROM axs.pub.ivc_line iln');
the temp table is created just fine.
What am I missing here?
You cannot create a temp table this way. The temp table is created but then 'goes away' the minute the query ends. You can create a table and do an insert/select.