Solved

Fixing Temp Table inside dynamic query

Posted on 2016-10-08
4
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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 66

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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