SlideShare uma empresa Scribd logo
1 de 22
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Introduction to PL SQL Online Training Classes 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Function 
• Is a block with a name 
• Returns one value only. 
• The DECLARE key word is not used 
• Parameters can only be IN 
• Is stored 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Creating or Replacing a Function 
CREATE OR REPLACE FUNCTION fname( ) 
RETURN datatype IS 
BEGIN 
EXECPTION 
END; 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Creating or Replacing a Function 
CREATE OR REPLACE FUNCTION sum_dept_sal(p_deptno IN NUMBER) 
RETURN emp.sal%TYPE IS 
v_sum_sal emp.sal%TYPE; 
BEGIN 
SELECT SUM(sal) 
INTO v_sum_sal 
FROM emp 
WHERE deptno = p_deptno; 
RETURN v_sum_sal; 
END; 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Invoking a Function 
SET SERVEROUTPUT ON; 
DECLARE 
… 
v_sal emp.sal%TYPE; 
… 
BEGIN 
… 
v_sal := sum_dept_sal(10); 
DBMS_OUTPUT.PUT_LINE(TO_CHAR(v_sal)); 
… 
END; 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Example, passing parameters 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Exercise 
• Write down a procedure that displays all 
records (all columns) from EMP table. Make 
sure program should restricted to fix number 
of records of this table. Display Date and TIME 
portions of column that has data type DATE. 
• Write down a procedure that can insert large 
number of records entered at the time of its 
execution. You can use EMP table and 
appending counter with some columns’ values 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Packages 
• Package 
– Package Specification 
– Package Body 
• Invoking Package subprogram 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.u 
Package Specification 
CREATE OR REPLACE PACKAGE emp_info is 
v_count INTEGER; 
PROCEDURE insert_record( p_empno IN NUMBER 
, p_ename IN VARCHAR2 
, p_job IN VARCHAR2 
, p_sal IN NUMBER 
, p_co.ukm IN NUMBER 
, p_deptno IN VARCHAR2); 
PROCEDURE delete_record(p_empno IN NUMBER); 
FUNCTION sum_dept_sal( p_deptno IN dept.deptno%TYPE) RETURN is dept.sal%TYPE; 
END emp_info; 
/ 
k 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Package Body 
CREATE OR REPLACE PACKAGE BODY emp_info is` 
PROCEDURE insert_record(p_empno IN NUMBER,p_ename IN VARCHAR2, 
p_job IN VARCHAR2,p_sal IN NUMBER, 
p_co.ukm IN NUMBER,p_deptno IN VARCHAR2) IS 
BEGIN 
INSERT INTO EMP(empno,ename,job,sal,co.ukm,deptno) 
VALUES(p_empno,p_ename,p_job,p_sal,p_co.ukm,p_deptno); 
END insert_record; 
PROCEDURE delete_record(p_empno IN NUMBER) IS 
BEGIN 
DELETE FROM EMP 
WHERE empno=p_empno; 
END delete_record; 
FUNCTION sum_dept_sal(p_deptno IN NUMBER) RETURN emp.sal%TYPE IS 
v_sum_sal emp.sal%TYPE; 
BEGIN 
SELECT SUM(sal) 
INTO v_sum_sal 
FROM emp 
WHERE deptno = p_deptno; 
RETURN v_sum_sal; 
END; 
END emp_info; --end of package body 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Invoking Package Subprogram 
DECLARE 
v_sum_sal emp.sal%TYPE; 
BEGIN 
… 
v_sum_sal := emp_info.sum_dept_sal(10); 
… 
emp_info.delete_record(1234567); 
… 
END; 
/ 
Or 
SQL> exec emp_info.insert_record(…,…) 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Triggers 
• Is a stored subprogram associated with a 
table. 
• Are fired by an event 
• Are mainly used for 
– Security 
– Enforce co.ukplex integrity constraint 
– Prevent invalid transaction 
– Event logging 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Creating or Replacing Triggers 
CREATE OR REPLACE TRIGGER del_emp( p_empno emp.empno%TYPE) 
BEFORE DELETE ON emp 
FOR EACH ROW 
BEGIN 
INSERT INTO emp_audit 
VALUES(p_empno, USER, sysdate); 
END; 
/ 
info@quontrasolutions.co.uk
Trigger with function usage Example
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Exercise 
Write a trigger for the following: 
When a record is added or deleted from an 
employee table, DEPT.NoOfEmp column gets 
updated accordingly to number of employees 
in EMP table corresponding to department 
number. 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Cursors 
• Is a pointer to a row. 
• Its is mainly used with tables which return 
more than one row. 
• It is handled implicitly and explicitly. 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Cusrors 
CURSOR c_emp IS 
SELECT empno, ename, job 
FROM emp 
WHERE deptno = 20; 
7369 Smith Clerk 
7566 Jones Manager current row 
7788 SCOTT Analyst 
7876 Adams Clerk 
7906 FORD Analyst 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Cursors 
DECLARE 
CURSOR c_emp IS 
SELECT empno, ename, job 
FROM emp 
WHERE deptno = 20; 
BEGIN 
FOR v_c IN c_emp LOOP 
… 
DBMS_OUTPUT.PUT_LINE(v_c.ename); 
… 
END LOOP; 
… 
end; 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Example 
• Given a table with first 
three columns are 
co.ukposite keys. Write 
a PL/SQL program using 
cursor to update 
cat_template1 with 
LONG column 
cat_template. Assuming 
this tables contains 
more than 40,000 
records. 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Example’s solution 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Dynamic SQL 
begin 
execute immediate 'create table tt(id 
number(3))'; 
end; 
/ 
info@quontrasolutions.co.uk
www.quontrasolutions.co.uk info@quontrasolutions.co.uk 
Visit: http://www.quontrasolutions.co.uk/ 
Email: info@quontrasolutions.co.uk 
Call Now : (20)-3734-1498. (UK) 
info@quontrasolutions.co.uk 
Contd…

Mais conteúdo relacionado

Mais de Quontra Solutions

Automation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsAutomation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsQuontra Solutions
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsQuontra Solutions
 
DataMining and OLAP Technology Concepts Presented By Quontra Solutions
DataMining and OLAP Technology Concepts Presented By Quontra SolutionsDataMining and OLAP Technology Concepts Presented By Quontra Solutions
DataMining and OLAP Technology Concepts Presented By Quontra SolutionsQuontra Solutions
 
Network security by quontra solutions uk
Network security by quontra solutions ukNetwork security by quontra solutions uk
Network security by quontra solutions ukQuontra Solutions
 
Introduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsIntroduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsQuontra Solutions
 
Informatica Metadata Exchange Frequently Asked Questions by Quontra Solutions
Informatica Metadata Exchange Frequently Asked Questions by Quontra SolutionsInformatica Metadata Exchange Frequently Asked Questions by Quontra Solutions
Informatica Metadata Exchange Frequently Asked Questions by Quontra SolutionsQuontra Solutions
 
Informatica metadata exchange frequently asked questions by quontra solutions
Informatica metadata exchange frequently asked questions by quontra solutionsInformatica metadata exchange frequently asked questions by quontra solutions
Informatica metadata exchange frequently asked questions by quontra solutionsQuontra Solutions
 
Dataware house Introduction By Quontra Solutions
Dataware house Introduction By Quontra SolutionsDataware house Introduction By Quontra Solutions
Dataware house Introduction By Quontra SolutionsQuontra Solutions
 
Selenium overview ppt by quontra solutions
Selenium overview ppt by quontra solutionsSelenium overview ppt by quontra solutions
Selenium overview ppt by quontra solutionsQuontra Solutions
 

Mais de Quontra Solutions (10)

Enterprise java beans
Enterprise java beansEnterprise java beans
Enterprise java beans
 
Automation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra SolutionsAutomation with Selenium Presented by Quontra Solutions
Automation with Selenium Presented by Quontra Solutions
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 
DataMining and OLAP Technology Concepts Presented By Quontra Solutions
DataMining and OLAP Technology Concepts Presented By Quontra SolutionsDataMining and OLAP Technology Concepts Presented By Quontra Solutions
DataMining and OLAP Technology Concepts Presented By Quontra Solutions
 
Network security by quontra solutions uk
Network security by quontra solutions ukNetwork security by quontra solutions uk
Network security by quontra solutions uk
 
Introduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsIntroduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutions
 
Informatica Metadata Exchange Frequently Asked Questions by Quontra Solutions
Informatica Metadata Exchange Frequently Asked Questions by Quontra SolutionsInformatica Metadata Exchange Frequently Asked Questions by Quontra Solutions
Informatica Metadata Exchange Frequently Asked Questions by Quontra Solutions
 
Informatica metadata exchange frequently asked questions by quontra solutions
Informatica metadata exchange frequently asked questions by quontra solutionsInformatica metadata exchange frequently asked questions by quontra solutions
Informatica metadata exchange frequently asked questions by quontra solutions
 
Dataware house Introduction By Quontra Solutions
Dataware house Introduction By Quontra SolutionsDataware house Introduction By Quontra Solutions
Dataware house Introduction By Quontra Solutions
 
Selenium overview ppt by quontra solutions
Selenium overview ppt by quontra solutionsSelenium overview ppt by quontra solutions
Selenium overview ppt by quontra solutions
 

Último

IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff17thcssbs2
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17Celine George
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Celine George
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonMayur Khatri
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptxmanishaJyala2
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Mark Carrigan
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfaedhbteg
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Discover the Dark Web .pdf InfosecTrain
Discover the Dark Web .pdf  InfosecTrainDiscover the Dark Web .pdf  InfosecTrain
Discover the Dark Web .pdf InfosecTraininfosec train
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 

Último (20)

“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17How to Analyse Profit of a Sales Order in Odoo 17
How to Analyse Profit of a Sales Order in Odoo 17
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdf
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
Discover the Dark Web .pdf InfosecTrain
Discover the Dark Web .pdf  InfosecTrainDiscover the Dark Web .pdf  InfosecTrain
Discover the Dark Web .pdf InfosecTrain
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 

Introduction to pl sql online training classes part 4