0% found this document useful (0 votes)
45 views23 pages

Procedure and Function: IF-2213 Pemrograman Basis Data Tahun Ajaran 2012-2013

The document discusses SQL procedural language (PSM) and PL/SQL, including basic syntax for procedures and functions in PL/SQL. It also covers parameters, variables, conditional statements, exceptions, and examples of procedures and functions that demonstrate these concepts. The document provides information on creating, calling, and dropping procedures and functions in PL/SQL.

Uploaded by

muhamadiruel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views23 pages

Procedure and Function: IF-2213 Pemrograman Basis Data Tahun Ajaran 2012-2013

The document discusses SQL procedural language (PSM) and PL/SQL, including basic syntax for procedures and functions in PL/SQL. It also covers parameters, variables, conditional statements, exceptions, and examples of procedures and functions that demonstrate these concepts. The document provides information on creating, calling, and dropping procedures and functions in PL/SQL.

Uploaded by

muhamadiruel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

05/07/20

Procedure and Function

IF-2213 Pemrograman Basis Data


Tahun Ajaran 2012-2013

1
05/07/20
Bagian 1

2
05/07/20
SQL/PSM
• stands for Structured Query Language/Persistent Stored Modules
• was developed by the American National Standards Institute
(ANSI) as an extension to SQL
• first adopted in 1996
• provides procedural programmability in addition to the querying
commands of SQL
• standardizes procedural extensions for SQL, including flow of
control, condition handling, statement condition signals and
resignals, cursors and local variables, and assignment of
expressions to variables and parameters
• formalizes declaration and maintenance of persistent database
language routines (e.g., "stored procedures").
• In PSM, we define modules, which are collections of function and
procedure definitions, temporary relation declarations, and several
other optional declarations.

3
05/07/20
PL/SQL
• Procedural Language/Structured Query Language
• Oracle Corporation's procedural extension language for SQL and
the Oracle relational database

4
05/07/20
Basic Syntax Procedure in PL/SQL

5
05/07/20
Basic Syntax Function in PL/SQL

6
05/07/20
Parameters
• The parameters in a procedure are preceded by a "mode," which
is either IN, OUT or INOUT
– IN – the parameter is input-only
– OUT – the parameter is output-only
– INOUT – the parameter is input and output
• The parameters in a function may only be of mode IN

7
05/07/20
Variables
• The major datatypes in PL/SQL include NUMBER, INTEGER,
CHAR, VARCHAR2, DATE, TIMESTAMP.
– variable_name NUMBER(P[,S]) := VALUE;
– variable_name VARCHAR2(L) := 'Text';
– variable_name DATE := '01-Jan-2005';
• TO_DATE('31-12-2004','dd-mm-yyyy')
• TO_CHAR (date_string, format_string)
– variable_name Table_name.Column_name%type;

8
05/07/20
Example
CREATE OR REPLACE PROCEDURE AddNewStar (
p_Name IN MovieStar.name%TYPE,
p_BirthPlace IN MovieStar.birthPlace%TYPE,
p_Gender IN MovieStar.gender%TYPE,
p_BirthDate IN MovieStar.birthDate%TYPE) AS

BEGIN
INSERT INTO MovieStar (name,birthPlace,gender,birthDate)
VALUES (p_Name,p_BirthPlace,p_Gender,p_BirthDate);
END AddNewStar;

9
05/07/20
Example
CREATE OR REPLACE PROCEDURE ChangeAddress (
p_name IN Studio.name%TYPE,
p_address IN Studio.address%TYPE) AS

BEGIN
UPDATE Studio
SET adress = p_address
WHERE name = p_name;
END ChangeAddress;

10
05/07/20
Example
CREATE OR REPLACE FUNCTION After50s (
p_Title IN Movie.title%TYPE) RETURN VARCHAR2 AS

v_Year Movie.year%TYPE;
BEGIN
SELECT year INTO v_Year FROM Movie
WHERE title = p_Title;
RETURN v_Year;
END After50s;

11
05/07/20
Conditional Statements
IF <condition> THEN
<statement list>
ELSIF <condition> THEN
<statement list>
ELSIF
.......
ELSE <statement list >
END IF;

12
05/07/20
Conditional Statements
CASE
WHEN <condition> THEN <statement list>
WHEN <condition> THEN <statement list>
....
ELSE <statement list>
END CASE;

13
05/07/20
Example
CREATE OR REPLACE FUNCTION After50s (
p_Title IN Movie.title%TYPE) RETURN VARCHAR2 AS

v_Year Movie.year%TYPE;
BEGIN
SELECT year INTO v_Year FROM Movie
WHERE title = p_Title;
IF v_Year<1950 THEN
RETURN ‘No’;
ELSE
RETURN ‘Yes’;
END IF;
END After50s;

14
05/07/20
Dropping Procedures and Functions
• The syntax for dropping a procedure is
DROP PROCEDURE procedure_name;
• and the syntax for dropping a function is
DROP FUNCTION function_name;

15
05/07/20
The CALL Statement

Example:

SQL> CALL AddNewStar('sayalagi',‘Batam','F','12-12-2010');


Call completed.

SQL> BEGIN
2 DBMS_OUTPUT.PUT_LINE(After50s(‘Se7en’));
3 END;
4 /
Yes

16
05/07/20
Bagian 2

17
05/07/20
Exceptions Raised Inside Subprograms

• If an error occurs inside a subprogram, an exception is raised


• This exception may be user-defined or predefined

EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
<statement list>
[WHEN exception3 [OR exception4. . .] THEN
<statement list>]
[WHEN OTHERS THEN
<statement list>]

18
05/07/20
Example
CREATE OR REPLACE PROCEDURE RaiseSalary (
p_cert# IN MovieExec.cert#%TYPE,
p_percent IN NUMBER) AS

BEGIN
UPDATE MovieExec
SET netWorth = netWorth * (1 + p_percent/100)
WHERE cert# = p_cert#;
END RaiseSalary;

19
05/07/20
Example
CREATE OR REPLACE PROCEDURE RaiseSalary (
p_cert# IN MovieExec.cert#%TYPE, p_percent IN NUMBER) AS

e_toomuch EXCEPTION;

BEGIN
IF p_percent >= 0.30 THEN
RAISE e_toomuch;
END IF;
UPDATE MovieExec
SET netWorth = netWorth * (1 + p_percent/100)
WHERE cert# = p_cert#;

EXCEPTION
WHEN e_toomuch THEN
DBMS_OUTPUT.PUT_LINE('Amount is too high.');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: raise not processed.');
END RaiseSalary;

20
05/07/20
Oracle Standard Exception

Oracle Exception Oracle Explanation


Name Error
NOT_LOGGED_ON ORA- You are not logged on.
01012
LOGIN_DENIED ORA- Invalid username/password.
01017
NO_DATA_FOUND ORA- No data was returned
01403
TOO_MANY_ROWS ORA- You tried to execute a SELECT
01422 INTO statement and more than
one row was returned.
ZERO_DIVIDE ORA- Divide by zero error.
01476

21
05/07/20
Oracle Standard Exception

Oracle Exception Oracle Explanation


Name Error
INVALID_NUMBER ORA- Converting a string to a number
01722 was unsuccessful.
STORAGE_ERROR ORA- Out of memory.
06500
PROGRAM_ERROR ORA- Generic "Contact Oracle support"
06501 message.
VALUE_ERROR ORA- You tried to perform an operation
06504 and there was a error on a
conversion, truncation, or invalid
constraining of numeric or
character data.
DUP_VAL_ON_INDEX You try to insert the value with the

22
same primary key.
05/07/20
Example
CREATE OR REPLACE PROCEDURE StarMovie (
p_Birthdate IN MovieStar.birthdate%TYPE) AS

BEGIN
SELECT name FROM MovieStar
WHERE birthdate = p_Birthdate;

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE(‘No movie star born.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE(‘More than one
moviestar born.');
END StarMovie;

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy