Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamic query with a Temp table

Author  Topic 

stormcandi
Starting Member

46 Posts

Posted - 2009-06-01 : 13:59:34
I have a dynamic query that I need to use to also reference a temp table. The temp table gets the most current year for each id passed in. I then need to query that table and get the data the most current year. The reason why the query is dynamic is because the fields being returned may vary.
They query will not run because of the Temp table in it, but I cannot figure out how to get it to work right.


Declare @TempTable Table
(
ScoreYear varchar(4),
ScoreSID varchar(15)
)
Declare @strSQL varchar(max)
--Inserts values into @TempTable
Insert Into @TempTable(ScoreYear, ScoreSID)
Select Max(ASY) as MaxYear, SID
From Scores
Where Scores.SID IN (Select * From SplitFunction(@SID,','))
Group By SID


Set @strSQL = 'SELECT Student.FName, Student.LName, Student.SID, ' + @Fields + ' FROM Scores inner join PUSD.dbo.Student ON Scores.SID = Student.SID inner join + ' @TempTable + ' Temp on Scores.SID = Temp.SID and Scores.ASY = Temp.ScoreYear Where SID IN (' + (Select * From SplitFunction(@SID, ',')) + ')'

Exec (@strSQL)



This part:
(Select * From SplitFunction(@SID, ',')
is a function that puts the ID into a table format so it can be queried.

PS The stored procedure is for Reporting Services.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-01 : 14:02:20
You are concatenating the table variable's name as a name of a table variable.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2009-06-01 : 14:18:29
I thought I was supposed to concantenate it since it is not a regular table? How should it be handled?

quote:
Originally posted by Peso

You are concatenating the table variable's name as a name of a table variable.



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-01 : 14:21:39
i think you need to put the results in a #table and use sp_executesql to execute the dynamic sql
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2009-06-01 : 14:40:11
Worked perfectly!


Thanks a bunch!

quote:
Originally posted by visakh16

i think you need to put the results in a #table and use sp_executesql to execute the dynamic sql

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 13:07:11
welcome
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-06-03 : 04:21:14
I really think you should read this if what you posted is representative of your production code:

http://www.sommarskog.se/dynamic_sql.html



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

stormcandi
Starting Member

46 Posts

Posted - 2009-06-03 : 09:24:52
Thank you! I will check it out!

quote:
Originally posted by Transact Charlie

I really think you should read this if what you posted is representative of your production code:

http://www.sommarskog.se/dynamic_sql.html



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION


Go to Top of Page
   

- Advertisement -