All Forums Database
23 May 2008
Cursors in teradata

Hi,I am having a stored procedure containing cursor declaration.I have fetched employee ID's in the cursor ..I need to loop through the cursor variable..If some one knows, please let me know.......Thanks,

Adeel Chaudhry 773 posts Joined 04/08
23 May 2008

Hello,You need to have a loop to do this, following is the example:DECLARE Var_EmployeeID INTEGER;DECLARE VarCount INTEGER;DECLARE Cur1 CURSOR FORSELECT Employee_ID FROM Table1 FOR READ ONLY;SELECT COUNT(*) INTO VarCount FROM Table1;IF VarCount <> 0 THEN --if you dont put this and the table is empty, you are in a never ending loop, alternatively you can use less than operator :) OPEN Cur1; Label1: Loop FETCH Cur1 INTO Var_EmployeeID; --do whatever you like here SELECT VarCount-1 INTO VarCount; IF VarCount = 0 THEN LEAVE Label1; End IF; END LOOP Label1; CLOSE Cur1;END IF;HTH.Regards,Adeel

-- If you are stuck at something .... consider it an opportunity to think anew.

Sahil Bhosale 1 post Joined 05/08
23 May 2008

Thanks a lot..

mdgmilen 1 post Joined 04/12
06 Apr 2012

Thanks from me too.

Just a little remark:

SELECT COUNT(*) - 1 INTO VarCount FROM Table1;

is better with "- 1", because if not, the last row is FETCH-ed twice/ two times.

Regards.

syed.shah 1 post Joined 10/12
25 Apr 2013

@mdgmilen
I was implementing a simialr thing kindly let me know, why the last row is fetched twice?

You must sign in to leave a comment.