plsql1

45
What is PL/SQL PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL. 1. Better performance. 2. Procedural features like … declaration of variable, condition checking, looping 3. Errors can be handled in a better way using exceptions. A Simple PL/SQL Block: Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block. A PL/SQL Block consists of three sections: The Declaration section (optional). This section is optional and is used to declare any placeholders like variables, constants, records and cursors, which are used to manipulate data in the execution section. Placeholders may be any of Variables, Constants and Records, which stores data temporarily. Cursors are also declared in this section. The Execution section (mandatory). The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends with END. This is a mandatory section and is the section where the program logic is written to perform any task. The programmatic constructs like loops, conditional statement and SQL statements form the part of execution section. The Exception (or Error) Handling section (optional).

Upload: justin-witt

Post on 27-May-2017

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PLSQL1

What is PL/SQL

PL/SQL stands for Procedural Language extension of SQL.

PL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of SQL.

1. Better performance.2. Procedural features like … declaration of variable, condition checking, looping3. Errors can be handled in a better way using exceptions.

A Simple PL/SQL Block:

Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block.

A PL/SQL Block consists of three sections:

The Declaration section (optional).

This section is optional and is used to declare any placeholders like variables, constants, records and cursors, which are used to manipulate data in the execution section. Placeholders may be any of Variables, Constants and Records, which stores data temporarily. Cursors are also declared in this section.

The Execution section (mandatory).

The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends with END. This is a mandatory section and is the section where the program logic is written to perform any task. The programmatic constructs like loops, conditional statement and SQL statements form the part of execution section.

The Exception (or Error) Handling section (optional).

This section is optional. Any errors in the program can be handled in this section, so that the PL/SQL Blocks terminates gracefully. If the PL/SQL Block contains exceptions that cannot be handled, the Block terminates abruptly with errors.

Page 2: PLSQL1

[DECLARE Variable declaration]BEGIN Program Execution [EXCEPTION Exception handling]END;

Example :

set serveroutput on

DECLARE

width INTEGER;

height INTEGER := 3;

area INTEGER;

name varchar(10);

BEGIN

area := 6;

width := area / height;

name :='deepa';

DBMS_OUTPUT.PUT_LINE('width = ' || width);

DBMS_OUTPUT.PUT_LINE('name = ' || name);

EXCEPTION

WHEN ZERO_DIVIDE THEN

DBMS_OUTPUT.PUT_LINE('Division by zero');

END;

Page 3: PLSQL1

PL/SQL VariablesFew of the datatypes used to define placeholders are as given below.

Number (n,m) , Char (n) , Varchar2 (n) , Date , Long , Long raw, Raw, Blob, Clob, Nclob, Bfile

The General Syntax to declare a variable is:

variable_name datatype [NOT NULL := value ];

variable_name is the name of the variable. datatype is a valid PL/SQL datatype. NOT NULL is an optional specification on the variable. value or DEFAULT valueis also an optional specification, where you can initialize a

variable. Each variable declaration is a separate statement and must be terminated by a semicolon.

Scope of variables1> DECLARE2> var_num1 number; 3>  var_num2 number; 4> BEGIN 5>  var_num1 := 100; 6>  var_num2 := 200; 7>  DECLARE 8>   var_mult number; 9>   BEGIN 10>    var_mult := var_num1 * var_num2; 11>   END; 12> END; 13> /

If you add Add DBMS_OUTPUT.PUT_LINE('outmult'||var_mult);

Assigning values to variables.

1. We can directly assign values to variables.2. We can assign values to variables directly from the database columns by using a SELECT.. INTO

statement. The General Syntax is:

SELECT column_nameINTO variable_name FROM table_name [WHERE condition];

Page 4: PLSQL1

DECLARE

var_salary number(6);

var_emp_id number(6) := 7844;

salary_increase CONSTANT number (3) := 10;

BEGIN

SELECT sal

INTO var_salary

FROM emp

WHERE empno = var_emp_id;

/*

salary_increase := 100;

*/

dbms_output.put_line(var_salary);

dbms_output.put_line('The employee ' || var_emp_id || ' has salary ' || var_salary);

dbms_output.put_line(‘yearly salary increase’||salary_increase);

END;

Constantsconstant_name CONSTANT datatype := VALUE;

constant_name is the name of the constant i.e. similar to a variable name. The word CONSTANT is a reserved word and ensures that the value does not change. VALUE - It is a value which must be assigned to a constant when it is declared. You

cannot assign a value later.

http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/02_funds.htm

Page 5: PLSQL1

Conditional Statements : IF THEN ELSE STATEMENTs

1)IF condition THEN  statement 1; ELSE  statement 2; END IF; 2)IF condition 1 THEN  statement 1;  statement 2; ELSIF condtion2 THEN  statement 3; ELSE  statement 4; END IF 3)IF condition 1 THEN  statement 1;  statement 2; ELSIF condtion2 THEN  statement 3; ELSE  statement 4; END IF; 4)IF condition1 THEN ELSE  IF condition2 THEN  statement1;  END IF; ELSIF condition3 THEN   statement2; END IF;

Page 6: PLSQL1

Iterative statements … while, for and simple loop

1. For loop

FOR counter IN val1..val2

LOOP statements; END LOOP;

val1 - Start integer value. val2 - End integer value.

/* for i in 1..10loop--sequence of statementsend loop;*/

2. While loopWHILE <condition>

LOOP statements; END LOOP;

3. Simple loop (do –while)

LOOP statements; EXIT; {or EXIT WHEN condition;}END LOOP; These are the important steps to be followed while using Simple Loop.

1) Initialize a variable before the loop body.2) Increment the variable in the loop.

Page 7: PLSQL1

3) Use a EXIT WHEN statement to exit from the Loop. If you use a EXIT statement without WHEN condition, the statements in the loop is executed only once.

Page 8: PLSQL1

Example using for:

declaren number;sum1 number default 0;endvalue number;

begin endvalue:=&endvalue;n:=1;for n in 1.. endvalueloopif mod(n,2)=1thensum1:=sum1+n;end if;end loop;dbms_output.put_line('sum = ' || sum1);end;

GOTO command

The label is surrounded by double brackets << >>.

The label must not have a semicolon after the label name.

The Syntax for the GOTO Statement Goto label_name;

Rules to follow to use:

1.A GOTO label must precede an executable statement or a PL/SQL block.

Page 9: PLSQL1

2. A GOTO statement cannot branch into an IF statement, LOOP statement, or sub-block.

3. To branch to a place that does not have an executable statement, add the NULL statement.

4. From the current block, a GOTO statement can branch to another place in the block or into an enclosing block, but not into an exception handler. From an exception handler, a GOTO statement can branch into an enclosing block, but not into the current block.

5. If you use the GOTO statement to exit a cursor FOR loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.

6. A given label can appear only once in a block. However, the label can appear in other blocks including enclosing blocks and sub-blocks. If a GOTO statement cannot find its target label in the current block, it branches to the first enclosing block in which the label appears.

    DECLARE          v_Status NUMBER := 1;    BEGIN         IF v_Status = 1 THEN             GOTO mybranch;         ELSE               v_Status := 1;         END IF;

BEGIN    <<mybranch>>        

dbms_output.put_line('after goto branch');

Page 10: PLSQL1

/*NULL;*/

END;

END;

PL/SQL RecordsWhat are records?

DECLARE

var_salary number(6);

var_emp_id number(6) := 7844;

var_name varchar(20);

var_job varchar(20);

salary_increase CONSTANT number (3) :=10;

BEGIN

SELECT sal,ename,job

INTO var_salary, var_name, var_job

FROM emp

WHERE empno = var_emp_id;

dbms_output.put_line('The employee '||var_name ||' id '|| var_emp_id || ' has salary ' || var_salary);

dbms_output.put_line('role of the employee' ||var_job);

dbms_output.put_line('yearly salary hike in percent '||salary_increase);

Page 11: PLSQL1

END;

Page 12: PLSQL1

RECORDS

Records are another type of datatypes which oracle allows to be defined as a placeholder. Records are composite datatypes, which means it is a combination of different scalar datatypes like char, varchar, number etc. Each scalar data types in the record holds a value. A record can be visualized as a row of data. It can contain all the contents of a row.

Declaring a record: To declare a record, you must first define a composite datatype; then declare a record for that type. The General Syntax to define a composite datatype is:

TYPE record_type_name IS RECORD (first_col_name column_datatype, second_col_name column_datatype, ...);

record_type_name – it is the name of the composite type you want to define. first_col_name, second_col_name, etc.,- it is the names the fields/columns within the record. column_datatype defines the scalar datatype of the fields.

There are different ways you can declare the datatype of the fields.

col_name table_name.column_name%type;

record_name record_type_name;

Example:

DECLARE TYPE employee_type IS RECORD (employee_id number(5),  employee_name emp.ename%type,  employee_job emp.job%type, employee_salary emp.sal%type, employee_department emp.deptno%type);

Page 13: PLSQL1

employee_rec employee_type;

%ROWTYPE :

if all the fields of a record are based on the columns of a table, we can declare the record as follows:

record_name table_name%ROWTYPE; Syntax

TYPE record_type_name IS RECORD (column_name1 datatype, column_name2 datatype, ...);

Usage

Define a composite datatype, where each field is scalar.

Syntax

col_name table_name.column_name%type; Usage

Dynamically define the datatype of a column based on a database column.

Syntax

record_name record_type_name;

Usage

Declare a record based on a user-defined type.

Syntax

record_name table_name%ROWTYPE;

Usage

Page 14: PLSQL1

Dynamically declare a record based on an entire row of a table. Each column in the table corresponds to a field in the record.

Page 15: PLSQL1

Simple example on Records

DECLARE

TYPE employee_type IS RECORD

(emp_id number(5),

emp_name emp.ename%type,

emp_job emp.job%type,

emp_sal emp.sal%type,

emp_dept emp.deptno%type);

employee_rec employee_type;

BEGIN

SELECT empno, ename, job, sal, deptno INTO employee_rec.emp_id, employee_rec.emp_name, employee_rec.emp_job,employee_rec.emp_sal, employee_rec.emp_dept FROM emp where empno=7844;

dbms_output.put_line('---------------EMPLOYEE DETAILS---------------------');

dbms_output.put_line('employee id ' ||employee_rec.emp_id);

dbms_output.put_line('employee name ' ||employee_rec.emp_name);

dbms_output.put_line('employee job ' ||employee_rec.emp_job);

dbms_output.put_line('employee salary ' ||employee_rec.emp_sal);

Page 16: PLSQL1

dbms_output.put_line('employee department ' ||employee_rec.emp_dept);

dbms_output.put_line('-----------------------------------------------------');

END;

Page 17: PLSQL1

What are Cursors? A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database, and manipulate this data. A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set.

1. Implicit cursors

These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed.

When you execute DML statements like DELETE, INSERT, UPDATE and SELECT statements, implicit cursors are created to process these statements.

Oracle provides few attributes called as implicit cursor attributes to check the status of DML operations. The cursor attributes available are

%FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN.

DECLARE

var_rows number(5);

BEGIN

UPDATE emp

SET sal = sal + 1000;

IF SQL%NOTFOUND THEN

dbms_output.put_line('None of the salaries where updated');

ELSIF SQL%FOUND THEN

var_rows := SQL%ROWCOUNT;

Page 18: PLSQL1

dbms_output.put_line('Salaries for ' || var_rows || 'employees are updated');

END IF;

END;

2. Explicit cursorsThey must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row.

The General Syntax for creating a cursor is as given below:

CURSOR cursor_name IS select_statement;

cursor_name – A suitable name for the cursor. select_statement – A select query which returns multiple rows.

How to use Explicit Cursor?

There are four steps in using an Explicit Cursor.

DECLARE the cursor in the declaration section. OPEN the cursor in the Execution Section. FETCH the data from cursor into PL/SQL variables or records in the Execution Section. CLOSE the cursor in the Execution Section before you end the PL/SQL Block.

Page 19: PLSQL1

DECLARECURSOR c1 IS SELECT * FROM emp where deptno=30;/* One more way of declaring a record record_name table_name%ROWTYPE; */e_rec emp%rowtype;BEGINOPEN c1;LOOPFETCH c1 INTO e_rec;EXIT WHEN c1%NOTFOUND;DBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);END LOOP;CLOSE c1; END;

Page 20: PLSQL1

-- Display details of Highest 10 salary paid employee

DECLARE

CURSOR c2 IS SELECT * FROM emp ORDER BY sal DESC;

e_rec emp%rowtype;

BEGIN

Open c2;

LOOP

FETCH c2 INTO e_rec;

DBMS_OUTPUT.PUT_LINE('Number: ' || ' ' || e_rec.empno);DBMS_OUTPUT.PUT_LINE('Name : ' || ' ' || e_rec.ename);DBMS_OUTPUT.PUT_LINE('Salary: ' || ' ' || e_rec.sal);EXIT WHEN c2%ROWCOUNT >= 10;END LOOP;

Close c2;

END;

Points to remember while fetching a row:

· We can fetch the rows in a cursor to a PL/SQL Record or a list of variables created in the PL/SQL Block. · If you are fetching a cursor to a PL/SQL Record, the record should have the same structure as the cursor.· If you are fetching a cursor to a list of variables, the variables should be listed in the same order in the fetch statement as the columns are present in the cursor.

Explicit cursor attributes

Page 21: PLSQL1

%FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN.

Page 22: PLSQL1
Page 23: PLSQL1

The DUAL table is a special one-row table present by default in all Oracle database installations. It is suitable for use in selecting a pseudocolumn such as SYSDATE or USER The table has a single VARCHAR2(1) column called DUMMY that has a value of "X"

It's the special table in Oracle, often used for calculation or checking system variable. For example;

"Select 2*4 from dual" prints out the result of the calculation "Select sysdate from dual" prints the server current date.

STRONG Cursors:

Ref cursors can be dynamically opened or opened on the logic.

DECLARE

TYPE ecursor IS REF CURSOR RETURN emp%ROWTYPE;

/*CURSOR c2 IS SELECT * FROM emp ORDER BY sal DESC;*/

ecur ecursor;e_rec emp%ROWTYPE;

dn NUMBER;

BEGIN

dn := &deptno;

OPEN ecur FOR SELECT * FROM emp WHERE deptno = dn;

LOOPfetch ecur into e_rec;

DBMS_OUTPUT.PUT_LINE ('Employee No : ' || e_rec.empno);

DBMS_OUTPUT.PUT_LINE ('Employee Salary: ' || e_rec.sal);

EXIT WHEN ecur%NOTFOUND;

END LOOP;

close ecur;

END;

Page 24: PLSQL1

Weak REF cursors;

DECLARETYPE tcursor IS REF CURSOR;

tcur tcursor;e1 emp%ROWTYPE;d1 dept%ROWTYPE;tname varchar(10);BEGINtname := '&tablename';IF tname = 'emp' THENOPEN tcur FOR SELECT * FROM emp;DBMS_OUTPUT.PUT_LINE ('Emp table opened.');close tcur;DBMS_OUTPUT.PUT_LINE ('Emp table closed.');ELSIF tname = 'dept' THENOPEN tcur FOR SELECT * FROM dept;DBMS_OUTPUT.PUT_LINE ('Dept table opened.');close tcur;DBMS_OUTPUT.PUT_LINE ('Emp table closed.');ELSERAISE_APPLICATION_ERROR (-20004, 'Table name is wrong');END IF;END;

Page 25: PLSQL1

Stored ProceduresWhat is a Stored Procedure?

A stored procedure or in simple a proc is a named PL/SQL block which performs one or more specific task. This is similar to a procedure in other programming languages. A procedure has a header and a body.

The header consists of the name of the procedure and the parameters or variables passed to the procedure.

The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block.

A procedure is similar to an anonymous PL/SQL Block but it is named for repeated usage.

We can pass parameters to procedures in three ways. 1) IN-parameters2) OUT-parameters3) IN OUT-parameters

A procedure may or may not return any value.

General Syntax to create a procedure is:

CREATE OR REPLACE PROCEDURE [schema] procedure name (argument { IN, OUT, IN OUT} data type, ..) {IS, AS}variable declarations; constant declarations;BEGINpl/sql subprogram body;EXCEPTIONexception pl/sql block;END;

Note:IS/AS - marks the beginning of the body of the procedureThe code between IS and BEGIN forms the Declaration section.

Page 26: PLSQL1

--PROCEDURE USING NO ARGUMENT..AND USING CURSOR

CREATE OR REPLACE PROCEDURE P2 IS

cursor cur1 is select * from emp;

erec emp%rowtype;

begin

OPEN cur1;

LOOPFETCH cur1 INTO erec;

EXIT WHEN cur1%NOTFOUND;

dbms_output.put_line(erec.ename);

end loop;

close cur1;end;

/* the other way of writing the same */

CREATE OR REPLACE PROCEDURE P2 IScursor cur1 is select * from emp;beginfor erec in cur1 loopdbms_output.put_line(erec.ename);end loop;end;

Page 27: PLSQL1

How to pass parameters to Procedures and Functions in PL/SQL ?

In PL/SQL, we can pass parameters to procedures and functions in three ways.

1) IN type parameter: These types of parameters are used to send values to stored procedures. 2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions.3) IN OUT parameter: These types of parameters are used to send values and get values from stored procedures.

1) IN parameter:

This is similar to passing parameters in programming languages. We can pass values to the stored procedure through these parameters or variables. This type of parameter is a read only parameter. We can assign the value of IN type parameter to a variable or use it in a query, but we cannot change its value inside the procedure.

CREATE OR REPLACE PROCEDURE SQR( X IN NUMBER) ISBEGINdbms_output.put_line(x*x);end;

2) OUT Parameter:

The OUT parameters are used to send the OUTPUT from a procedure or a function. This is a write-only parameter i.e, we cannot pass values to OUT paramters while executing the stored procedure, but we can assign values to OUT parameter inside the stored procedure and the calling program can recieve this output value.

CREATE OR REPLACE PROCEDURE getempname(id IN NUMBER, salary OUT number)ISBEGINselect sal into salaryfrom emp where empno=id;END;

Page 28: PLSQL1

DECLARE

CURSOR cur IS SELECT empno, ename FROM emp;

salary number;

BEGIN

FOR emprec in cur

LOOP

getempname(emprec.empno, salary);

dbms_output.put_line('The employee '|| emprec.ename || ' with id ' || emprec.empno ||' has salary '|| salary);

END LOOP;END;

To get Name from procedure using OUT parameter(***ERROR***)

CREATE OR REPLACE PROCEDURE getempname(id IN NUMBER, name OUT emp.ename%type)

IS

BEGIN

select ename

into name

from emp

where empno=id;

END;

Page 29: PLSQL1

INOUT Parameter

create or replace procedure getsal( salary in out number)isbeginselect sal into salary from empwhere empno = salary;end ;

declaresalary number := 7900;begingetsal(salary);dbms_output.put_line('The employee salary is' || salary);end ;

Page 30: PLSQL1

Triggers

A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database table. A trigger is triggered automatically when an associated DML statement is executed.

Advantages of database triggers:---> Data is generated on it's own---> Replicate table can be maintained---> To enforce complex integrity contraints---> To edit data modifications---> To auto increment a field

CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF } {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name [REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] WHEN (condition) /* trigger condition */

BEGIN /* trigger body */ --- sql statements END;

{BEFORE | AFTER | INSTEAD OF } -what time should the trigger get fired. i.e for example: before or after updating a table. INSTEAD OF is used to create a trigger on a view. before and after cannot be used to create a trigger on a view.{INSERT [OR] | UPDATE [OR] | DELETE} - This clause determines the triggering event. More than one triggering events can be used together separated by OR keyword. The trigger gets fired at all the specified triggering event.[OF col_name] - This clause is used with update triggers. This clause is used when you want to trigger an event only when a specific column is updated.[ON table_name] - This clause identifies the name of the table or view to which the trigger is associated.[REFERENCING OLD AS o NEW AS n] - This clause is used to reference the old and new values of the data being changed. By default, you reference the values as :old.column_name or :new.column_name. The reference names can also be changed from old (or new) to any

Page 31: PLSQL1

other user-defined name. You cannot reference old values when inserting a record, or new values when deleting a record, because they do not exist.[FOR EACH ROW] - This clause is used to determine whether a trigger must fire when each row gets affected ( i.e. a Row Level Trigger) or just once when the entire sql statement is executed(i.e.statement level Trigger).WHEN (condition) - This clause is valid only for row level triggers. The trigger is fired only for rows that satisfy the condition specified.

<trigger_body> is a PL/SQL block, rather than sequence of SQL statements. Oracle has placed certain restrictions on what you can do in <trigger_body>, in order to avoid situations where one trigger performs an action that triggers a second trigger, which then triggers a third, and so on, which could potentially create an infinite loop. The restrictions on <trigger_body> include:

o You cannot modify the same relation whose modification is the event triggering the trigger.

o You cannot modify a relation connected to the triggering relation by another constraint such as a foreign-key constraint.

DROP TABLE SALARY;CREATE TABLE SALARY (JOB VARCHAR2(9) primary key, MINSAL NUMBER(7,2), MAXSAL NUMBER(7,2) );

INSERT INTO SALARY VALUES ('CLERK', 800, 1300);INSERT INTO SALARY VALUES ('ANALYST', 3000, 3500);INSERT INTO SALARY VALUES ('SALESMAN', 1250, 1600);INSERT INTO SALARY VALUES ('MANAGER', 2450, 2975);INSERT INTO SALARY VALUES ('PRESIDENT', 5000, 5500);

Page 32: PLSQL1

create or replace trigger check_salary_EMPbefore insert or update of SAL, JOB on EMPfor each rowwhen (new.JOB != 'PRESIDENT')declare

minsal number;maxsal number;

begin

select MINSAL, MAXSAL into minsal, maxsal from SALARY where JOB = :new.JOB;

if :new.SAL < minsal or :new.SAL > maxsal thenraise_application_error(-20225, 'Salary range exceeded');

elsif :new.SAL < :old.SAL thendbms_output.put_line(‘Salary has been decreased’);

elsif :new.SAL > 1.1*:old.SAL thendbms_output.put_line(‘Salary has been increased by 10 %’);

increase');end if;

end;/

update emp set sal=1600 where empno=7369;

update emp set sal=1300 where empno=7369;

update emp set sal=4000 where empno=7369;

update emp set sal=900 where job=’CLERK’;

Before & After Triggers:

Before

Triggers execute the trigger action before the triggering statement.

Page 33: PLSQL1

By using BEFORE trigger, user can eliminate unnecessary processing of the triggering statement.

Are used to derive specific column values before completing a triggering INSERT or UPDATE statement.

After

Triggers execute the trigger action after the triggering statement. If a before trigger already exists an after trigger can perform different actions on the

same triggering statement.

CREATE TABLE T4 (a INTEGER, b CHAR(10));

CREATE TABLE T5 (c CHAR(10), d INTEGER);

CREATE TRIGGER trig1    AFTER INSERT ON T4    REFERENCING NEW AS newRow    FOR EACH ROW    WHEN (newRow.a <= 10)    BEGIN        INSERT INTO T5 VALUES(:newRow.b, :newRow.a);    END;

Displaying Trigger Definition Errorsshow errors trigger <trigger_name>;

Viewing Defined Triggers

select trigger_name from user_triggers;

-- A database trigger that allows changes to employee table only during the business hours(i.e. from 8 a.m to 5.00 p.m.) from monday to saturday. There is no restriction on viewing data from the table

Page 34: PLSQL1

CREATE OR REPLACE TRIGGER Time_Check BEFORE INSERT OR UPDATE OR DELETE ON EMP BEGIN IF TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) < 10 OR TO_NUMBER(TO_CHAR(SYSDATE,'hh24')) >= 17 OR TO_CHAR(SYSDATE,'DAY') = 'SAT' OR TO_CHAR(SYSDATE,'DAY') = 'SAT' THENRAISE_APPLICATION_ERROR (-20004,'YOU CAN ACCESS ONLY BETWEEN 10 AM TO 5 PM ON MONDAY TO FRIDAY ONLY.');END IF;END;

Page 35: PLSQL1

ActiveX

ActiveX is the set of technologies that allow separately compiled components to communicate with one another. It allows you to develop components in many different languages such as VC++, Java, Excel, and Visual Basic and have all of the components work together.

What is an ActiveX DLL?An ActiveX DLL is a code component that runs in the same process as the client application. So it runs in the same address space as the client application that is using the component.

Any components you create to work with Microsoft Transaction Server must be ActiveX DLL components. Any other components you develop for use on the same machine as the client application can also be ActiveX DLL components. This is normally the preferred choice because it is easier to test (by adding another project using the new VB5 project group feature) and has better performance.

Steps to create an ActiveX DLL

1. Select a new project as Active X DLL2. Add functions/procedures to the class provided.Give name for procedure, functions, attributes, events etc.

3. Make File as DLLFile -> Make -> <projectname>.DLL

4. Save File/project in System32 folder5. Register the DLL

RUN-> regsvr32<projectname>

DLL to Add two numbers.

Public Function AddTwoNumbers(ByVal a As Integer, ByVal b As Integer)

AddTwoNumbers = a + b

End Function

Page 36: PLSQL1

Steps to Include the ActiveX DLL component created in the project.

1. Go to Project-> References2. Select the DLL created which would be used in our application.3. Make use of the class -> function to use the component features.

Private Sub Command1_Click()

Dim a As New SUM.Class1

Dim sum1 As Integer

sum1 = a.AddTwoNumbers(9, 10)

MsgBox (sum1)

End Sub