<wkraatz@csc.comwrote in message
news:1132331179.811469.269660@
deepak.rathore@gmail.com wrote:
I am trying to execute a dynamic query as follows
begin
declare @tablename char(46)
declare @query char(1000)
declare @Age int
select @tablename='People'
select @query='select @Age = age from '+@tablename +' where name=xyz'
exec (@query)
end
This is giving error :
QLException :: (Must declare variable '@Securityid'., sqlcode=137,
sqlstate=ZZZZZ)
Am i doing something wrong or such thing is not possible.
Thanks
The dynamic query acts as a mini-stored procedure. Your @Age variable
is not local to that procedure.
Note that as of 15.0 (as well as the upcoming 12.5.4), this has changed: as
of these versions, you *can* access local variables declared outisde the
exec-immediate from inside the exec-immediate.
HTH,
Rob
Rob Verschoor
Certified Sybase Professional DBA for ASE 12.5/12.0/11.5/11.0
and Replication Server 12.5 / TeamSybase
Author of Sybase books (order online at www.sypron.nl/shop):
"Tips, Tricks & Recipes for Sybase ASE"
"The Complete Sybase Replication Server Quick Reference Guide"
"The Complete Sybase ASE Quick Reference Guide"
mailto:rob@YUR.SPAM.sypron.nl.NT.FR.ME
http://www.sypron.nl
Sypron B.V., PBox 10695, 2501HR Den Haag, The Netherlands