Dbms Unit II Plsql Pnr 2 of 2
Dbms Unit II Plsql Pnr 2 of 2
END procedure_name;
Each PL/SQL subprogram has a name, and may have a parameter list. Like
anonymous PL/SQL blocks and, the named blocks a subprograms will also
have following three parts:
1 Declarative Part
It is an optional part. However, the declarative part for a subprogram does not start
with the DECLARE keyword. It contains declarations of types, cursors, constants,
variables, exceptions, and nested subprograms. These items are local to the
subprogram and cease to exist when the subprogram completes execution.
2 Executable Part
This is a mandatory part and contains statements that perform the designated
action.
3 Exception-handling
This is again an optional part. It contains the code that handles run-time errors.
stored procedure that selects offices located in a
particular country.
Mysql> DELIMITER //
Mysql> DELIMITER ;
➢ DELIMITER ; ➢ DELIMITER ;
Creating a Function
My sql:
CREATE FUNCTION function_name
[[IN | OUT | IN OUT] parameter_name type [, ...])]
RETURNS return_datatype
BEGIN
< function_body >
Return variable;
END;
Ex: Creating and calling a standalone function. This function returns the total
number of CUSTOMERS in the customers table
DECLARE
c number(2);
BEGIN
DECLARE
a number;
b number;
Maximum of (23,45): 45
PL/SQL procedure successfully completed.
Student_result(student_id, student_marks)Write procedure to display
grades for marks: less than 35 fail, less than 50 C grade less than 70 B
grade otherwise A grade
•When you use a statement that does not use INSERT, DELETE or
UPDATE statement to change data in a table, the triggers
associated with the table are not invoked. For example, the
TRUNCATE statement removes all data of a table but does
not invoke the trigger associated with that table.
MYSQL TRIGGER SYNTAX
CREATE TRIGGER trigger_name trigger_time
trigger_event
ON table_name
FOR EACH ROW
BEGIN
OR
DELIMITER $$
CREATE TRIGGER trigger_name
[BEFORE|AFTER] [INSERT|UPDATE|DELETE] ON table_name
FOR EACH ROW [FOLLOWS|PRECEDES existing_trigger_name]
BEGIN
…
END$$
DELIMITER ;
You put the trigger name after the CREATE TRIGGER
statement. The trigger name should follow the naming
convention [trigger time]_[table name]_[trigger event], for
example before_employees_update.
Row-Level Triggers
Row-level triggers execute once for each row in a transaction. Row-level
Statement-Level Triggers
Statement-level triggers execute once for each transaction. When we insert in
one transaction 20 rows to EMPLOYEE table , then statement-level trigger
is executed only once.
Schema – Event –
Employee Delete on
LeftOutEmployee Employee
When an employee leaves the company his record is deleted from
Action – Insert
on
LeftOutEmploy
ee
The Example for creating a trigger is:
create or replace trigger trigger1
after delete on Employee
for each row
begin
insert into LeftOutEmployee values
4) AFTER UPDATE, Row Level: This trigger will insert a record into the table
'product_check' after each row is updated.
DELIMITER ; DELIMITER ;
Alter Triggger trig_ Name disable/enable
Disable/Enable Triggger trig_ Name
To remove:
Drop Trigger Trig_Name
If you drop a table, any triggers for the table are also dropped.
DECLARE
total_rows number(2);
BEGIN
If you check the records in customers table, you will find that
DECLARE
c_id customers.id%type;
c_name customers.name%type;
Variable
Where finished is a variable to indicate that the cursor has reached the end of the
result set. Notice that the handler declaration must appear after variable and
cursor declaration inside the stored procedures.
Or
SQL>@parameter_cursor_demo
EMP_No: 4
EMP_Name: Zenia Sroll
EMP_Dept: Web Developer
EMP_Salary: 42k
68
EXAMPLE 1
69
EXAMPLE 2