user3353623
user3353623

Reputation: 9

Inserting data from a Exec clause

Any idea why I keep getting the error that #temp2 is an invalid object?

I am trying to populate this table from a dynamic query that I am executing with EXEC command:

set @sql = 'Select DFRId, FiscalDate, Franchise_Number, Store_Number, Reviewer, '+@listStr+' from (Select DFRId, Reviewer, FiscalDate, Franchise_Number, Store_Number, Question, Score from #temp) as sourcetable Pivot (Max(Score) for Question in ('+@listStr+')) as pivotable order by Franchise_Number,Store_Number,FiscalDate  '

Insert #temp2 exec(@Sql)

Upvotes: 1

Views: 63

Answers (2)

Hasib Tarafder
Hasib Tarafder

Reputation: 5813

At first, make sure that your temporary table #temp2 is created.

Next, check the following code.

I hope your problem will be solved..

set @sql = 'INSERT INTO #temp2 
Select DFRId, FiscalDate, Franchise_Number, Store_Number, Reviewer, '+@listStr+' from (Select DFRId, Reviewer, FiscalDate, Franchise_Number, Store_Number, Question, Score from #temp) as sourcetable Pivot (Max(Score) for Question in ('+@listStr+')) as pivotable order by Franchise_Number,Store_Number,FiscalDate'

exec(@Sql)

Upvotes: 1

Necktru
Necktru

Reputation: 1

There are two errors, at first you must to include the into #temp2 clause before the FROM and inside the string. If you want to use the insert into #temp2, your table must to be defined previously

Upvotes: 0

Related Questions