Solved

Fixing Temp Table inside dynamic query

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
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…

749 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