VF=>user  ID=>    Login Register
Feedback   FAQ   Blog
680,547 quizzes played * 1,621 active players * US$57,334 in prizes awarded

Commentary

Actions


  On Oracle PL/SQL: Write No Unnecessary Code (Oracle PL/SQL)      Add to Favorites

Summary

Program units that contain unnecessary code (code that can be removed without changing the behavior of that program) are units inviting maintenance problems. You should always be careful to remove any code that is not needed. Use the compile-time warning to help you identify at least some of these occurrences.

Details

Assumes Oracle Database 10g Release 2 or higher
Beginner Quiz, IDs: 7386/421431/107975

Reviewer(s): Ken Holmslykke, Vitaliy Lyanchevskiy

The Question

Each choice contains a combination of DDL statements and PL/SQL blocks. A choice is correct if it does not contain any unnecessary code.

Code is unnecessary if you can remove it from the choice and the result of running that code remains the same. It is then incorrect.

Special rules for this quiz:

  • You cannot add or change code - only remove.
  • You may not remove part of an identifier (for example, remove "PLS_" from "PLS_INTEGER" and be left with "INTEGER"). Instead, entire identifiers and syntax elements must be removed.
  • White space (space, new line, etc.) is not code.

The following block, for example, is incorrect:

BEGIN
   NULL;
   DBMS_OUTPUT.PUT_LINE (1);
END;

since the NULL statement can be removed without any impact whatsoever. The following block is correct, since if you remove the NULL; statement (or BEGIN or END), the block will no longer be valid.

BEGIN
   NULL;
END;

So which choice does not contain any unnecessary code?

The Choices [↑]

Explanation of Result Icons
Choice 1 (14052)
BEGIN
   EXECUTE IMMEDIATE 'create table plch_new_table (n number)';

   COMMIT;
END;
/

When a DDL statement is parsed, it is also executed and Oracle performs an implicit commit. So the COMMIT; statement is unnecessary.




82%
Choice 2 (14054)
DECLARE
   l_count PLS_INTEGER;
BEGIN
   SELECT COUNT(*) INTO l_count
     FROM ALL_OBJECTS;

   DBMS_OUTPUT.PUT_LINE (l_count);
END;
/

I can change "PLS_INTEGER" to "INT" or "INTEGER" and the code will perform the same - specifically because we know that in a default installation, ALL_OBJECTS will not contain enough rows to exceed the limit on PLS_INTEGER:

DECLARE
   l_count INTEGER;
BEGIN
   SELECT COUNT(*) INTO l_count
     FROM ALL_OBJECTS;

   DBMS_OUTPUT.PUT_LINE (l_count);
END;
/

Removal of part of an identifier is not, however, allowed in this quiz. So this choice is correct.




100%
Choice 3 (14055)
CREATE OR REPLACE PROCEDURE plch_proc
IS
BEGIN
   NULL;
END;
/

DECLARE
   CURSOR source_cur
   IS
      SELECT *
        FROM user_source
       WHERE name = 'PLCH_PROC' AND line = 1;
BEGIN
   FOR rec IN source_cur
   LOOP
      DBMS_OUTPUT.put_line (rec.line);
   END LOOP;

   IF source_cur%ISOPEN
   THEN
      CLOSE source_cur;
   END IF;
END;
/

Oracle always closes the cursor that it opens in a cursor FOR loop. So the entire IF statement can be removed:

DECLARE
   CURSOR source_cur
   IS
      SELECT *
        FROM user_source
       WHERE name = 'PLCH_PROC' AND line = 1;
BEGIN
   FOR rec IN source_cur
   LOOP
      DBMS_OUTPUT.put_line (rec.line);
   END LOOP;
END;
/



70%
Choice 4 (14053)
CREATE TABLE plch_tab (n NUMBER)
/

BEGIN
   EXECUTE IMMEDIATE 'insert into plch_tab VALUES (100)';

   COMMIT;
END;
/

The execution of a DML statement does not result in an implicit commit, as does a DDL statement. Removal of either the EXECUTE IMMEDIATE or COMMIT statements will result in a change of behavior.

Having said that, notice (as Sean Stuber did) that the insert statement itself is not dynamic. So this block could be replaced with:

BEGIN
   insert into plch_tab VALUES (100);

   COMMIT;
END;
/

and the outcome would be the same. Nice catch, Sean!

And, of course, it is very important to be able to make this call - notice that the code is over-complicated and can be simplified.




100%

Answer [↑]

Program units that contain unnecessary code (code that can be removed without changing the behavior of that program) are units inviting maintenance problems. You should always be careful to remove any code that is not needed.

Also consider using the compile-time warning to help you identify at least some of these occurrences. Here is an example of doing to to identify code that could be removed (or requiring some other change in the program unit):

ALTER SESSION SET plsql_warnings = 'enable:all'
/

CREATE OR REPLACE PROCEDURE plch_plw6006
   AUTHID DEFINER
AS
   l_var   NUMBER;

   PROCEDURE not_used
   IS
   BEGIN
      NULL;
   END not_used;
BEGIN
   DBMS_OUTPUT.put_line (l_var);
END plch_plw6006;
/

SHOW ERRORS PROCEDURE plch_plw6006

Statistics and Rankings [↑]

Rank Percentile Score Seconds% Correct
Median
208
50
196
162
100
Best
1
100
243
21
100

Verify Quiz [↑]

Use the following code to verify the scoring of all the choices in this quiz. If you feel that there is a bug in our code or you disagree with the results, submit an objection in the Commentary section.

/* Commit after DDL? Unnecessary */

BEGIN
   EXECUTE IMMEDIATE 'create table plch_new_table (n number)';

   COMMIT;
END;
/

SELECT COUNT (*)
  FROM user_objects
 WHERE object_name = 'PLCH_NEW_TABLE'
/

/* Confirm */

DROP TABLE plch_new_table
/

BEGIN
   EXECUTE IMMEDIATE 'create table plch_new_table (n number)';
END;
/

SELECT COUNT (*)
  FROM user_objects
 WHERE object_name = 'PLCH_NEW_TABLE'
/

DROP TABLE plch_new_table
/

/* Get count from table - all code needed */

DECLARE
   l_count   PLS_INTEGER;
BEGIN
   SELECT COUNT (*) INTO l_count FROM all_objects;

   DBMS_OUTPUT.put_line (l_count);
END;
/

/* Close a CFL cursor? Unnecessary */

CREATE OR REPLACE PROCEDURE plch_proc
IS
BEGIN
   NULL;
END;
/

DECLARE
   CURSOR source_cur
   IS
      SELECT *
        FROM user_source
       WHERE name = 'PLCH_PROC' AND line = 1;
BEGIN
   FOR rec IN source_cur
   LOOP
      DBMS_OUTPUT.put_line (rec.line);
   END LOOP;

   IF source_cur%ISOPEN
   THEN
      CLOSE source_cur;
   END IF;
END;
/

/* Confirm */

DECLARE
   CURSOR source_cur
   IS
      SELECT *
        FROM user_source
       WHERE name = 'PLCH_PROC' AND line = 1;
BEGIN
   FOR rec IN source_cur
   LOOP
      DBMS_OUTPUT.put_line (rec.line);
   END LOOP;
END;
/

DROP PROCEDURE plch_proc
/

/* Execute DML - commit is needed. */

CREATE TABLE plch_tab (n NUMBER)
/

BEGIN
   EXECUTE IMMEDIATE 'insert into plch_tab VALUES (100)';

   COMMIT;
END;
/

ROLLBACK
/

SELECT COUNT (*) FROM plch_tab
/

/* Confirm */

TRUNCATE TABLE plch_tab
/


BEGIN
   EXECUTE IMMEDIATE 'insert into plch_tab VALUES (100)';
END;
/

ROLLBACK
/

SELECT COUNT (*) FROM plch_tab
/

DROP TABLE plch_tab
/

Link to Quiz [↑]

Commentary [↑]

Ask for Help     Raise Objection     Start New Discussion     Your Comments
Last: 82 days ago
Replies: 6
Likes: 0
SUBMITTED
Unread Comments
Last: 82 days ago
Replies: 4
Likes: 0
SUBMITTED
Unread Comments
Last: 79 days ago
Replies: 27
Likes: 1
ACCEPTED
Unread Comments
Last: 81 days ago
Replies: 4
Likes: 1
SUBMITTED
Unread Comments
Likes: 5
ACCEPTED
Unread Comments
Last: 81 days ago
Replies: 1
Likes: 0
SUBMITTED
Unread Comments
Likes: 0
SUBMITTED
Unread Comments
1 - 7