• Skip to main content

Tech Honey

The #1 Website for Oracle PL/SQL, OA Framework and HTML

  • Home
  • HTML
    • Tags in HTML
    • HTML Attributes
  • OA Framework
    • Item in OAF
    • Regions in OAF
    • General OAF Topics
  • Oracle
    • statement
    • function
    • clause
    • plsql
    • sql

AFTER INSERT TRIGGER in Oracle PLSQL

December 12, 2012 by techhoneyadmin

Oracle PLSQL AFTER INSERT TRIGGER means that the trigger will get executed or triggered just after an INSERT operation is performed on the table for which the trigger is written.

Oracle PLSQL syntax to create an AFTER INSERT TRIGGER is:

CREATE OR REPLACE TRIGGER trigger_name
AFTER INSERT
ON table_name
[FOR EACH ROW]
DECLARE

–variable declaration section of code
BEGIN
–code to be triggered
EXCEPTION
WHEN..

–exception handling code
END;

Here in the above Oracle PLSQL trigger syntax the ‘trigger_name’ is the name given to AFTER INSERT TRIGGER.

Some points to remember about PLSQL AFTER INSERT TRIGGER are:

We cannot create an AFTER INSERT TRIGGER on an Oracle PLSQL VIEW.

We cannot update :NEW values with AFTER INSERT TRIGGER.

We cannot update the :OLD values using AFTER INSERT TRIGGER.

Example of Oracle PLSQL AFTER INSERT TRIGGER

Suppose we create a table named ‘new_employee’ using the script below:

CREATE TABLE new_employee
(
  employee_id    NUMBER
  ,employee_name VARCHAR2(1000)
  ,creation_date DATE
  ,created_by    VARCHAR2(1000)
);

If we query the ‘new_employee’ table as:

SELECT *
FROM new_employee;

We will get the following output:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY

Also we create a duplicate table of ‘new_employee’ table as new_employee_duplicate’ suing the script below:

CREATE TABLE new_employee_duplicate
AS(SELECT *
      FROM new_employee);

If we query the ‘new_employee_duplicate’ table as

SELECT *
FROM new_employee_duplicate;

We will get the following output:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY

At this moment we don’t have any data in ‘new_employee’ and ‘new_employee_duplicate’ tables.


Creating an Oracle PLSQL AFTER INSERT Trigger on new_employee table.

Now let’s create a trigger on ‘new_employee’ table so that whenever we are entering any employee record in the ‘new_employee’ table the same record also gets stored in ‘new_employee_duplicate’ table.

For this purpose we can create a PLSQL AFTER INSERT TRIGGER as:

CREATE OR REPLACE TRIGGER new_employee_trigger
AFTER INSERT
ON new_employee
FOR EACH ROW
DECLARE
  v_creator_name VARCHAR2(1000);
  v_creation_date DATE;
BEGIN
--Getting the name of the current logged in User
  SELECT USER INTO v_creator_name
  FROM dual;
--setting system date in v_creation_date
  v_creation_date := sysdate;
--Inserting data the new_employee_duplicate table
  INSERT INTO new_employee_duplicate
  VALUES (:new.employee_id
          ,:new.employee_name
          ,v_creation_date
          ,v_creator_name
         );
END;

Here we have created a PLSQL AFTER INSERT TRIGGER named ‘new_employee_trigger’ which will insert a record in the ‘new_employee_duplicate’ table as soon as insert operation is performed on ‘new_employe’ table.


Let’s see PLSQL AFTER INSERT TRIGGER in action.

Let’s add a row in ‘new_employee’ table as:

INSERT INTO new_employee
VALUES
(1, 'Emp ABCD',sysdate,’SCOTT’);

Once we run the above INSERT statement we can query the ‘new_employee’ as:

SELECT *
FROM new_employee;

We will get the following result:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY
1 Emp ABCD 12/12/2012 5:34:49 PM SCOTT

Also if we query the ‘new_employee_duplicate’ table as

SELECT *
FROM new_employee_duplicate;

We will get the following output:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY
1 Emp ABCD 12/12/2012 5:34:49 PM SCOTT

Here using the Oracle PLSQL AFTER INSERT TRIGGER we can see that in the ‘new_employee_duplicate’ table a record got inserted as soon as we inserted a record in ‘new_employee’ table.

Please note that the values in CREATION_DATE and CREATED_BY columns may be different for you as they depend on system date and logged in user.

Filed Under: plsql Tagged With: After insert trigger in oracle plsql, AfterInsertTriggerPLSQL, CREATETRIGGERSPLSQL, how to create after insert trigger in oracle plsql, what is After insert trigger in oracle plsql

BEFORE INSERT TRIGGER in Oracle PLSQL

December 12, 2012 by techhoneyadmin

Oracle PLSQL BEFORE INSERT TRIGGER means that the trigger will get executed or triggered, just before an INSERT operation is performed on the table for which the trigger is written.

Oracle PLSQL syntax to create a BEFORE INSERT TRIGGER is:

CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT
ON table_name
[FOR EACH ROW]
DECLARE

–variable declaration section of code
BEGIN
–code to be triggered
EXCEPTION
WHEN..

–exception handling code
END;

Here in the above Oracle PLSQL trigger syntax the trigger_name is the name given to trigger.

Some points to remember about PLSQL BEFORE INSERT TRIGGER are:

We cannot create a BEFORE INSERT TRIGGER on an Oracle PLSQL VIEW.

We can update :NEW values with BEFORE INSERT TRIGGER.

We cannot update the :OLD values using BEFORE INSERT TRIGGER.


Example of Oracle PLSQL BEFORE INSERT TRIGGER

Suppose we create a table named ‘new_employee’ using the script below:

CREATE TABLE new_employee
(
  employee_id    NUMBER
  ,employee_name VARCHAR2(1000)
  ,creation_date DATE
  ,created_by    VARCHAR2(1000)
);

If we query the ‘new_employee’ table as:

SELECT *
FROM new_employee;

We will get the following output:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY

At this moment we don’t have any data in the ‘new_employee’ table


Creating an Oracle PLSQL BEFORE INSERT TRIGGER on ‘new_employee’ table

Now let’s create a trigger on ‘new_employee’ table so that whenever we enter any employee record, the CREATION_DATE and CREATED_BY columns for that record gets filled automatically.

For this purpose we can create a PLSQL BEFORE INSERT TRIGGER as:

CREATE OR REPLACE TRIGGER new_employee_trigger
BEFORE INSERT
  ON new_employee
FOR EACH ROW
DECLARE
  v_creator_name VARCHAR2(1000);
BEGIN
--Getting the name of the current logged in User
  SELECT USER INTO v_creator_name
  FROM dual;
--setting system date in created_date column
  :new.creation_date := sysdate;
--setting the user name to created_by column
  :new.created_by    := v_creator_name;
END;

Here we have created a PLSQL BEFORE INSERT TRIGGER named ‘new_employee_trigger’ which will update the CREATED_BY and CREATION_DATE columns in the ‘new_employee’ table.


Let’s see PLSQL BEFORE INSERT TRIGGER in action.

Let’s add a row in ‘new_employee’ table as:

INSERT INTO new_employee
VALUES
(1, 'Emp ABCD',NULL,NULL);

Once we run the above INSERT statement we can query the ‘new_employee’ as:

SELECT *
FROM new_employee;

We will get the following result:

EMPLOYEE_ID EMPLOYEE_NAME CREATION_DATE CREATED_BY
1 Emp ABCD 12/12/2012 5:34:49 PM SCOTT

Here although we have passed NULL values for the CREATION_DATE and CREATED_BY columns, the PLSQL BEFORE INSERT TRIGGER (‘new_employee_trigger’) inserts the CREATED_BY and CREATION_DATE columns in the ‘new_employe’ table.

Please note that the values in CREATTION_DATE and CREATED_BY columns may be different for you as they depend on system date and logged in user.


Filed Under: plsql Tagged With: Before insert trigger in oracle plsql, BeforeInsertTriggerPLSQL, CREATETRIGGERSPLSQL, how to create Before insert trigger in oracle plsql, what is Before insert trigger in oracle plsql

TRIGGERS in Oracle PLSQL

December 11, 2012 by techhoneyadmin

Oracle PLSQL TRIGGER is a set of statements that get executed or triggered as a side effect of modification of database.

Oracle PLSQL Triggers can be categorized as:

  1. Insert Trigger
  2. Update Trigger
  3. Delete Trigger

1. Insert Trigger:- This type of plsql trigger gets executed as soon as an INSERT action is performed on a table.

Insert trigger can be of two types:

  • Before Insert Trigger
  • After Insert Trigger

2. Update Trigger:- This type of plsql trigger gets executed as soon as an UPDATE action is performed on a table.

Update Trigger can be of 2 types:

  • Before Update Trigger
  • After Update Trigger

3. Delete Trigger:- This type of plsql trigger gets executed as soon as a DELETE action is performed on a table.

Delete Trigger can be of 2 types:

  • Before Delete Trigger
  • After Delete Trigger

Please click on the trigger types above to understand the detailed explanation of each trigger type with examples.


Drop an Oracle PLSQL trigger:

Oracle PLSQL allows up to drop a trigger which we have created previously

PLSQL Syntax to drop a trigger is:

DROP TRIGGER trigger_name;

Example to drop a PLSQL trigger:

DROP TRIGGER trigger_employee_update;

The above drop statement will drop the trigger named ‘trigger_employee_update’ from the data base.


Enable and Disable an Oracle PLSQL Trigger:

1. Enable a PLSQL Trigger

Oracle PLSQL allows us to enable the trigger that we have created and disabled previously using ALTER statement and ENABLE keyword.

Oracle PLSQL syntax to enable a trigger is:

ALTER TRIGGER trigger_name;

Example to enable a PLSQL trigger:

ALTER TRIGGER trigger_employee_update ENABLE;

The above drop statement will enable the trigger named ‘trigger_employee_update’.

2. Enable all PLSQL Triggers on a table

Oracle PLSQL allows us to enable all the triggers on a table at once using the ALTER statement with the ENABLE ALL TRIGGERS keyword.

Syntax to enable all PLSQL triggers on a table is:

ALTER TABLE table_name ENABLE ALL TRIGGERS;

Example to enable all Oracle PLSQL triggers on a table:

ALTER TABLE employee ENABLE ALL TRIGGERS;

The above ALTER TABLE statement will enable all the triggers on ‘employee’ table.

3.Disable an Oracle PLSQL Trigger

We can disable a PLSQL Trigger that we have created earlier by using the ALTER statement with the DISABLE keyword.

Oracle PLSQL syntax to disable a trigger is:

ALTER TRIGGER trigger_name DISABLE;

Example to disable a PLSQL trigger in PLSQL:

ALTER TRIGGER trigger_employee_update DISABLE;

The above ALTER TRIGGER statement will disable the ‘trigger_employee_update’ trigger.

4. Disable all Oracle PLSQL Triggers on a table

Oracle PLSQL allows us to disable all the trigger on a table at once using the ALTER TABLE Statement with the DISABLE ALL TRIGGERS keyword.

Syntax to disable all plsql triggers on a table is:

ALTER TABLE table_name DISABLE ALL TRIGGERS;

Example to disable all PLSQL triggers on a table:

ALTER TABLE employee DISABLE ALL TRIGGERS;

The above statement will disable all the triggers on ‘employee’ table.


Filed Under: plsql Tagged With: CREATETRIGGERSPLSQL, drop enable and disable trigger in oracle plsql, How to create triggers in oracle plsql, types of triggers in oracle plsql

Creating PROCEDURES in Oracle PLSQL

December 10, 2012 by techhoneyadmin

Oracle PLSQL PROCEDURE is a named block like PLSQL Functions which can perform one or more actions.

Using Oracle PLSQL PROCEDURE we can achieve complex business needs.

Oracle PLSQL syntax to create a PROCEDURE is:

CREATE [OR REPLACE] PROCEDURE [schema.] procedure_name
[( parameter_1 [IN] [OUT] parameter_data_type_1,
parameter_2 [IN] [OUT] parameter_data_type_2,…
parameter_N [IN] [OUT] parameter_data_type_N )]
[AUTHID DEFINER | CURRENT_USER]
IS
— declaration_statements
BEGIN
— executable_statements
return {return_data_type};
[EXCEPTION
— the exception-handling statements]
END [procedure_name];

Let’s understand the above PLSQL PROCEDURE creation syntax in detail:

For ease of understanding let’s divide the PROCEDURE creation syntax in 2 parts.

1. PROCEDURE Head

2. PROCEDURE Body


PLSQL PROCEDURE Head:-

All the code before the “IS” keyword is called the PROCEDURE head or signature.

Various parts of PLSQL PROCEDURE Head are:

a. Schema: This is an optional parameter and defines the schema name in which the procedure will be created.

If omitted, it defaults to the schema of the current user.

If we specify a different user then, the other user must have the privileges to create a procedure in his/her schema.

b. Name: The NAME parameter defines the name of the procedure.

The name of a procedure should preferably start with a verb to make it more meaningful.

c. Parameters: The parameters are optional. These will be required to pass and receive values from a PLSQL procedure.

There are 3 styles of passing parameters.

IN, OUT and IN OUT

  • IN:This is the default style of parameter in PLSQL procedure.

    We use the IN mode whenever we want the parameter to be read only i.e. we cannot change the value of the parameter in the PLSQL procedure.

    The parameter defined as IN will behave as constant type inside the PLSQL procedure.

    We can assign a default value to the IN type of parameter. Oracle PLSQL also allows us to make the IN as optional.

  • OUT: The parameters labeled as OUT returns the values to the calling subprogram or subroutines.

    A default value cannot be assigned to OUT parameter hence we cannot make it optional.

    We have to assign a value to OUT parameter before we exit the procedure or the value of the OUT parameter will be NULL.

    While calling a procedure with OUT parameters, we have to make sure than we pass variables for the corresponding OUT parameters.

  • IN OUT: In this mode the actual parameter is passed to the PLSQL procedure with initial values and then within the PLSQL procedure the value of the parameter may get changed or reassigned.

    The IN OUT parameter is finally returned to the calling subroutine.

d. AUTHID: This is also an optional parameter and it defines whether the procedure will execute with the privileges of the CREATOR / DEFINER of the procedure or with that of the CURRENT_USER privileges.


PLSQL PROCEDURE’s body:-

Everything after the “IS” keyword is called as the body of the procedure.

The procedure’s body conatins the declaration of variables in the declaration section, the code to be executed in the executable_statements section and the code to handle any exception in the exception handeling section.

The declaration and exception handling sections are optional in PLSQL procedure body.

We must have at least one executable statement in the executable statement section of the PLSQL procedure body.

The execution section is the one where we have to write the business logic for implementing the solution.

RETURN statement is also found in PLSQL procedure, but here it serves a different purpose than it serves in PLSQL Function.

The RETURN statement in PLSQL procedure is used to discontinue the execution of the procedure further and return the control to the calling subroutine.


Example of a PLSQL PROCEDURE:

Suppose we have a table named ‘employee’ as shown below:

Employee_ID Employee_Name Salary Department Commission
101 Emp A 10000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support
105 Emp E 32000 Sales 10
106 Emp F 40000 Sales 10
107 Emp G 12000 Sales 10
108 Emp H 12000 Sales

Let’s assume that we want to create a PLSQL PROCEDURE in which we will pass the ‘employee_id’ and ‘salary’ and the PLSQL PROCEDURE will update the record of the employee having the ‘empooyee_id’ using Oracle SQL UPDATE statement.

We can achieve the same as:

CREATE OR REPLACE PROCEDURE update_employee_salary
  (emp_id_in IN NUMBER
   ,salary_in IN NUMBER )
IS
BEGIN
  UPDATE employee
  SET salary = salary_in;
  WHERE employee_id = emp_id_in;
END update_employee_salary;

Above we have created a PLSQL procedure named ‘update_employee_salary’ which can take two parameters ‘employee_id’ and ‘salary’ and update the ‘employee’ table with the details.


Calling PLSQL Procedure:

A PLSQL PROCEDURE can be called using the EXEC or EXECUTE Statement:

Syntax to call a PROCEDURE using EXEC or EXECUTE statement is:

EXEC procedure_name(parameters);

Or

EXECUTE procedure_name(parameters);

Suppose we want to update the salary of ‘employee_id = 101’ from 10000 to 15000 using update_employee_salary procedure.

Let’s call update_employee_salary procedure using EXEC statement.

EXEC update_employee_salary(101,15000);

Now if we query the ‘employee’ table as:

SELECT *
FROM employee;

We will get the following output:

Employee_ID Employee_Name Salary Department Commission
101 Emp A 15000 Sales 10
102 Emp B 20000 IT 20
103 Emp C 28000 IT 20
104 Emp D 30000 Support  
105 Emp E 32000 Sales 10
106 Emp F 40000 Sales 10
107 Emp G 12000 Sales 10
108 Emp H 12000 Sales  

Here we can see that we have successfully updated the record for ‘employee_id = 101’ using PLSQL Procedure.


Filed Under: plsql Tagged With: CREATEPROCEDURESPLSQL, creating procedures in oracle plsql, how to create procedure in oracle plsql, oracle plsql procedures

Creating Functions in Oracle PLSQL

December 10, 2012 by techhoneyadmin

Oracle PLSQL function is a named block which can return a value.

Oracle PLSQL allows us to create functions to meet various business needs.

PLSQL functions are also called as subroutines or subprograms.

Oracle PLSQL syntax to create a function is:

CREATE [OR REPLACE] FUNCTION function_name
[( parameter_1 [IN] [OUT] parameter_data_type_1,
parameter_2 [IN] [OUT] parameter_data_type_2,…
parameter_N [IN] [OUT] parameter_data_type_N )]

RETURN return_datatype
IS | AS
— declaration_statements
BEGIN
— executable_statements
return {return_data_type};
[EXCEPTION
— the exception-handling statements]
END [function_name];

Let’s understand the above function creation syntax in detail:

1. The function_name is the name given to the PLSQL function. Preferably it should begin with a verb e.g. convert_to_date.

2. The parameter_name is the name of the parameter that we are passing to the function.

3. The parameter_data_type is the data type of the parameter that we are passing to the PLSQL function.

4. Every Oracle PLSQL function must have a RETURN statement in the code execution part.

The RETURN specified in the header part of the Oracle PLSQL function specifies the data-type of the value returned by the PLSQL function.

Ways to pass parameters to an Oracle PLSQL Function.

There are 3 ways of passing parameters to PLSQL Function:
a. IN
b. OUT and
c. IN OUT

    • IN: This is the default style of parameter in PLSQL function. We use the IN mode whenever we want the parameter to be read only i.e. we cannot change the value of the parameter in the PLSQL function.The parameter defined as IN will behave as constant type inside the PLSQL function.

      We can assign a default value to the IN type of parameter. Oracle PLSQL also allows up to make the IN as optional.

    • OUT: The parameters labeled as OUT returns the values to the calling subprogram or subroutines.A default value cannot be assigned to OUT parameter hence we cannot make it optional.

      We have to assign a value to OUT parameter before we exit the function or the value of the OUT parameter will be NULL.

    • While calling a function with OUT parameters, we have to make sure than we pass variables for the corresponding OUT parameters.

    • IN OUT: In this mode the actual parameter is passed to the PLSQL function with initial values and then within the PLSQL function the value of the parameter may get changed or reassigned.The IN OUT parameter is finally returned to the calling subroutine.

    The block structure of a PLSQL function is same as that of an PLSQL Anonymous Block except for the addition of CREATE [OR REPLACE FUNCTION, the parameters section of code and the RETURN Clause.


    Example to create an Oracle PLSQL Function:

    Suppose we have a table named ‘employee’ as shown below.

    Employee_ID Employee_Name Salary Department Commission
    101 Emp A 10000 Sales 10
    102 Emp B 20000 IT 20
    103 Emp C 28000 IT 20
    104 Emp D 30000 Support  
    105 Emp E 32000 Sales 10
    106 Emp F 40000 Sales 10
    107 Emp G 12000 Sales 10
    108 Emp H 12000 Sales  

    Suppose we want to create a function that shows us the name of an employee whenever we pass employee_id as parameter.

    We can create an Oracle PLSQL Function as:

    CREATE OR REPLACE FUNCTION get_employee_name
           (emp_id_in IN NUMBER)
    RETURN VARCHAR2
      IS
      emp_name VARCHAR2(100);
    BEGIN
        SELECT employee_name into emp_name
        FROM employee
        WHERE employee_id = emp_id_in;
    RETURN emp_name;
    END get_employee_name;
    

    Calling an Oracle PLSQL Function:

    1. Calling PLSQL Function using Oracle SQL SELECT statement

    Now, if we call the above PLSQL function using an SQL SELECT statement as:

    SELECT get_employee_name (101)
    FROM dual;
    

    We will get ‘Emp A’ as result.

    Also, if we change the employee_id passed to the function then we will get the name of another employee e.g.

    SELECT get_employee_name (105)
    FROM dual;
    

    Will return ‘ Emp E’ as the employee_name.


    2. Calling PLSQL Function using Oracle anonymous block

    Let’s create an anonymous block to call the get_employee_name PLSQL function.

    DECLARE
      n_x VARCHAR2(1000);
      n_y VARCHAR2(1000);
       n_z VARCHAR2(1000);
    BEGIN
       n_x := get_employee_name(101);
       n_y := get_employee_name(102);
       n_z := get_employee_name(103);
    
       DBMS_OUTPUT.PUT_LINE(n_x);
       DBMS_OUTPUT.PUT_LINE(n_y);
       DBMS_OUTPUT.PUT_LINE(n_z);
    END;
    

    Once we run the above Oracle SQL code we will get the following output:
    Emp A
    Emp B
    Emp C

    Here we have called the Oracle PLSQL Function using the Oracle anonymous block.


Filed Under: plsql Tagged With: CREATEFUNCTIONSPLSQL, Creating function in oracle PLSQL, how to create function in oracle PLSQL, Oracle PLSQL Functions

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 14
  • Page 15
  • Page 16
  • Page 17
  • Page 18
  • Interim pages omitted …
  • Page 76
  • Go to Next Page »

Copyright © 2025 · Parallax Pro on Genesis Framework · WordPress · Log in