0

I want to combine SQL query with temp table data like below. My sql query is

declare @temp table(Id int)
insert into @temp values(10)
insert into @temp values(20)

declare @SQL nvarchar(max)=''
set @SQL='select * from nomination where id in (select id from @temp)'
exec (@sql)

when execute above query, it throws error as Must declare the table variable "@temp". can any help to solve this?

Lalitha
  • 67
  • 1
  • 11
  • you can use temprorary table : create table #temp(id int) insert into #temp values(10) insert into #temp values(20) declare @SQL nvarchar(max)='' set @SQL='select * from nomination where id in (select id from #temp)' exec (@sql) – nimajv Nov 20 '21 at 08:26
  • Why are you using "dynamic" SQL at all here; it's not dynamic... – Thom A Nov 20 '21 at 12:06

0 Answers0