Solved

Fixing Temp Table inside dynamic query

Posted on 2016-10-08
4
93 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 13

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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