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.
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 statementdeclare @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 intdeclare @sql string@sql = 'select max(uid) from ' + @table_nameexec @sqlif the exec works, then your issue is to do with assigning the result into the int variable. |
 |
|
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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|