Solved

Fixing Temp Table inside dynamic query

Posted on 2016-10-08
4
88 Views
Last Modified: 2016-10-09
I had this question after viewing Create table dynamically while passing the table name.

Hi Experts,

How to fix this error in below query:
Invalid object name '#temp1'.


DECLARE @FindDuplicatedRowsInAsIsTableSQL varchar (MAX);
SET @FindDuplicatedRowsInAsIsTableSQL = N'SELECT
	 ID
	,Name
	,COUNT(*) MYCount
INTO #temp1
FROM MyTable
GROUP BY
	ID
	,Name
HAVING COUNT(*)>1'
EXECUTE(@FindDuplicatedRowsInAsIsTableSQL)

---Insert Duplicated Rows in AsIs_DupicatedRowsTable
DECLARE @InsertDuplicatedRowsIn_AsIsDupicatedRowsTableSQL varchar (MAX);
SET @InsertDuplicatedRowsIn_AsIsDupicatedRowsTableSQL = N'INSERT INTO Xtable
SELECT * 
FROM #temp1';
EXECUTE(@InsertDuplicatedRowsIn_AsIsDupicatedRowsTableSQL)

Open in new window




Thanks a lot in advance.
Harreni
0
Comment
Question by:Harreni
4 Comments
 
LVL 12

Accepted Solution

by:
Nakul Vachhrajani earned 250 total points
ID: 41834979
The problem here is the scope of the temporary table. Since it is a local temporary table, it effectively goes out of scope when the EXECUTE on the dynamic SQL completes.

To fix this you need to either convert it to a global temporary table, or change the implementation to something similar to the following (basically I try to group the operations in a single query):

DECLARE @FindDuplicatedRowsInAsIsTableSQL varchar (MAX);
SET @FindDuplicatedRowsInAsIsTableSQL = N'SELECT
	 ID
	,Name
	,COUNT(*) MYCount
INTO #temp1
FROM MyTable
GROUP BY
	ID
	,Name
HAVING COUNT(*)>1

INSERT INTO Xtable
SELECT ID, Name, MyCount
FROM #temp1
'
EXECUTE(@FindDuplicatedRowsInAsIsTableSQL)

SELECT * FROM dbo.Xtable;

Open in new window

0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 150 total points
ID: 41834999
You dont need temp table. Why complicate the process. Use below.

DECLARE @FindDuplicatedRowsInAsIsTableSQL varchar (MAX) = N'
INSERT INTO Xtable
SELECT
	 ID
	,Name
	,COUNT(*) MYCount
FROM MyTable
GROUP BY
	ID
	,Name
HAVING COUNT(*)>1'
EXECUTE(@FindDuplicatedRowsInAsIsTableSQL)

SELECT * FROM dbo.Xtable;

Open in new window



Even you dont need the dynamic string also.

INSERT INTO Xtable
SELECT
	 ID
	,Name
	,COUNT(*) MYCount
FROM MyTable
GROUP BY
	ID
	,Name
HAVING COUNT(*)>1

SELECT * FROM dbo.Xtable;

Open in new window


Enjoy !!
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 41835133
Without going into specifics, why are you creating temp tables using Dynamic SQL?   That's about as dangerous as the Danger Committee, mostly because as Nakul said the scope of the temp table begins when the statement is executed. Seems like a better way is possible such as a CTE.
0
 

Author Closing Comment

by:Harreni
ID: 41835604
Thanks a lot Experts.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question