PL/SQL loop through a set of values

Responsive image

Here’s a simple trick on how to make a PL/SQL loop iterate through a set of predefined values. This can be useful when you need to perform a report query on a set of Oracle user account, or to query a set of specific dictionary objects for example.

Note that you can also make this static list a dynamically generated list.

set serveroutput on

DECLARE
    type nt_type is table of VARCHAR2(30);
    nt nt_type := nt_type ('Choice_1', 'Choice_2'
    , 'Choice_3', 'Choice_4'
    , 'Choice_5', 'Choice_6');
BEGIN
  FOR i IN 1..nt.count
  LOOP
    dbms_output.put_line(nt(i));
  END LOOP;
END;
/
Written on February 20th, 2017 by Samy Gejzenblozen

Tags:


Social networks

You may also enjoy:

Forcing ASMM component to shrink

Forcing ASMM component to shrink

#oracle #database

Here's the way to force the shared pool to shrink dynamically. Documentation states that ASMM can only increase shared pool, and can't shrink. When the automatic shared memory management feature is enabled, the internal tuning algorithm tries to determine an optimal size for the shared pool based on the workload. It usually converges on this va... Read more

20 Mar 2019 - 2 minute read
How to extract Oracle user DDL

How to extract Oracle user DDL

#database #oracle

In some cases, you need to duplicate an Oracle user, along with all it's privileges. Doing this manually can be tedious. Hopefully, we can use the data dictionary to extract the data we need and dump it into a SQL file to modify and replay. Here's how to do it: ```sql set head off set pages 0 set long 9999999 spool user_script.sql SELECT DBMS... Read more

20 Feb 2018 - 1 minute read
Restart a hung Oracle database

Restart a hung Oracle database

#oracle

On rare occasions, mostly during a high server load peak or a process failure, an Oracle instance may not accept any connection. Either from regular users as well as SYSDBA. This situation is called a **hung database** and must be quickly resolved as the database isn't accessible for your users anymore. As you cannot connect to the hung database... Read more

20 May 2017 - 1 minute read