PLSQL

PL/SQL DBMS output

PL/SQL DBMS output

The DBMS_OUTPUT is said to be the built-in package which enables us for displaying output, debugging information, and send messages from PL/SQL blocks, subprograms, packages, triggers. 

Example:
BEGIN 
   dbms_output.put_line  (user || ' Teachers in the database:'); 
   FOR t IN (SELECT table_name FROM user_teachers) 
   LOOP 
      dbms_output.put_line(t.table_name); 
   END LOOP; 
END; 
/

Datetime Data types and Functions 

Given below are the Datetime data types:

1.DATE
This data type stores date and time information in both of the character and number datatypes. This is made of information like on century, year, month, date, hour, and second.

2.TIMESTAMP
This is the extension data type of the DATE. This stores year, month, and day of the DATE datatype, including with hour, minute and second values. This is very useful in storing precise time values. 

3.YIMESTAMP WITH TIME ZONE
This is the variant of TIMESTAMP that includes a time zone region name or the offset in the value. Offset is the difference between the local time and UTC that is in hours and minutes as well. It is useful for collecting and then evaluating data information in all the geographic regions.

4.TIMESTAMP WITH LOCAL TIME ZONE
This is also a variant of time zone that involves a time zone offset in its value.

Below are the Datetime functions:

  1. ADD_MONTHS(x, y); – this adds y months to x.
  2. LAST_DAY(x); – this returns the last day of the month.
  3. MONTHS_BETWEEN(x, y); – this gives us number of months between x and y.
  4. NEXT_DAY(x, day); - this returns the datetime of the next day after the x.
  5. NEW_TIME; - this returns the time/day value that is specified by the programmer.
  6. ROUND(c [, unit]); - it rounds the given c.
  7. SYSDATE(); - this returns the current system date.
  8. TRUNC(x [, unit]); - this datatype truncates x.

Below are the timestamp functions.

  1. CURRENT_TIMESTAMP(); - this returns a TIMESTAMP WITH TIME ZONE that contains current session time, along with the time zone.
  2. EXTRACT({ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | } TIMEZONE_ABBR ) FROM x) – this extracts and also returns as a result a year, month, day, month, second, or time zone from x.
  3. FROM_TZ(x, time_zone); - this converts TIMESTAMP x and the time zone that are specified by time_zone to a TIMESTAMP WITH TIMEZONE
  4. LOCALTIMESTAMP(); - This gives us TIMESTAMP that contains local time in the session time zone.
  5. SYSTIMESTAMP(); - this returns TIMESTAMP WITH TIME ZONE that contains current database time.
  6. SYS_EXTRACT_UTC(x); - this converts the TIMESTAMP WITH TIMEZONE x to a TIMESTAMP that contains date the date and time in UTC.
  7. TO_TIMESTAMP(x, [format]); - this converts string x into the TIMESTAMP.
  8. TO_TIMESTAMP_TZ(x, [format]); - this converts string x into the TIMESTAMP WITH TIME ZONE.

The Interval Data Types and Functions

Given below are the interval data types:

1.INTERVAL YEAR TO MONTH

2.INTERVAL DAY TO SECOND
INTERVAL FUNCTIONS:
This stores a period of time using the YEAR and MONTH datetime fields.

  1. NUMTODSINTERVAL(x, interval_unit); - this converts the number x to an INTERVAL DAY TO SECOND.
  2.  NUMTOYMINTERVAL(x, interval_unit); -  this converts the number x to an INTERVAL YEAR TO MONTH.
  3. TO_DSINTERVAL(x); - this converts the string to an INTERVAL DAY TO SECOND.
  4. TO_YMINTERVAL(x); - this converts the string to an INTERVAL YEAR TO MONTH.
Examples:
SELECT SYSDATE FROM DUAL;

Output:
09/30/2021 7:12:34

SELECT LOCALTIMESTAMP FROM DUAL;

Output:
09/30/2021 7:12:54.336000

DBMS_OUTPUT Subprograms

  1. DBMS_OUTPUT.DISABLE; - This method disables the message output.
  2. DBMS_OUTPUT.ENABLE(buffer_size IN INTEGER DEFAULT 2000); - This method enables the message output. 
  3. DBMS_OUTPUT.GET_LINE(line OUT VARCHAR2, status OUT INTEGER) - This method retrievesa single line of buffered information.
  4. DBMS_OUTPUT.NEW_LINE; - This method puts an end-of-line marker.
  5. DBMS_OUTPUT.PUT(item IN VARCHAR2); - This method places partial line in the buffer.
  6. DBMS_OUTPUT.PUT_LINE(item IN VARCHAR2) – This method places a line in the buffer. 
Example:
DECLARE 
   lines dbms_output.chararr; 
   num_lines number; 
BEGIN 
   -- enable the buffer with default size 20000 
   dbms_output.enable; 
   dbms_output.put_line('Hello welcome to great learning!'); 
   dbms_output.put_line('Hope you have enjoyed the tutorials!'); 
   dbms_output.put_line('Have a happy learning'); 
   num_lines := 3; 
   dbms_output.get_lines(lines, num_lines); 
   FOR i IN 1..num_lines LOOP 
      dbms_output.put_line(lines(i)); 
   END LOOP; 
END; 
/
Output:
Hello welcome to great learning! 
Hope you have enjoyed the tutorials! 
Have a happy learning!  
PL/SQL procedure successfully completed