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)
 Syntax error is driving me crazy!

Author  Topic 

techdawg270
Starting Member

4 Posts

Posted - 2011-09-15 : 11:54:07
This code is placed within a cursor, and I have tried a few different variations to no avail. Just trying to set a variable using a dynamically generated statement
declare @maxId int;
set @maxId = exec('select max(uid) from ' + @table_name);

quote:
Incorrect syntax near the keyword 'exec'

Any ideas?

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2011-09-15 : 12:10:58
putting a text string into an "int" variable?

declare @maxid int
declare @sql string
@sql = 'select max(uid) from ' + @table_name
exec @sql
if the exec works, then your issue is to do with assigning the result into the int variable.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-15 : 13:50:37
if you want to return the result in a variable inside dynamic string, you need to use sp_executesql instead of exec.

see
http://msdn.microsoft.com/en-us/library/ms188001(v=SQL.90).aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -