I have the following statement which compiles fine in my package:
package header:
TYPE role_user_type IS RECORD (
ROLE_ID some_table.ROLE_ID%TYPE,
SUBGROUP some_table.USER_ID%TYPE
);
body:
ROLE_USER_REC MY_PACKAGE.ROLE_USER_TYPE;
SELECT B.USER_ID, B.ROLE INTO ROLE_USER_REC
FROM some_table where user_id like 'M%'
what is the skeleton for looping through ROLE_USER_REC
? can we even loop through it?
In Oracle, the FOR LOOP allows you to execute code repeatedly for a fixed number of times.
A record type is a composite data type that consists of one or more identifiers and their corresponding data types. You can create user-defined record types by using the TYPE IS RECORD statement within a package or by using the CREATE TYPE (Object) statement. Dot notation is used to reference fields in a record.
RECORD and TABLE are user-defined composite types that can be defined by the programmer. The record type allows the defination of a type that consists of specified fields. Once the type has been defined, you can then define a specific name for the type as it is being used, a variable name that references the type.
There is nothing to loop.
role_user_type
defines a single record, that you can access via:
dbms_output.put_line( role_user_rec.role_id || ', ' || role_user_rec.subgroup );
Your SELECT ... INTO
will fail as soon as more than one row is returned.
If you need to store several of those records, you can use nested tables likeTYPE role_user_tab IS TABLE OF role_user_type
:
Example:
DECLARE
TYPE role_user_type IS RECORD (
ROLE_ID VARCHAR2(10),
SUBGROUP VARCHAR2(10)
);
TYPE role_user_tab IS TABLE OF role_user_type;
role_user_rec role_user_tab;
BEGIN
SELECT 'A', 'B'
BULK COLLECT INTO role_user_rec
FROM dual;
FOR i IN role_user_rec.FIRST .. role_user_rec.LAST LOOP
dbms_output.put_line( role_user_rec(i).role_id || ', ' || role_user_rec(i).subgroup );
END LOOP;
END;
You can use cursors for this
FOR i in (/* Your Select query*/)
loop
/* You can use value of the column fetched from select query like i.column_name
and use it which ever way you want */
end loop;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With