2

I try to create #temp table from another #temp table then it's through the error.

 Set @Query = 'Select Work_Order_No,ServiceCode,StageNo,ItemWeight,StagePercentage,FebLocation 
    INTO #TempMaster
    FROM #Temp '
EXEC(@Query)

Above query throw the error

Invalid object name '#TempMaster'.

and if I execute this query

Set   @Query = 'Select Work_Order_No,ServiceCode,StageNo,ItemWeight,StagePercentage,FebLocation 
    INTO ##TempMaster
    FROM #Temp '

EXEC(@Query)

Then it's working fine. What is different between these two statement. What is reason that first query throw error and second query run successfully.

5
  • 3
    The code you posted does not throw that error SQL Fiddle. Maybe the error was coming from another line you haven't shown us. Commented Jun 3, 2013 at 9:17
  • First query throw the error and second one execute successfully Commented Jun 3, 2013 at 9:18
  • both query running fine Commented Jun 3, 2013 at 9:20
  • Sorry to all I update the query Commented Jun 3, 2013 at 9:22
  • So it is not the dynamic query execution that throws the error, right? The problem happens after EXEC(@Query), specifically when you are attempting to read from the table specified in the dynamic query's INTO clause. Commented Jun 4, 2013 at 8:57

4 Answers 4

3

Following the edit the difference is that local #temp tables created in a child scope are automatically dropped when the scope exits and are not visible to the parent scope. ## prefixed tables are global temporary tables and not dropped automatically when the scope exits. Instead they are dropped when the creating connection is closed and no other connection is currently accessing it.

There is no apparent requirement to use EXEC here anyway so you could avoid this issue by using the code you originally posted.

SELECT Work_Order_No,
       ServiceCode,
       StageNo,
       ItemWeight,
       StagePercentage,
       FebLocation
INTO   #TempMaster
FROM   #Temp 
Sign up to request clarification or add additional context in comments.

1 Comment

@SaroopTrivedi - Well in that case you need a global ##temp table then as the local one you create in the child scope will be automatically dropped. Or you need to add all code using the table to the same child scope dynamic query. It is actually possible to create a local #temp table in the parent scope then ALTER in a child scope but that is very messy.
0

Try this one -

DECLARE @SQL NVARCHAR(MAX)

SELECT @SQL = '
IF OBJECT_ID (N''tempdb.dbo.##TempMaster'') IS NOT NULL
   DROP TABLE ##TempMaster

SELECT Work_Order_No,ServiceCode,StageNo,ItemWeight,StagePercentage,FebLocation 
INTO ##TempMaster
FROM #Temp'

EXEC sys.sp_executesql @SQL

2 Comments

My ##TempMaster working fine I just want to know why #TempMaster throw error
Please see @Martin Smith's answer.
0

Do like this:

set @Query = 'select Work_Order_No,ServiceCode,StageNo,ItemWeight,StagePercentage,FebLocation 
INTO #TempMaster1
FROM tablename

select * into #t from #TempMaster1

drop table #TempMaster1
drop table #t '

exec (@Query)

Comments

0

If you create #temp table inside dynamic query it is only valid inside that dynamic query and you can not use it later after dynamic query has finished executing.

If you really need to fill your table dynamically - you should create it up front.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.