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 2005 Forums
 Transact-SQL (2005)
 Exec Query not working

Author  Topic 

ryanlcs
Yak Posting Veteran

62 Posts

Posted - 2009-09-08 : 20:45:48
Hi

I got 2 Set of command:

Statement 1:
Select @result = Count(Rec_Id) FROM Table1 Where Rec_Id = 2
Update #Tmp_Tbl_Table2 Set Series_Value1 = @result

Statement 2:
Exec(@Series_Formula1)
Update #Tmp_Tbl_Table2 Set Series_Value1 = @result

In statement 2, the variable @Series_Formula1 is assigned with
"Select @result = Count(Rec_Id) FROM Table1 Where Rec_Id = 2" (retrieve from DB), which is exactly same as statement 1 line 1, somehow, it says that "Must declare the scalar variable "@result"".

Please help.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-08 : 20:48:47
You haven't shown us your entire code since we don't see the DECLARE in there. I would imagine you have a GO in there which means your local variables are gone.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ryanlcs
Yak Posting Veteran

62 Posts

Posted - 2009-09-08 : 20:57:04
Of course I have my variable declaration and GO command, cos the problem now is that the differences between the 2 statement I posted. So, I would focus on those 2 statement.
Go to Top of Page

asgast
Posting Yak Master

149 Posts

Posted - 2009-09-09 : 09:19:35

1 if you have DECLARE @result ........ 1st statement ................. GO ...........2nd statement......... the result variable is gone
2 your dynamic sql stored in @Series_Formula1 doesn't know the scalar variable @result
if you have @Series_Formula1 = 'Select @result = Count(Rec_Id) FROM Table1 Where Rec_Id = 2'
and then you do EXEC(@Series_Formula1) this will never work
search BOL for sp_exectesql
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-09 : 13:09:52
I don't think you understood my reply about the GO statement.

Post the entire code so that we can help. Your issue is almost certainly because of GO.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -