Explore core differences, usage scenarios, and behaviors of PL/SQL implicit and explicit cursors with this quiz designed for beginners. Enhance your understanding of cursor types, their attributes, and their roles in SQL data retrieval within PL/SQL programming.
Which statement best describes an implicit cursor in PL/SQL?
Explanation: An implicit cursor is automatically managed by PL/SQL whenever a single SQL statement like SELECT INTO, INSERT, UPDATE, or DELETE is executed. Option B describes an explicit cursor, not an implicit one. Option C is incorrect because implicit cursors do not require manual opening or closing. Option D is too narrow; implicit cursors can be used for multiple types of DML statements, not only INSERT.
When must a programmer define an explicit cursor in PL/SQL?
Explanation: Explicit cursors are essential for handling queries that return more than one row, allowing the programmer to fetch each row individually. Option A is handled by implicit cursors. Option B uses an implicit cursor by default. Option D is unrelated to cursor management and refers to data processing, not database interaction.
Which cursor attribute can be used with both implicit and explicit cursors in PL/SQL to check if the last SQL statement was successful?
Explanation: The %FOUND attribute checks if the last fetch or DML statement affected any rows and is available for both cursor types. %OPEN is only relevant to explicit cursors, as implicit cursors are always closed automatically. %BULK_ROWCOUNT is primarily used with bulk operations, not standard cursors. %LOOP is not an actual PL/SQL cursor attribute.
What is a required step when working with explicit cursors but not with implicit cursors in PL/SQL?
Explanation: Explicit cursors must be declared explicitly and associated with a query so they can be used to process multiple rows. Option B is incorrect as DELETE can be handled by implicit cursors. Option C incorrectly describes implicit cursors, not explicit. Option D fails to identify the key difference: explicit cursors require manual steps, while implicit are fully managed by PL/SQL.
Given the code: DELETE FROM employees WHERE department_id = 50;, which cursor type does PL/SQL use by default?
Explanation: PL/SQL automatically handles DML statements like DELETE using an implicit cursor, so no explicit declaration is necessary. Option B is incorrect because explicit cursors must be declared by the programmer. Global and static cursors (Options C and D) are not actual PL/SQL cursor types.
What is the correct way to declare an explicit cursor in PL/SQL?
Explanation: Explicit cursors must be declared with the syntax: DECLARE CURSOR cursor_name IS SELECT ...;. Option B is the step to open a declared cursor. Option C incorrectly attempts to assign a SELECT result directly to a cursor name. Option D is syntactically invalid in PL/SQL.
In order to retrieve each row from a result set using an explicit cursor, which action is necessary?
Explanation: Explicit cursors require the programmer to fetch each row using the FETCH statement, typically inside a loop. PL/SQL does not automatically fetch all rows for explicit cursors (Option B). Option C is valid only for implicit cursors when expecting one row. Opening the cursor multiple times (Option D) is not necessary and would only reset the process.
To find the number of rows affected by the last DML statement using an implicit cursor, which attribute should be used?
Explanation: %ROWCOUNT returns the number of rows affected by the most recent DML statement, making it the correct attribute. %COUNT and %ROWS are not valid PL/SQL attributes for row counting. %AFFECTED is also not a PL/SQL cursor attribute.
For which scenario are implicit cursors primarily used in PL/SQL?
Explanation: Implicit cursors are designed for cases where only one row is returned or for DML operations like INSERT, UPDATE, or DELETE. Option B requires explicit cursors. Options C and D are unrelated to cursor management in PL/SQL.
Who is responsible for closing explicit cursors in PL/SQL after use?
Explanation: With explicit cursors, the programmer is responsible for closing them using the CLOSE statement to free resources. Option B is true only for implicit cursors; PL/SQL manages their closure automatically. Option C is incorrect because cursor management is a programming responsibility. Option D is inaccurate as leaving cursors open can lead to resource leaks.