Home > Archive > SQL Anywhere database > April 2005 > Cursor with updates - Urgent









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cursor with updates - Urgent
Chandhu

2005-04-22, 8:23 pm

Hi,
We are using sybase 8.0.2.4308.
I need some help on cursors. Is this cursor having any problem?


alter procedure DBA.test()
begin
declare CurTable1 dynamic scroll cursor for select A from Dba.Table1;
declare t1_a bigint;
declare j integer;
set j=0;
Open CurTable1;
Fetch next CurTable1 into t1_a;
while @@Sqlstatus = 0
Loop
Fetch next CurTable1 into t1_a;
-----IS it correct ..... here i want to update another column
Update "Table1" set b = t1_a+10;
-- Can we update any other table ..like
update table2 set c=t1_a-20 where a=t1_a;
end loop;
close curtable1;
end

My problem is I want to update some of the table & the same in the
cursor...? can we do that?


Thanks..

Chandhu.


Reg Domaratzki \(iAnywhere Solutions\)

2005-04-25, 9:23 am

Of course you can. Did you try it? The following works great for me :

create table t1 ( pkey integer default autoincrement primary key, c1
integer );
create table t2 ( pkey integer default autoincrement primary key, c1
integer );

insert into t1(c1) values ( 1 );
insert into t1(c1) values ( 2 );
insert into t1(c1) values ( 3 );
insert into t1(c1) values ( 4 );

insert into t2(c1) values ( 1 );
insert into t2(c1) values ( 2 );
insert into t2(c1) values ( 3 );
insert into t2(c1) values ( 4 );

create procedure DoTest ()
begin
declare MyCur dynamic scroll cursor for select pkey from t1;
declare c_pkey integer;
open MyCur;
fetch first MyCur into c_pkey;
while( sqlcode = 0 ) loop
update t1 set c1 = 10 + c_pkey;
update t2 set c1 = c_pkey - 20 where pkey = c_pkey;
fetch next MyCur into c_pkey;
end loop;
close MyCur;
end;

call DoTest;

select * from t1;
Result :
pkey c1
-------
1 14
2 14
3 14
4 14

select * from t2;
Result :
pkey c1
--------
1 -19
2 -18
3 -17
4 -16

--
Reg Domaratzki, Sybase iAnywhere Solutions
Sybase Certified Professional - Sybase ASA Developer Version 8
Please reply only to the newsgroup

iAnywhere Developer Community : http://www.ianywhere.com/developer
iAnywhere Documentation : http://www.ianywhere.com/developer/product_manuals
ASA Patches and EBFs : http://downloads.sybase.com/swx/sdmain.stm
-> Choose SQL Anywhere Studio
-> Set "Platform Preview" and "Time Frame" to ALL

"Chandhu" <chandrasekhars@i-vantage.com> wrote in message
news:426997af@forums
-2-dub...
> Hi,
> We are using sybase 8.0.2.4308.
> I need some help on cursors. Is this cursor having any problem?
>
>
> alter procedure DBA.test()
> begin
> declare CurTable1 dynamic scroll cursor for select A from Dba.Table1;
> declare t1_a bigint;
> declare j integer;
> set j=0;
> Open CurTable1;
> Fetch next CurTable1 into t1_a;
> while @@Sqlstatus = 0
> Loop
> Fetch next CurTable1 into t1_a;
> -----IS it correct ..... here i want to update another column
> Update "Table1" set b = t1_a+10;
> -- Can we update any other table ..like
> update table2 set c=t1_a-20 where a=t1_a;
> end loop;
> close curtable1;
> end
>
> My problem is I want to update some of the table & the same in the
> cursor...? can we do that?
>
>
> Thanks..
>
> Chandhu.
>
>



Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com