简体   繁体   中英

SQL Invalid object name for temp table with openquery

SQL query is as below:

DECLARE @Sql VARCHAR (8000)
SET @Sql='XXXXXX'

IF OBJECT_ID('tempdb..#Orders') IS NOT NULL DROP  TABLE #Orders
EXEC('SELECT * INTO  #Orders FROM OPENQUERY(TMM10, ''' +@Sql+ ''')')  
--Until now everything seems OK

SELECT * FROM  #Order

This statement runs well until insertion for temp table. Result shows "49134 rows have been inserted". However, error shows "invalid object name #Order", which does exist in Tempdb.

Scope issue; I modified your test to demonstrate a way around it.

DECLARE @Sql VARCHAR (8000)
SET @Sql='XXXXXX'

IF OBJECT_ID('tempdb..#Orders') IS NOT NULL DROP  TABLE #Orders
CREATE TABLE #Orders (ID INT)

INSERT INTO #Orders
        ( ID )

EXEC('SELECT 1')  
--Until now everything seems OK

SELECT * FROM  #Orders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM