|
|
Question : Sybase declare cursor
|
|
Wondering whether anyone could help out with a portion of a stored procedure I am having trouble with ?
This is an example of a stored procedure I am trying to execute (this one works fine no errors:
create proc sp_tst1 as DECLARE @var1 char(10) DECLARE @message varchar(80)
DECLARE title_curs cursor for SELECT var1 from myTable where key = 3359 open title_curs
fetch title_curs into @var1
if (@@sqlstatus = 2) begin print "no key found" close title_curs deallocate cursor title_curs return end
while (@@sqlstatus = 0) begin select @message = ' this is the key ' +@var1 print @message fetch title_curs into @var1 end
close title_curs deallocate cursor title_curs
But I want to make the SELECT statement dynamic say :
create proc sp_tst1 as DECLARE @var1 char(10) DECLARE @message varchar(80) DECLARE @tableName char(30)
select @tableName = 'secdb..financial'
EXEC ("DECLARE title_curs cursor for SELECT var1 from " +@tableName+" where key = 3359 open title_curs")
fetch title_curs into @var1
if (@@sqlstatus = 2) begin print "no key found" close title_curs deallocate cursor title_curs return end
while (@@sqlstatus = 0) begin select @message = ' this is the key' +@var1 print @message fetch title_curs into @var1 end
close title_curs deallocate cursor title_curs
But when I run this I get :
The cursor 'title_curs' can not be used as it could not be found. It is possible that either it was not declared or it is not available in the current context. (return status = -6) (1 row affected)
I tried using local and global in front of keyword cursor but I believe that is Microsoft SQL syntax. What would I do for Sybase 11.0
Thanks!
|
Answer : Sybase declare cursor
|
|
this is my solution:
create proc sp_test as declare @var1 char(10), @message varchar(80), @tableName char(30)
select @tableName = 'mytable'
declare title_curs cursor for select fld1 from ' +@tableName+ 'where fld1 = 3273 for read only /* this is change... regards */
open title_curs
while (1=1) begin fetch title_curs into @var1
if @@sqlstatus = 0 begin print 'no more records' break end
select @message = 'this is the key ' + @var1 print @message end
close title_curs return 0
|
|
|
|