6

My problem is using a table variable in a exec.

declare @sort_col nvarchar(1000) = 'itm_id'
declare @sort_dir nvarchar(4) = 'desc'
declare @filters nvarchar(1000) = ' and itm_name like ''%aa%'''

declare @temp table
(
 itm_id int
)

insert into @temp
EXEC('select itm_id from Tblitm where itm_name not like ''%aa%''')

EXEC('select * from (select (ROW_NUMBER() OVER (ORDER BY '+@sort_col+' '+@sort_dir+')) row_num, * FROM (select itm_id, itm_name, 
dbo.fnItmsHistory(itm_id) itm_history
         from dbo.Tblitm as itm
         left outer join '+@temp+' as temp on itm.itm_id = temp.itm_id
         where itm_id=itm_id and temp.itm_id = null '+@filters+') as x) as tmp')

It says Must declare the scalar variable "@temp" when the temp table is declared i tried using original temp table and it worked, but i had problems when trying to update my entity model.So is there any solution for this problem?

Note: I must use exec because in filters i store string for the where clause.

6
  • 2
    Use #temp instead of @temp and just reference #temp directly in the EXEC() instead of treating it like a variable name. Commented May 20, 2013 at 16:33
  • See Dynamic insert into variable table statement SQL Server Commented May 20, 2013 at 16:38
  • @AaronBertrand i made the query functional like that, but i had problems when updating my entity model so i am afraid i cant use that fix. Commented May 20, 2013 at 16:55
  • I have the exact problem when using temp table. stackoverflow.com/questions/16593473/… Commented May 20, 2013 at 17:36
  • Wow, EF is pretty annoyingly intrusive, eh? In theory, it should be able to determine the output of the stored procedure without caring what comes before the first select. Out of curiosity, what happens if you encrypt the stored procedure? Commented May 20, 2013 at 19:07

2 Answers 2

1

Try moving the table variable inside the dynamic statement.

EXEC('
declare @temp table
(
 itm_id int
)
insert into @temp
select itm_id from Tblitm where itm_name not like ''%aa%''
select * from (select (ROW_NUMBER() OVER (ORDER BY '+@sort_col+' '+@sort_dir+')) row_num, * FROM (select itm_id, itm_name, 
dbo.fnItmsHistory(itm_id) itm_history
         from dbo.Tblitm as itm
         left outer join @temp as temp on itm.itm_id = temp.itm_id
         where itm_id=itm_id and temp.itm_id = null '+@filters+') as x) as tmp')
Sign up to request clarification or add additional context in comments.

3 Comments

This is a solution that works, but i cant use this as a solution it slows the query down significantly.
@Aleks Why do you need a temp table anyway, why not join with the sub-query select itm_id from Tblitm where itm_name not like ''%aa%'' directly instead?
the query i wrote is an example i have a lot more complicated query and when i put it all in exec it takes a lot more time than using a temp table.
0

For solution i had to use a temp table and then on the start of my stored procedure i used the if condition from the EF can't infer return schema from Stored Procedure selecting from a #temp table anwser.

It's the best solution for this scenario i think.

Comments

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.