Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE PROCEDURE MySP
(
@Type INT
)

DECLARE @strSQL AS VARCHAR(4000)

IF EXISTS (SELECT table_name FROM tempdb.information_Schema.tables WHERE
table_name = '##MyTmpTable')

DROP TABLE ##MyTmpTable

SELECT @strSQL = 'SELECT MyField1, MyField2, MyField3 INTO ##MyTmpTable FROM
MyTable';

EXECUTE(@strSQL)


How to retrive the data from (@strSQL).

Otherwise how to eliminate global temp table.
Posted
Updated 24-Oct-13 1:35am
v2

Try executing the given below way -

SQL
CREATE PROCEDURE MySP
(
@Type INT
)
as
begin
DECLARE @strSQL AS VARCHAR(4000)

IF EXISTS (SELECT table_name FROM tempdb.information_Schema.tables WHERE
table_name = '##MyTmpTable')

DROP TABLE ##MyTmpTable

SELECT @strSQL = 'SELECT MyField1, MyField2, MyField3 INTO ##MyTmpTable FROM
MyTable';

EXECUTE(@strSQL)

SELECT @strSQL = 'SELECT * FROM  ##MyTmpTable';
EXECUTE (@strSQL)
end
 
Share this answer
 
You can query down global temp table after executing @strsql

SQL
Select * FROM ##MyTmpTable


If you want to access "##MyTmpTable" data globally use global temp table otherwise use CTE (Common Table Expressions) that can be useful in the query window only so that it will reduce the memory and stress on the database and sql server
 
Share this answer
 
hi,

You can do it by adding select statement at end of procedure as below


SQL
CREATE PROCEDURE MySP
(
@Type INT
)

DECLARE @strSQL AS VARCHAR(4000)

IF EXISTS (SELECT table_name FROM tempdb.information_Schema.tables WHERE
table_name = '##MyTmpTable')

DROP TABLE ##MyTmpTable

SELECT @strSQL = 'SELECT MyField1, MyField2, MyField3 INTO ##MyTmpTable FROM
MyTable';

EXECUTE(@strSQL)
select * from ##MyTmpTable


if it helps please mark it as answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900