Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to identify last record in cursor in FOR...LOOP

Status
Not open for further replies.

SJSFoxPro

Technical User
Jun 19, 2003
110
US
Here is part of my PL/SQL block:
ACCEPT create_like_user PROMPT 'Enter CREATE LIKE user ID: '
ACCEPT new_user PROMPT 'Enter new user ID to create: '

VARIABLE s_grantee VARCHAR2(256)

DECLARE
CURSOR all_user_cur IS
SELECT distinct grantee
FROM dba_role_privs
WHERE grantee = UPPER('&create_like_user')
AND grantee NOT IN (SELECT role FROM dba_roles)
ORDER BY grantee;
CURSOR all_defrol_cur IS
SELECT granted_role
FROM dba_role_privs
WHERE grantee = :s_grantee
AND default_role = 'NO'
ORDER BY granted_role;
BEGIN
FOR u_name IN all_user_cur
LOOP
dbms_output.put_line('alter user "'||UPPER('&new_user')||
'" default role ALL except ');
:s_grantee := u_name.grantee;
FOR r_name IN all_defrol_cur
LOOP
-- NEED CONDITION HERE TO TEST FOR THE LAST RECORD IN THE CURSOR
-- IF NOT THE LAST RECORD, PRINT THE FOLLOWING LINE
dbms_output.put_line(' '||r_name.granted_role||',');
-- IF ON THE LAST RECORD, PRINT THIS LINE INSTEAD
-- (i.e., without the ',' appended to the end)
dbms_output.put_line(' '||r_name.granted_role);
END LOOP;
dbms_output.put_line(' '||';');
END LOOP;
END;
/

I have added comments in the code where I need a conditional statement to print one line if it is not the last record and the other line if it is the last record.

Appreciate any help you can provide. I have not found any documentation that provides a clear example for what I am trying to accomplish. I'm new to PL/SQL.

Thanks!
 

Do it the other way around:

Code:
...Etc...
L_Sep Varchar2(4):='   ';
Begin
    For U_Name In All_User_Cur
    Loop
        Dbms_Output.Put_Line('Alter User "'||Upper('&New_User')
             ||'" Default Role All Except ');
        :S_Grantee := U_Name.Grantee;
        L_Sep := '   ';
        For R_Name In All_Defrol_Cur                       
        Loop
            Dbms_Output.Put_Line( L_Sep||R_Name.Granted_Role);
            L_Sep:=', ';
        End Loop;
        Dbms_Output.Put_Line('    '||';');
    End Loop;
End;
/
...Etc...
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top