Job Search

Saturday, December 19, 2015

PL/SQL Dynamic SQL with Collections

Example 1:

CREATE OR REPLACE PROCEDURE proc_dynamicsql_collection(source_tab VARCHAR2,
                                                       dest_tab   VARCHAR2,
                                                       comp_col   VARCHAR2) IS
  cur_statement VARCHAR2(2000);
  p_str         VARCHAR2(4000);
BEGIN
  cur_statement := 'SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY FROM ' ||
                   source_tab || ' WHERE SYSDATE - ' || comp_col || ' > 60';

  p_str := 'DECLARE
  err_code      NUMBER;
  err_msg       VARCHAR2(1000);
  TYPE tab_cur IS TABLE OF emp_test%ROWTYPE INDEX BY PLS_INTEGER;
  lv_tab_cur tab_cur;

  TYPE cur_source_tab IS REF CURSOR;
  ref_cur cur_source_tab;

BEGIN
  OPEN ref_cur FOR ' || cur_statement || ';
  LOOP
    FETCH ref_cur BULK COLLECT
      INTO lv_tab_cur LIMIT 20;
    EXIT WHEN lv_tab_cur.count = 0;
    FORALL i IN lv_tab_cur.first .. lv_tab_cur.last ';
  p_str := p_str || '
      INSERT INTO ' || dest_tab || '
      VALUES
        (lv_tab_cur(i).employee_id,
         lv_tab_cur(i).first_name,
         lv_tab_cur(i).last_name,
         lv_tab_cur(i).email,
         lv_tab_cur(i).phone_number,
         lv_tab_cur(i).hire_date,
         lv_tab_cur(i).job_id,
         lv_tab_cur(i).salary);
  END LOOP;
  COMMIT;
  CLOSE ref_cur;
EXCEPTION
  WHEN OTHERS THEN
    err_code := SQLCODE;
    err_msg  := SQLERRM;
    dbms_output.put_line(dbms_utility.format_error_backtrace() || err_code ||
                         '' | '' || err_msg);
END;
';
  dbms_output.put_line(p_str);
  EXECUTE IMMEDIATE p_str;
END;

Example 2:

DECLARE
  p_str   LONG;
  d_table VARCHAR2(30) := 'MYEMP';
BEGIN
  DELETE FROM myemp;
  p_str := 'Declare type l_table is table of emp%rowtype;';
  p_str := p_str || ' p_table l_table := l_table();';
  p_str := p_str || ' Begin select * bulk collect into p_table from emp;';
  p_str := p_str || ' forall i in 1..p_table.count';
  p_str := p_str || ' insert into ' || d_table ||' values p_table(i); commit; end;';
  EXECUTE IMMEDIATE p_str;
END;

Example 3:

DECLARE
  TYPE t_emp_id_tab IS TABLE OF emp.employee_id%TYPE;
  l_tab t_emp_id_tab;
  l_table_name VARCHAR2(30) := 'EMP';
BEGIN
  -- Populate collection use in forall.
  SELECT e.employee_id
    BULK COLLECT
    INTO l_tab
    FROM emp e
   WHERE e.employee_id BETWEEN 100 AND 113
   ORDER BY e.employee_id;

  FORALL i IN l_tab.first .. l_tab.last EXECUTE IMMEDIATE
                             'UPDATE ' || l_table_name ||
                             ' SET  employee_id = employee_id / 100 WHERE  employee_id = :1'
                             USING l_tab(i)
    ;
    COMMIT;
END;

Example 4:

DECLARE
  TYPE t_emp_id_tab IS TABLE OF EMP_TEST.EMPLOYEE_ID%TYPE;
  l_tab  t_emp_id_tab;
  l_table_name VARCHAR2(30) := 'EMP_TEST';
BEGIN
  EXECUTE IMMEDIATE
    'DELETE FROM  ' || l_table_name ||' RETURNING employee_id  INTO :1'
     RETURNING BULK COLLECT INTO l_tab;
  DBMS_OUTPUT.put_line('Deleted IDs : ' || l_tab.count || ' rows');
  ROLLBACK;
END;


Example 5:
  
DECLARE
  TYPE t_emp_id_tab IS TABLE OF EMP_TEST.EMPLOYEE_ID%TYPE;
  l_in_tab  t_emp_id_tab;
  l_out_tab t_emp_id_tab;
  l_table_name VARCHAR2(30) := 'EMP_TEST';
BEGIN
  -- Populate collection use in forall.
  EXECUTE IMMEDIATE 'SELECT e.employee_id
     FROM ' || l_table_name ||' e WHERE e.employee_id BETWEEN 100 AND 113' BULK COLLECT INTO l_in_tab;

  FORALL i IN l_in_tab.first .. l_in_tab.last
  EXECUTE IMMEDIATE 'DELETE FROM ' || l_table_name ||'  WHERE employee_id = :1 RETURNING employee_id INTO :2'
  USING l_in_tab(i) RETURNING BULK COLLECT INTO l_out_tab;

  dbms_output.put_line('Starting IDs : ' || l_in_tab.count || ' rows');
  dbms_output.put_line('Deleted IDs  : ' || l_out_tab.count || ' rows');

  ROLLBACK;

END;


I hope you all have enjoyed reading this article. Comments are welcome....

Monday, December 14, 2015

ORACLE DATA TYPES

The following is a list of datatypes available in Oracle/PLSQL, which includes character, numeric, date/time, LOB and rowid datatypes.

CHARACTER DATATYPES

The following are the Character Datatypes in Oracle/PLSQL:

Data Type Syntax
Oracle 9i
Oracle 10g
Oracle 11g
Explanation
char(size)
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Where size is the number of characters to store. Fixed-length strings. Space padded.
nchar(size)
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Where size is the number of characters to store. Fixed-length NLS string Space padded.
nvarchar2(size)
Maximum size of 4000 bytes.
Maximum size of 4000 bytes.
Maximum size of 4000 bytes.
Where size is the number of characters to store. Variable-length NLS string.
varchar2(size)
Maximum size of 4000 bytes.
Maximum size of 32KB in PLSQL.
Maximum size of 4000 bytes.
Maximum size of 32KB in PLSQL.
Maximum size of 4000 bytes.
Maximum size of 32KB in PLSQL.
Where size is the number of characters to store. Variable-length string.
long
Maximum size of 2GB.
Maximum size of 2GB.
Maximum size of 2GB.
Variable-length strings. (backward compatible)
raw
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Maximum size of 2000 bytes.
Variable-length binary strings
long raw
Maximum size of 2GB.
Maximum size of 2GB.
Maximum size of 2GB.
Variable-length binary strings. (backward compatible)

 

NUMERIC DATATYPES

The following are the Numeric Datatypes in Oracle/PLSQL:

Data Type Syntax
Oracle 9i
Oracle 10g
Oracle 11g
Explanation
number(p,s)
Precision can range from 1 to 38.
Scale can range from -84 to 127.
Precision can range from 1 to 38.
Scale can range from -84 to 127.
Precision can range from 1 to 38.
Scale can range from -84 to 127.
Where p is the precision and s is the scale.
For example, number(7,2) is a number that has 5 digits before the decimal and 2 digits after the decimal.
numeric(p,s)
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Where p is the precision and s is the scale.
For example, numeric(7,2) is a number that has 5 digits before the decimal and 2 digits after the decimal.
float




dec(p,s)
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Where p is the precision and s is the scale.
For example, dec(3,1) is a number that has 2 digits before the decimal and 1 digit after the decimal.
decimal(p,s)
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Precision can range from 1 to 38.
Where p is the precision and s is the scale.
For example, decimal(3,1) is a number that has 2 digits before the decimal and 1 digit after the decimal.
integer




int




smallint




real




double precision




 

DATE/TIME DATATYPES

The following are the Date/Time Datatypes in Oracle/PLSQL:

Data Type Syntax
Oracle 9i
Oracle 10g
Oracle 11g
Explanation
date
A date between Jan 1, 4712 BC and Dec 31, 9999 AD.
A date between Jan 1, 4712 BC and Dec 31, 9999 AD.
A date between Jan 1, 4712 BC and Dec 31, 9999 AD.

timestamp (fractional seconds precision)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
Includes year, month, day, hour, minute, and seconds.
For example:
timestamp(6)
timestamp (fractional seconds precision) with time zone
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
Includes year, month, day, hour, minute, and seconds; with a time zone displacement value.
For example:
timestamp(5) with time zone
timestamp (fractional seconds precision) with local time zone
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
Includes year, month, day, hour, minute, and seconds; with a time zone expressed as the session time zone.
For example:
timestamp(4) with local time zone
interval year
(year precision)
to month
year precision is the number of digits in the year. (default is 2)
year precision is the number of digits in the year. (default is 2)
year precision is the number of digits in the year. (default is 2)
Time period stored in years and months.
For example:
interval year(4) to month
interval day
(day precision)
to second (fractional seconds precision)
day precision must be a number between 0 and 9. (default is 2)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
day precision must be a number between 0 and 9. (default is 2)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
day precision must be a number between 0 and 9. (default is 2)
fractional seconds precisionmust be a number between 0 and 9. (default is 6)
Time period stored in days, hours, minutes, and seconds.
For example:
interval day(2) to second(6)

 

LARGE OBJECT (LOB) DATATYPES

The following are the LOB Datatypes in Oracle/PLSQL:

Data Type Syntax
Oracle 9i
Oracle 10g
Oracle 11g
Explanation
bfile
Maximum file size of 4GB.
Maximum file size of 232-1 bytes.
Maximum file size of 264-1 bytes.
File locators that point to a binary file on the server file system (outside the database).
blob
Store up to 4GB of binary data.
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage).
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage).
Stores unstructured binary large objects.
clob
Store up to 4GB of character data.
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character data.
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character data.
Stores single-byte and multi-byte character data.
nclob
Store up to 4GB of character text data.
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character text data.
Store up to (4 gigabytes -1) * (the value of the CHUNK parameter of LOB storage) of character text data.
Stores unicode data.

 

ROWID DATATYPES

The following are the Rowid Datatypes in Oracle/PLSQL:

Data Type Syntax
Oracle 9i
Oracle 10g
Oracle 11g
Explanation
rowid
The format of the rowid is: BBBBBBB.RRRR.FFFFF
Where BBBBBBB is the block in the database file;
RRRR is the row in the block;
FFFFF is the database file.
The format of the rowid is: BBBBBBB.RRRR.FFFFF
Where BBBBBBB is the block in the database file;
RRRR is the row in the block;
FFFFF is the database file.
The format of the rowid is: BBBBBBB.RRRR.FFFFF
Where BBBBBBB is the block in the database file;
RRRR is the row in the block;
FFFFF is the database file.
Fixed-length binary data. Every record in the database has a physical address or rowid.
urowid(size)



Universal rowid.
Where size is optional.




I hope you all have enjoyed reading this article. Comments are welcome....