2

I have a temp table like

DECLARE @t TABLE (a BIGINT)

I want to do this

SELECT * FROM @t

but I need to do it by EXEC query, and this doesn't work

DECLARE @query AS NVARCHAR(100) = 'SELECT * FROM @t'
EXEC(@query)

how can I create a custom query to select the temp table?

Thanks.

4
  • 2
    You can replace temp table with hash table i.e (#t) Commented Dec 29, 2016 at 7:05
  • hi, i tried to apply hashTable, it looks worked. but then I need to drop the table everytime after the trigger is finished (the query ran within a trigger), is it correct? thanks Commented Dec 29, 2016 at 7:22
  • yes, you have to do that because it will create in memory Commented Dec 29, 2016 at 7:23
  • ok thanks., then i will keep implementing in this aspect first. they looks more readable to me. Commented Dec 29, 2016 at 7:25

3 Answers 3

2

This is because EXEC statement will execute the Statements in new Session. And the Table variables scope is fixed to the batch of statement.

Since you declared the Table Variable out side of the Session , you can't access the table variable in EXEC statement.

So you need to DECLARE ,INSERT, UPDATE, SELECT table variables in the Dynamic Code itself.

DECLARE @query NVARCHAR(MAX)='';
SELECT @query ='
DECLARE @t TABLE
        (
            a BIGINT
        )'

SELECT  @query += 'SELECT * FROM @t'
EXEC(@query)

Solution 2:

The another Solution is to create Global Temporary Table which we can create using ##. And Global Temporary tables scope is limited to Database all connections.

CREATE TABLE ##TABLE1
    (
        a BIGINT
    )
DECLARE @query NVARCHAR(MAX)='';
SELECT  @query += 'SELECT * FROM ##TABLE1'
EXEC(@query)

But be aware if another user execute the same Query, there might be conflict with the same name creation.

Sign up to request clarification or add additional context in comments.

Comments

1

Try hash table as below:

DECLARE @tblTest AS Table
(
    Name VARCHAR(50) 
)

insert into @tblTest values('Sandip')
insert into @tblTest values('AAA')

IF OBJECT_ID('tempdb..#tblTest') IS NOT NULL
    DROP TABLE #tblTest

SELECT * INTO #tblTest FROM @tblTest
DECLARE @query AS NVARCHAR(100) = 'SELECT * FROM #tblTest'

EXEC (@query)

Output:

enter image description here

2 Comments

i found that i don't need to create #tblTest at all, where can i find the detail information about using #hashTable? can you tell me where I can find more information of it about how it is stored and how it actually work. Thanks.
search for : sql server hash table vs table variable
0
CREATE TYPE MyTable AS TABLE 
( 
 a BIGINT
);
GO


DECLARE @T AS MyTable;

EXEC sp_executesql
  N'SELECT * FROM @T',
  N'@T MyTable READONLY',
  @T=@T 

please try this

12 Comments

It will create MyTable in database, which not required
it will not create table,It will make a table type.
@SandipPatel : please read answer carefully and than vote it down.
then can you please try to execute same statement in other window and see exception, obvious it create table as you have used create statement not declarative one
I don't want theory logic, just run your code in other window it need to be work there without any error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.