cursor in SPL
Posted in 2000
Topics: Stored Procedures & SPL
Hello everyone, I am new to the list and this is my first post. I would like to write a stored procedure that loops through a cursor and compares each consecutive value with the next value in the cursor. Something like this: define p_doc_id, p_doc_id_2 integer; foreach select document_id into p_doc_id from tablename order by document_id fetch next into p_doc_id_2 if (p_doc_id + 1) <> p_doc_id_2 then do something end foreach; What I am not sure about is how to move the cursor in SPL to the next record. Thank you, Ben Partlow benpartlow@yahoo.com __________________________________________________ Do You Yahoo!? Yahoo! Photos -- now, 100 FREE prints! http://photos.yahoo.com
FOREACH implicitly loops through the rows SELECTed. You could do something similar to the following : define p_doc_id, p_old_doc_id integer; let p_old_doc_id = 0; -- Initialize foreach select document_id into p_doc_id from tablename order by document_id if p_doc_id <> p_old_doc_id then do something; end if; -- Set up p_old_doc_id for next iteration let p_old_doc_id = p_doc_id; end foreach; Rudy Benjamin Partlow wrote: > I would like to write a stored procedure that loops > through a cursor and compares each consecutive value > with the next value in the cursor. Something like > this: > > define p_doc_id, p_doc_id_2 integer; > > foreach select document_id > into p_doc_id > from tablename > order by document_id > > fetch next into p_doc_id_2 > > if (p_doc_id + 1) <> p_doc_id_2 then do > something > > end foreach; > > What I am not sure about is how to move the cursor in > SPL to the next record. Thank you, > > Ben Partlow > benpartlow@yahoo.com