SlideShare a Scribd company logo
Oracle DB
Subprograms
Simon Huang
simon581923@gmail.com
Agenda
• PL/SQL Units
• Anonymous Block
• Stroed PL/SQL Units
• PL/SQL Packages
參考文件
• Oracle Database Documentation Library
 http://www.oracle.com/pls/db112/homepage
• E25519
 Oracle Database PL/SQL Language Reference 11g Release 2 (11.2)
• E41502
 Oracle Database Advanced Application Developer's Guide 11g Release 2
(11.2)
PL/SQL Units
PL/SQL Units
• PL/SQL is a modern, block-structured programming
language. PL/SQL provides procedural constructs, such
as loops and conditional statements, that are not
available in standard SQL.
• You can directly enter SQL data manipulation language
(DML) statements inside PL/SQL blocks, and you can
use subprograms supplied by Oracle to perform data
definition language (DDL) statements.
• PL/SQL Units include
 Anonymous Blocks
 Stored PL/SQL Units
 Triggers
Anonymous Block
Anonymous block
• An anonymous block is a PL/SQL unit that has no name.
• An anonymous block consists of
 An optional declarative part
 An executable part
 And one or more optional exception handlers.
• The declarative part declares
 PL/SQL variables
 Exceptions handlers
 Cursors
• The executable part contains
 PL/SQL code
 SQL statements
 And can contain nested blocks.
• Exception handlers contain code that is invoked when the exception
is raised, either as a predefined PL/SQL exception (such as
NO_DATA_FOUND or ZERO_DIVIDE) or as an exception that you
define.
Anonymous block Example -
Simple
DECLARE
last_name VARCHAR2(10);
cursor c1 IS
SELECT LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 20
ORDER BY LAST_NAME;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO last_name;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(last_name);
END LOOP;
END;
Anonymous block Example -
Exception Handler
DECLARE
Emp_number INTEGER := 9999;
Emp_name VARCHAR2(10);
BEGIN
SELECT LAST_NAME INTO Emp_name
FROM EMPLOYEES
WHERE EMPLOYEE_ID = Emp_number;
DBMS_OUTPUT.PUT_LINE('Employee name is '
|| Emp_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No such employee: '
|| Emp_number);
END;
Anonymous block Example -
User-Defined Exception
DECLARE
Emp_name VARCHAR2(10);
Emp_number INTEGER;
Empno_out_of_range EXCEPTION;
BEGIN
Emp_number := 10001;
IF Emp_number > 9999 OR Emp_number < 1000 THEN
RAISE Empno_out_of_range;
ELSE
SELECT LAST_NAME INTO Emp_name
FROM EMPLOYEES
WHERE EMPLOYEE_ID = Emp_number;
DBMS_OUTPUT.PUT_LINE('Employee name is ' ||
Emp_name);
END IF;
EXCEPTION
WHEN Empno_out_of_range THEN
DBMS_OUTPUT.PUT_LINE('Employee number ' ||
Emp_number || ' is out of range.');
END;
Stored PL/SQL
Units
Stored PL/SQL Units
• A stored PL/SQL unit is a subprogram (procedure or
function) or package that:
 Has a name.
 Can take parameters, and can return values.
 Is stored in the data dictionary.
 Can be invoked by many users.
• If a subprogram belongs to a package, it is called a
package subprogram; if not, it is called a standalone
subprogram.
Stored Procedure with
Parameters
CREATE OR REPLACE PROCEDURE get_emp_names (
dept_num IN NUMBER ) IS
emp_name VARCHAR2(10);
CURSOR c1 (dept_num NUMBER) IS
SELECT LAST_NAME FROM EMPLOYEES
WHERE DEPARTMENT_ID = dept_num;
BEGIN
OPEN c1(dept_num);
LOOP
FETCH c1 INTO emp_name;
EXIT WHEN C1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(emp_name);
END LOOP;
CLOSE c1;
END;
Stored Procedure with
%TYPE and %ROWTYPE
CREATE OR REPLACE PROCEDURE get_emp_rec (
emp_number IN EMPLOYEES.EMPLOYEE_ID%TYPE,
emp_info OUT EMPLOYEES%ROWTYPE) IS
BEGIN
SELECT * INTO emp_info
FROM EMPLOYEES
WHERE EMPLOYEE_ID = emp_number;
END;
Function
CREATE FUNCTION get_bal(acc_no IN NUMBER)
RETURN NUMBER
IS acc_bal NUMBER(11,2);
BEGIN
SELECT order_total
INTO acc_bal
FROM orders
WHERE customer_id = acc_no;
RETURN(acc_bal);
END;
執行Procedure & Function
• EXEC get_emp_names(10);
• VARIABLE emp_info EMPLOYEES%ROWTYPE;
• EXEC get_emp_rec( 7369, : emp_info);
• SELECT get_bal(165) FROM DUAL;
PL/SQL Packages
PL/SQL Packages - Concept
• A package is a collection of related program objects (for
example, subprogram, variables, constants, cursors, and
exceptions) stored as a unit in the database.
• Packages is an alternative to creating subprograms as
standalone schema objects.
• Packages have many advantages over standalone
subprograms. For example, they:
 Let you organize your application development more efficiently.
 Let you grant privileges more efficiently.
 Let you modify package objects without recompiling dependent
schema objects.
 Enable Oracle Database to read multiple package objects into
memory at once.
 Can contain global variables and cursors that are available to all
subprograms in the package.
 Let you overload subprograms. Overloading a subprogram means
creating multiple subprograms with the same name in the same
package, each taking arguments of different number or data type.
PL/SQL Packages - Concept
• A package include two parts: the specification part and
the body of a package.
• The specification part of a package declares the public
types, variables, constants, and subprograms that are
visible outside the immediate scope of the package.
• The body of a package defines both the objects declared
in the specification and private objects that are not
visible to applications outside the package.
Package Specification -
Example
-- Package specification:
CREATE or REPLACE PACKAGE employee_management IS
FUNCTION hire_emp (
firstname VARCHAR2,
lastname VARCHAR2,
email VARCHAR2,
phone VARCHAR2,
hiredate DATE,
job VARCHAR2,
sal NUMBER,
comm NUMBER,
mgr NUMBER,
deptno NUMBER
) RETURN NUMBER;
PROCEDURE fire_emp(
emp_id IN NUMBER
);
PROCEDURE sal_raise (
emp_id IN NUMBER,
sal_incr IN NUMBER
);
END employee_management;
Package Body – Example I
-- Package body:
CREATE or REPLACE PACKAGE BODY employee_management IS
FUNCTION hire_emp (
firstname VARCHAR2,
lastname VARCHAR2,
email VARCHAR2,
phone VARCHAR2,
hiredate DATE,
job VARCHAR2,
sal NUMBER,
comm NUMBER,
mgr NUMBER,
deptno NUMBER
) RETURN NUMBER IS
new_empno NUMBER(10);
BEGIN
new_empno := emp_sequence.NEXTVAL;
INSERT INTO EMPLOYEES (
employee_id, first_name, last_name, email, phone_number, hire_date,
job_id, salary, commission_pct, manager_id, department_id)
VALUES (
new_empno, firstname, lastname, email, phone, hiredate,
job, sal, comm, mgr, deptno);
RETURN (new_empno);
END hire_emp;
Package Body – Example II
PROCEDURE fire_emp (
emp_id IN NUMBER) IS
BEGIN
DELETE FROM EMPLOYEES
WHERE EMPLOYEE_ID = emp_id;
IF SQL%NOTFOUND THEN
raise_application_error(
-20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id));
END IF;
END fire_emp;
PROCEDURE sal_raise (
emp_id IN NUMBER,
sal_incr IN NUMBER) IS
BEGIN
UPDATE EMPLOYEES
SET SALARY = SALARY + sal_incr
WHERE EMPLOYEE_ID = emp_id;
IF SQL%NOTFOUND THEN
raise_application_error(
-20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id));
END IF;
END sal_raise;
END employee_management;
Invoke package procedures
DECLARE
empno NUMBER(6);
sal NUMBER(6);
temp NUMBER(6);
BEGIN
empno := employee_management.hire_emp(
'John', 'Doe', 'john.doe@company.com', '555-0100',
'20-SEP-07', 'ST_CLERK', 2500, 0, 100, 20);
DBMS_OUTPUT.PUT_LINE(
'New employee ID is ' || TO_CHAR(empno));
END;
Q & A

More Related Content

What's hot

PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
Bharat Kalia
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
DataminingTools Inc
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
Esmita Gupta
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
Gurpreet singh
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
Pooja Dixit
 
Plsql overview
Plsql overviewPlsql overview
Plsql overview
Gagan Deep
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledge
Masood Khan
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
Srinath Maharana
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
Jafar Nesargi
 
plsql les02
 plsql les02 plsql les02
plsql les02
sasa_eldoby
 
PLSQL
PLSQLPLSQL
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar
rehaniltifat
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
rehaniltifat
 
plsql les03
 plsql les03 plsql les03
plsql les03
sasa_eldoby
 
Plsql les04
Plsql les04Plsql les04
Plsql les04
sasa_eldoby
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
Quang Minh Đoàn
 
Plsql
PlsqlPlsql
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
mohdoracle
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
DataminingTools Inc
 

What's hot (20)

PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Plsql overview
Plsql overviewPlsql overview
Plsql overview
 
pl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledgepl/sql online Training|sql online Training | iTeknowledge
pl/sql online Training|sql online Training | iTeknowledge
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
plsql les02
 plsql les02 plsql les02
plsql les02
 
PLSQL
PLSQLPLSQL
PLSQL
 
11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar11 Understanding and Influencing the PL/SQL Compilar
11 Understanding and Influencing the PL/SQL Compilar
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
plsql les03
 plsql les03 plsql les03
plsql les03
 
Plsql les04
Plsql les04Plsql les04
Plsql les04
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Plsql
PlsqlPlsql
Plsql
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 

Viewers also liked

Secure Technical Implementation Guide for databases by Martin Obst
Secure Technical Implementation Guide for databases by Martin ObstSecure Technical Implementation Guide for databases by Martin Obst
Secure Technical Implementation Guide for databases by Martin Obst
Carsten Muetzlitz
 
Oracle security 08-oracle network security
Oracle security 08-oracle network securityOracle security 08-oracle network security
Oracle security 08-oracle network security
Zhaoyang Wang
 
Auditing security of Oracle DB (Karel Miko)
Auditing security of Oracle DB (Karel Miko)Auditing security of Oracle DB (Karel Miko)
Auditing security of Oracle DB (Karel Miko)
DCIT, a.s.
 
Oracle security 02-administering user security
Oracle security 02-administering user securityOracle security 02-administering user security
Oracle security 02-administering user security
Zhaoyang Wang
 
Osobní bezpečnost na internetu
Osobní bezpečnost na internetuOsobní bezpečnost na internetu
Osobní bezpečnost na internetu
DCIT, a.s.
 
1 z0 052
1 z0 0521 z0 052
1 z0 052
Wasim Ahmed
 
Oracle Berkeley Db 11g R2
Oracle Berkeley Db 11g R2Oracle Berkeley Db 11g R2
Oracle Berkeley Db 11g R2
Prem Kumar
 
Oracle Compute Cloud Service快速实践
Oracle Compute Cloud Service快速实践Oracle Compute Cloud Service快速实践
Oracle Compute Cloud Service快速实践
Zhaoyang Wang
 

Viewers also liked (8)

Secure Technical Implementation Guide for databases by Martin Obst
Secure Technical Implementation Guide for databases by Martin ObstSecure Technical Implementation Guide for databases by Martin Obst
Secure Technical Implementation Guide for databases by Martin Obst
 
Oracle security 08-oracle network security
Oracle security 08-oracle network securityOracle security 08-oracle network security
Oracle security 08-oracle network security
 
Auditing security of Oracle DB (Karel Miko)
Auditing security of Oracle DB (Karel Miko)Auditing security of Oracle DB (Karel Miko)
Auditing security of Oracle DB (Karel Miko)
 
Oracle security 02-administering user security
Oracle security 02-administering user securityOracle security 02-administering user security
Oracle security 02-administering user security
 
Osobní bezpečnost na internetu
Osobní bezpečnost na internetuOsobní bezpečnost na internetu
Osobní bezpečnost na internetu
 
1 z0 052
1 z0 0521 z0 052
1 z0 052
 
Oracle Berkeley Db 11g R2
Oracle Berkeley Db 11g R2Oracle Berkeley Db 11g R2
Oracle Berkeley Db 11g R2
 
Oracle Compute Cloud Service快速实践
Oracle Compute Cloud Service快速实践Oracle Compute Cloud Service快速实践
Oracle Compute Cloud Service快速实践
 

Similar to Oracle db subprograms

Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
sweetysweety8
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
faizan992426
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
Vinay Kumar
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
PrabhatKumar591
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
jain.pralabh
 
plsql Les05
plsql Les05 plsql Les05
plsql Les05
sasa_eldoby
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
rehaniltifat
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
Rushdi Shams
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
Thuan Nguyen
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
Khadija Parween
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
Belal Arfa
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
rehaniltifat
 
The Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result CacheThe Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result Cache
Steven Feuerstein
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPT
SujayaBiju
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_material
gayaramesh
 
pl_sql.ppt
pl_sql.pptpl_sql.ppt
pl_sql.ppt
Prabhat106214
 
Store programs
Store programsStore programs
Store programs
Hitesh Kumar Markam
 
Dbms 2011
Dbms 2011Dbms 2011
Dbms 2011
Atiqa Khan
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
Danish Mehraj
 
Oracle DBA interview_questions
Oracle DBA interview_questionsOracle DBA interview_questions
Oracle DBA interview_questions
Naveen P
 

Similar to Oracle db subprograms (20)

Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
plsql.ppt
plsql.pptplsql.ppt
plsql.ppt
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
Pl sql chapter 1
Pl sql chapter 1Pl sql chapter 1
Pl sql chapter 1
 
Procedures/functions of rdbms
Procedures/functions of rdbmsProcedures/functions of rdbms
Procedures/functions of rdbms
 
plsql Les05
plsql Les05 plsql Les05
plsql Les05
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
Dynamic websites lec3
Dynamic websites lec3Dynamic websites lec3
Dynamic websites lec3
 
08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata08 Dynamic SQL and Metadata
08 Dynamic SQL and Metadata
 
The Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result CacheThe Amazing and Elegant PL/SQL Function Result Cache
The Amazing and Elegant PL/SQL Function Result Cache
 
Introduction to PLSQL.PPT
Introduction to PLSQL.PPTIntroduction to PLSQL.PPT
Introduction to PLSQL.PPT
 
Unit 4 rdbms study_material
Unit 4  rdbms study_materialUnit 4  rdbms study_material
Unit 4 rdbms study_material
 
pl_sql.ppt
pl_sql.pptpl_sql.ppt
pl_sql.ppt
 
Store programs
Store programsStore programs
Store programs
 
Dbms 2011
Dbms 2011Dbms 2011
Dbms 2011
 
PL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics CoveredPL/SQL Complete Tutorial. All Topics Covered
PL/SQL Complete Tutorial. All Topics Covered
 
Oracle DBA interview_questions
Oracle DBA interview_questionsOracle DBA interview_questions
Oracle DBA interview_questions
 

More from Simon Huang

企業資源規劃(Erp)系統實務2.0
企業資源規劃(Erp)系統實務2.0企業資源規劃(Erp)系統實務2.0
企業資源規劃(Erp)系統實務2.0
Simon Huang
 
Mvc model
Mvc modelMvc model
Mvc model
Simon Huang
 
Sql server performance Tuning
Sql server performance TuningSql server performance Tuning
Sql server performance Tuning
Simon Huang
 
ASP.NET MVC The Begining
ASP.NET MVC The BeginingASP.NET MVC The Begining
ASP.NET MVC The Begining
Simon Huang
 
企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃
Simon Huang
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
Simon Huang
 
Oracle db architecture
Oracle db architectureOracle db architecture
Oracle db architecture
Simon Huang
 
關聯式資料庫系統的規劃
關聯式資料庫系統的規劃關聯式資料庫系統的規劃
關聯式資料庫系統的規劃
Simon Huang
 
在台灣導入Open Source ERP的問題與對策
在台灣導入Open Source ERP的問題與對策在台灣導入Open Source ERP的問題與對策
在台灣導入Open Source ERP的問題與對策
Simon Huang
 

More from Simon Huang (10)

企業資源規劃(Erp)系統實務2.0
企業資源規劃(Erp)系統實務2.0企業資源規劃(Erp)系統實務2.0
企業資源規劃(Erp)系統實務2.0
 
Mvc model
Mvc modelMvc model
Mvc model
 
Sql server performance Tuning
Sql server performance TuningSql server performance Tuning
Sql server performance Tuning
 
ASP.NET MVC The Begining
ASP.NET MVC The BeginingASP.NET MVC The Begining
ASP.NET MVC The Begining
 
企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃企業資源規劃(Erp)系統導入規劃
企業資源規劃(Erp)系統導入規劃
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
 
Oracle db architecture
Oracle db architectureOracle db architecture
Oracle db architecture
 
Portable db
Portable dbPortable db
Portable db
 
關聯式資料庫系統的規劃
關聯式資料庫系統的規劃關聯式資料庫系統的規劃
關聯式資料庫系統的規劃
 
在台灣導入Open Source ERP的問題與對策
在台灣導入Open Source ERP的問題與對策在台灣導入Open Source ERP的問題與對策
在台灣導入Open Source ERP的問題與對策
 

Recently uploaded

Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 

Recently uploaded (20)

Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 

Oracle db subprograms

  • 2. Agenda • PL/SQL Units • Anonymous Block • Stroed PL/SQL Units • PL/SQL Packages
  • 3. 參考文件 • Oracle Database Documentation Library  http://www.oracle.com/pls/db112/homepage • E25519  Oracle Database PL/SQL Language Reference 11g Release 2 (11.2) • E41502  Oracle Database Advanced Application Developer's Guide 11g Release 2 (11.2)
  • 5. PL/SQL Units • PL/SQL is a modern, block-structured programming language. PL/SQL provides procedural constructs, such as loops and conditional statements, that are not available in standard SQL. • You can directly enter SQL data manipulation language (DML) statements inside PL/SQL blocks, and you can use subprograms supplied by Oracle to perform data definition language (DDL) statements. • PL/SQL Units include  Anonymous Blocks  Stored PL/SQL Units  Triggers
  • 7. Anonymous block • An anonymous block is a PL/SQL unit that has no name. • An anonymous block consists of  An optional declarative part  An executable part  And one or more optional exception handlers. • The declarative part declares  PL/SQL variables  Exceptions handlers  Cursors • The executable part contains  PL/SQL code  SQL statements  And can contain nested blocks. • Exception handlers contain code that is invoked when the exception is raised, either as a predefined PL/SQL exception (such as NO_DATA_FOUND or ZERO_DIVIDE) or as an exception that you define.
  • 8. Anonymous block Example - Simple DECLARE last_name VARCHAR2(10); cursor c1 IS SELECT LAST_NAME FROM EMPLOYEES WHERE DEPARTMENT_ID = 20 ORDER BY LAST_NAME; BEGIN OPEN c1; LOOP FETCH c1 INTO last_name; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE(last_name); END LOOP; END;
  • 9. Anonymous block Example - Exception Handler DECLARE Emp_number INTEGER := 9999; Emp_name VARCHAR2(10); BEGIN SELECT LAST_NAME INTO Emp_name FROM EMPLOYEES WHERE EMPLOYEE_ID = Emp_number; DBMS_OUTPUT.PUT_LINE('Employee name is ' || Emp_name); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('No such employee: ' || Emp_number); END;
  • 10. Anonymous block Example - User-Defined Exception DECLARE Emp_name VARCHAR2(10); Emp_number INTEGER; Empno_out_of_range EXCEPTION; BEGIN Emp_number := 10001; IF Emp_number > 9999 OR Emp_number < 1000 THEN RAISE Empno_out_of_range; ELSE SELECT LAST_NAME INTO Emp_name FROM EMPLOYEES WHERE EMPLOYEE_ID = Emp_number; DBMS_OUTPUT.PUT_LINE('Employee name is ' || Emp_name); END IF; EXCEPTION WHEN Empno_out_of_range THEN DBMS_OUTPUT.PUT_LINE('Employee number ' || Emp_number || ' is out of range.'); END;
  • 12. Stored PL/SQL Units • A stored PL/SQL unit is a subprogram (procedure or function) or package that:  Has a name.  Can take parameters, and can return values.  Is stored in the data dictionary.  Can be invoked by many users. • If a subprogram belongs to a package, it is called a package subprogram; if not, it is called a standalone subprogram.
  • 13. Stored Procedure with Parameters CREATE OR REPLACE PROCEDURE get_emp_names ( dept_num IN NUMBER ) IS emp_name VARCHAR2(10); CURSOR c1 (dept_num NUMBER) IS SELECT LAST_NAME FROM EMPLOYEES WHERE DEPARTMENT_ID = dept_num; BEGIN OPEN c1(dept_num); LOOP FETCH c1 INTO emp_name; EXIT WHEN C1%NOTFOUND; DBMS_OUTPUT.PUT_LINE(emp_name); END LOOP; CLOSE c1; END;
  • 14. Stored Procedure with %TYPE and %ROWTYPE CREATE OR REPLACE PROCEDURE get_emp_rec ( emp_number IN EMPLOYEES.EMPLOYEE_ID%TYPE, emp_info OUT EMPLOYEES%ROWTYPE) IS BEGIN SELECT * INTO emp_info FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_number; END;
  • 15. Function CREATE FUNCTION get_bal(acc_no IN NUMBER) RETURN NUMBER IS acc_bal NUMBER(11,2); BEGIN SELECT order_total INTO acc_bal FROM orders WHERE customer_id = acc_no; RETURN(acc_bal); END;
  • 16. 執行Procedure & Function • EXEC get_emp_names(10); • VARIABLE emp_info EMPLOYEES%ROWTYPE; • EXEC get_emp_rec( 7369, : emp_info); • SELECT get_bal(165) FROM DUAL;
  • 18. PL/SQL Packages - Concept • A package is a collection of related program objects (for example, subprogram, variables, constants, cursors, and exceptions) stored as a unit in the database. • Packages is an alternative to creating subprograms as standalone schema objects. • Packages have many advantages over standalone subprograms. For example, they:  Let you organize your application development more efficiently.  Let you grant privileges more efficiently.  Let you modify package objects without recompiling dependent schema objects.  Enable Oracle Database to read multiple package objects into memory at once.  Can contain global variables and cursors that are available to all subprograms in the package.  Let you overload subprograms. Overloading a subprogram means creating multiple subprograms with the same name in the same package, each taking arguments of different number or data type.
  • 19. PL/SQL Packages - Concept • A package include two parts: the specification part and the body of a package. • The specification part of a package declares the public types, variables, constants, and subprograms that are visible outside the immediate scope of the package. • The body of a package defines both the objects declared in the specification and private objects that are not visible to applications outside the package.
  • 20. Package Specification - Example -- Package specification: CREATE or REPLACE PACKAGE employee_management IS FUNCTION hire_emp ( firstname VARCHAR2, lastname VARCHAR2, email VARCHAR2, phone VARCHAR2, hiredate DATE, job VARCHAR2, sal NUMBER, comm NUMBER, mgr NUMBER, deptno NUMBER ) RETURN NUMBER; PROCEDURE fire_emp( emp_id IN NUMBER ); PROCEDURE sal_raise ( emp_id IN NUMBER, sal_incr IN NUMBER ); END employee_management;
  • 21. Package Body – Example I -- Package body: CREATE or REPLACE PACKAGE BODY employee_management IS FUNCTION hire_emp ( firstname VARCHAR2, lastname VARCHAR2, email VARCHAR2, phone VARCHAR2, hiredate DATE, job VARCHAR2, sal NUMBER, comm NUMBER, mgr NUMBER, deptno NUMBER ) RETURN NUMBER IS new_empno NUMBER(10); BEGIN new_empno := emp_sequence.NEXTVAL; INSERT INTO EMPLOYEES ( employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id) VALUES ( new_empno, firstname, lastname, email, phone, hiredate, job, sal, comm, mgr, deptno); RETURN (new_empno); END hire_emp;
  • 22. Package Body – Example II PROCEDURE fire_emp ( emp_id IN NUMBER) IS BEGIN DELETE FROM EMPLOYEES WHERE EMPLOYEE_ID = emp_id; IF SQL%NOTFOUND THEN raise_application_error( -20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id)); END IF; END fire_emp; PROCEDURE sal_raise ( emp_id IN NUMBER, sal_incr IN NUMBER) IS BEGIN UPDATE EMPLOYEES SET SALARY = SALARY + sal_incr WHERE EMPLOYEE_ID = emp_id; IF SQL%NOTFOUND THEN raise_application_error( -20011, 'Invalid Employee Number: ‘ || TO_CHAR(Emp_id)); END IF; END sal_raise; END employee_management;
  • 23. Invoke package procedures DECLARE empno NUMBER(6); sal NUMBER(6); temp NUMBER(6); BEGIN empno := employee_management.hire_emp( 'John', 'Doe', 'john.doe@company.com', '555-0100', '20-SEP-07', 'ST_CLERK', 2500, 0, 100, 20); DBMS_OUTPUT.PUT_LINE( 'New employee ID is ' || TO_CHAR(empno)); END;
  • 24. Q & A