|
|
Question : execute-immediate facility in sybase
|
|
Hi I am trying to use execute-immediate facility of sybase. I am not much knowledge about execute-immediate.
Actually i am trying to find out how many rows are there in all user tables starting with name 'HIN'.
FOr this i made a unix script containging:
isql -Urwnzlrep -Pncs123jp -n -b -c <declare test cursor for select name from sysobjects where type ='U' and name like '%HIN%'
set nocount on open test declare @caid varchar(100) declare @tost char(10) declare @ctost char(10) fetch test into @caid exec('select '+ @tost + '=count(*) from '+@caid) select @ctost=convert(char(10),@tost) print "total number of rows in table %1! is %2!",@caid,@ctost declare @res int declare @rest char(5) select @res=@@sqlstatus while(@res = 0) begin select @rest=convert(char(5),@res) --print @rest fetch test into @caid select @res=@@sqlstatus if(@res = 0) begin print "total number of rows in table %1! is ",@caid exec('select count(*) from '+@caid) end end close test
! Now when i run this script, i gets error: Msg 102, Level 15, State 1: Server 'SYBDEV12', Line 1: Incorrect syntax near 'select'. total number of rows in table HIN_HOLDER_ID_NUM is total number of rows in table SHD_SEC_HIN_DTLS is 0
Can anyone tell me what is wrong syntex near 'select'.Also it will be much apriciated if you provide some features and weakness of "execute-immediate' facility of sybase.
Regards
pattha
|
Answer : execute-immediate facility in sybase
|
|
Hi, can you try re-writing the script as follows?
declare test cursor for select name from sysobjects where type ='U' and name like '%HIN%' go
set nocount on open test declare @caid varchar(100) declare @tost char(10) declare @ctost char(10) declare @statement varchar(255) fetch test into @caid while @@sqlstatus = 0 begin
select @statement = 'declare @tost numeric(25,0)' + char(13) + 'select @tost =count(*) from '+@caid + char(13) + 'print "total number of rows in table ' + @caid + ' is %1!",@tost' + char(13) --+ 'GO' + char(13) -- select @statement exec(@statement) fetch test into @caid end go
close test deallocate cursor test go
Here we are using @caid in the parent connection to create the query. When we run exec() we also have to define any variable we use in the query itself. To analyse this solution you might want to add to uncomment the line before the exec(). Hope this helps, Cheers, Rij.
P.S. I have rewitten the cursor handling a bit more ;-)
|
|
|
|
|