Perl Tutorial
Perl Tutorial
1
Today’s Roadmap
l Views
l Triggers
l Cursors
l Stored Procedures
2
Using Select Inside Triggers
Create Trigger EmpDate
Before Insert On Employee
For Each Row - Execute Select inside trigger
Declare - Store the result in temp variables
temp date;
Begin
Select sysdate into temp from dual;
IF (:new.hireDate is null) Then Works fine if the Select
:new.hireDate := temp;
returns one tuple…
End IF;
End;
/ Create Trigger EmpDate
Before Insert On Employee
- Select two columns
For Each Row
Declare
into two variables
tempAge number;
tempName varchar2(100);
Begin
Select age, name into tempAge, tempName from R where ID = 5;
….
3
End;
/
Cursors: Introduction
l Select statement may return many records
Select empID, name, salary
From Employee
Get 0 or more records
Where salary > 120,000;
4
What is a Cursor
l A mechanism to navigate tuple-by-tuple over a relation
5
Creating a Cursor
Cursor name Any query can go here
Cursor HighSalEmp IS
Select empID, name, salary
From Employee
Where salary > 120,000;
6
Cursor Operations
Cursor HighSalEmp IS
l Create cursor Select empID, name, salary
From Employee
Where salary > 120,000;
Open HighSalEmp;
l Open cursor
l Execute the query and put the pointer at the first tuple
7
Example 1
l Have two tables: Customer & Product
l When insert a new customer
l Put in Marketing table, the customer ID along with the products labeled ‘OnSale’
Create Trigger NewCust
After Insert On Customer
For Each Row Define the cursor in ‘Declare’ section
Declare
pid number;
cursor C1 is Select product_id From Product Where label = 'OnSale';
Begin
Open the cursor
open C1;
Loop Loop over each record at a time
Fetch C1 Into pid;
IF (C1%Found) Then If the fetch returned a record
Insert into Marketing(Cust_id, Product_id) values (:new.Id, pid);
END IF;
Exit When C1%NotFound;
END Loop; Customer ID
close C1; Close the cursor 8
End; /
Example 2: Another way
Begin
Automatically opens the cursor and fetches
a record in each iteration
For rec In C1 Loop
Insert into Marketing(Cust_id, Product_id) values (:new.Id, rec.product_id);
End Loop;
End; /
Automatically closes the cursor That is how to read the rec
variable
9
Cursor Attributes
l These are attributes maintained by the system
10
Parameterized Cursor
l Cursors can take parameters while opening them
l Very powerful to customize their execution each time
12
Today’s Roadmap
l Views
l Triggers
l Assertions
l Cursors
l Stored Procedures
13
Stored Procedures & Functions
Views
14
Stored Procedures in Oracle
l Stored procedures in Oracle follow a
language called PL/SQL
15
Creating A Stored Procedure
If exists, then drop it and create it again ‘IS’ or ‘AS’ both are valid
Mode:
IN è input parameter (default)
OUT è output parameter
INOUT è input and output parameter
16
General Structure
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
[IS | AS]
[declaration_section]
BEGIN
executable_section Optional section for exception handling
[EXCEPTION
exception_section]
END [procedure_name];
17
Example I
Define a variable
By default, it is IN
18
Example II
Declaration section
Define a cursor that references the
input parameter
19
Calling a Stored Procedure
l SQL> exec <procedureName> [(<paramList>)];
20
Printing From Stored Procedures
Taking three parameters
Printing lines to
output screen
21
Features in Stored Procedures
IN parameters
BEGIN
ret_code := 10; Variable assignment
22
More Features: CURSOR &
FOR Statement
Create Procedure OpeningBal (p_type IN string) AS
cursor C1 Is
Select productId, name, price
From products
where type = p_type;
Begin
For rec in C1 Loop
Insert into Temp values (rec.productId, rec.name, rec.price);
End Loop;
End;
/
23
Return Value
l Stored procedures can set output variables
24
Stored Functions
l Similar to stored procedures except that they return value
CREATE [OR REPLACE] FUNCTION <functionName>
RETURN <type> [(<paramList>)] AS
<localDeclarations>
<functionBody>;
25
Stored Functions
26
Using Stored Procedures or
Functions
l Stored Procedures
l Called from other procedures, functions, triggers, or
standalone
27
Example I
30
Summary of Stored Procedures/
Functions
31
End of Advanced SQL
l Views
l Triggers
l Assertions
l Cursors
l Stored Procedures/Functions
32