Link to home
Start Free TrialLog in
Avatar of Harreni
HarreniFlag for Saudi Arabia

asked on

Fixing Temp Table inside dynamic query

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
ASKER CERTIFIED SOLUTION
Avatar of Nakul Vachhrajani
Nakul Vachhrajani
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Harreni

ASKER

Thanks a lot Experts.