SlideShare a Scribd company logo
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
18
Interacting with
the Oracle Server
18-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Write a successful SELECT statement in
PL/SQL
• Declare the datatype and size of a
PL/SQL variable dynamically
• Write DML statements in PL/SQL
• Control transactions in PL/SQL
• Determine the outcome of SQL DML
statements
18-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SQL Statements in PL/SQL
• Extract a row of data from the database
by using the SELECT command. Only a
single set of values can be returned.
• Make changes to rows in the database
by using DML commands.
• Control a transaction with the COMMIT,
ROLLBACK, or SAVEPOINT command.
• Determine DML outcome with implicit
cursors.
18-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SELECT Statements in PL/SQL
Retrieve data from the database with
SELECT.
Syntax
SELECT select_list
INTO {variable_name[, variable_name]...
| record_name}
FROM table
WHERE condition;
18-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SELECT Statements in PL/SQL
The INTO clause is required.
Example
DECLARE
v_deptno NUMBER(2);
v_loc VARCHAR2(15);
BEGIN
SELECT deptno, loc
INTO v_deptno, v_loc
FROM dept
WHERE dname = 'SALES';
...
END;
18-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Retrieving Data in PL/SQL
Retrieve the order date and the ship date
for the specified order.
Example
DECLARE
v_orderdate ord.orderdate%TYPE;
v_shipdate ord.shipdate%TYPE;
BEGIN
SELECT orderdate, shipdate
INTO v_orderdate, v_shipdate
FROM ord
WHERE id = 620;
...
END;
18-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Retrieving Data in PL/SQL
Return the sum of the salaries for all
employees in the specified department.
Example
DECLARE
v_sum_sal emp.sal%TYPE;
v_deptno NUMBER NOT NULL := 10;
BEGIN
SELECT SUM(sal) -- group function
INTO v_sum_sal
FROM emp
WHERE deptno = v_deptno;
END;
18-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
INSERT
UPDATE
DELETE
Manipulating Data Using PL/SQL
Make changes to database tables by
using DML commands:
• INSERT
• UPDATE
• DELETE
18-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inserting Data
Add new employee information to the
EMP table.
Example
BEGIN
INSERT INTO emp(empno, ename, job, deptno)
VALUES (empno_sequence.NEXTVAL, 'HARDING',
'CLERK', 10);
END;
18-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Updating Data
Increase the salary of all employees in the
EMP table who are Analysts.
Example
DECLARE
v_sal_increase emp.sal%TYPE := 2000;
BEGIN
UPDATE emp
SET sal = sal + v_sal_increase
WHERE job = 'ANALYST';
END;
18-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Deleting Data
Delete rows that belong to department 10
from the EMP table.
Example
DECLARE
v_deptno emp.deptno%TYPE := 10;
BEGIN
DELETE FROM emp
WHERE deptno = v_deptno;
END;
18-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Naming Conventions
• Use a naming convention to avoid
ambiguity in the WHERE clause.
• Database columns and identifiers
should have distinct names.
• Syntax errors can arise because PL/SQL
checks the database first for a column
in the table.
18-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Naming Conventions
DECLARE
orderdate ord.orderdate%TYPE;
shipdate ord.shipdate%TYPE;
ordid ord.ordid%TYPE := 601;
BEGIN
SELECT orderdate, shipdate
INTO orderdate, shipdate
FROM ord
WHERE ordid = ordid;
END;
SQL> /
DECLARE
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested
number of rows
ORA-06512: at line 6
18-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
COMMIT and ROLLBACK
Statements
• Initiate a transaction with the first DML
command to follow a COMMIT or
ROLLBACK.
• Use COMMIT and ROLLBACK SQL
statements to terminate a transaction
explicitly.
18-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SQL Cursor
• A cursor is a private SQL work area.
• There are two types of cursors:
– Implicit cursors
– Explicit cursors
• The Oracle Server uses implicit cursors
to parse and execute your SQL
statements.
• Explicit cursors are explicitly declared
by the programmer.
18-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SQL Cursor Attributes
Using SQL cursor attributes, you can test
the outcome of your SQL statements.
SQL%ROWCOUNT Number of rows affected by the
most recent SQL statement (an
integer value)
SQL%FOUND Boolean attribute that evaluates to
TRUE if the most recent SQL
statement affects one or more rows
SQL%NOTFOUND Boolean attribute that evaluates to
TRUE if the most recent SQL
statement does not affect any rows
SQL%ISOPEN Always evaluates to FALSE because
PL/SQL closes implicit cursors
immediately after they are executed
18-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
SQL Cursor Attributes
Delete rows that have the specified order
number from the ITEM table. Print the
number of rows deleted.
Example
VARIABLE rows_deleted VARCHAR2(30)
DECLARE
v_ordid NUMBER := 605;
BEGIN
DELETE FROM item
WHERE ordid = v_ordid;
:rows_deleted := (SQL%ROWCOUNT ||
' rows deleted.');
END;
/
PRINT rows_deleted
18-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• Embed SQL in the PL/SQL block:
SELECT, INSERT, UPDATE, DELETE
• Embed transaction control statements
in a PL/SQL block:
COMMIT, ROLLBACK, SAVEPOINT
18-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• There are two cursor types: implicit and
explicit.
• Implicit cursor attributes verify the
outcome of DML statements:
– SQL%ROWCOUNT
– SQL%FOUND
– SQL%NOTFOUND
– SQL%ISOPEN
• Explicit cursors are defined by the
programmer.
18-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Practice Overview
• Creating a PL/SQL block to select data
from a table
• Creating a PL/SQL block to insert data
into a table
• Creating a PL/SQL block to update data
in a table
• Creating a PL/SQL block to delete a
record from a table

More Related Content

What's hot

Rdbms (2)
Rdbms (2)Rdbms (2)
Rdbms (2)
Prakash .
 
Fundamentals of Database system
Fundamentals of Database systemFundamentals of Database system
Fundamentals of Database system
philipsinter
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
Deepam Aggarwal
 
Mobile dbms
Mobile dbmsMobile dbms
Mobile dbmsTech_MX
 
Animation in Computer Graphics
Animation in Computer GraphicsAnimation in Computer Graphics
Animation in Computer Graphics
RinkuNahar
 
Er & eer to relational mapping
Er & eer to relational mappingEr & eer to relational mapping
Er & eer to relational mappingsaurabhshertukde
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
Parthipan Parthi
 
Raster scan systems with video controller and display processor
Raster scan systems with video controller and display processorRaster scan systems with video controller and display processor
Raster scan systems with video controller and display processor
hemanth kumar
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
Database design (entity, entity set and entity type) unit 2 part 3
Database design (entity, entity set and entity type)  unit 2 part 3Database design (entity, entity set and entity type)  unit 2 part 3
Database design (entity, entity set and entity type) unit 2 part 3
Ram Paliwal
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
sinhacp
 
Html
HtmlHtml
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.Mohd Arif
 
Illumination Models & Shading
Illumination Models & ShadingIllumination Models & Shading
Chapter 2 database environment
Chapter 2 database environmentChapter 2 database environment
Chapter 2 database environment>. <
 
Computer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and GradientComputer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and Gradient
Ahmed Gad
 
Dbms 7: ER Diagram Design Issue
Dbms 7: ER Diagram Design IssueDbms 7: ER Diagram Design Issue
Dbms 7: ER Diagram Design Issue
Amiya9439793168
 
03 digital image fundamentals DIP
03 digital image fundamentals DIP03 digital image fundamentals DIP
03 digital image fundamentals DIP
babak danyal
 
Types of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal GulatiTypes of keys in database management system by Dr. Kamal Gulati

What's hot (20)

Rdbms (2)
Rdbms (2)Rdbms (2)
Rdbms (2)
 
Fundamentals of Database system
Fundamentals of Database systemFundamentals of Database system
Fundamentals of Database system
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause Group By, Having Clause and Order By clause
Group By, Having Clause and Order By clause
 
Mobile dbms
Mobile dbmsMobile dbms
Mobile dbms
 
Animation in Computer Graphics
Animation in Computer GraphicsAnimation in Computer Graphics
Animation in Computer Graphics
 
Er & eer to relational mapping
Er & eer to relational mappingEr & eer to relational mapping
Er & eer to relational mapping
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
 
Raster scan systems with video controller and display processor
Raster scan systems with video controller and display processorRaster scan systems with video controller and display processor
Raster scan systems with video controller and display processor
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Database design (entity, entity set and entity type) unit 2 part 3
Database design (entity, entity set and entity type)  unit 2 part 3Database design (entity, entity set and entity type)  unit 2 part 3
Database design (entity, entity set and entity type) unit 2 part 3
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Html
HtmlHtml
Html
 
Ellipses drawing algo.
Ellipses drawing algo.Ellipses drawing algo.
Ellipses drawing algo.
 
Illumination Models & Shading
Illumination Models & ShadingIllumination Models & Shading
Illumination Models & Shading
 
Chapter 2 database environment
Chapter 2 database environmentChapter 2 database environment
Chapter 2 database environment
 
Computer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and GradientComputer Vision: Correlation, Convolution, and Gradient
Computer Vision: Correlation, Convolution, and Gradient
 
Dbms 7: ER Diagram Design Issue
Dbms 7: ER Diagram Design IssueDbms 7: ER Diagram Design Issue
Dbms 7: ER Diagram Design Issue
 
03 digital image fundamentals DIP
03 digital image fundamentals DIP03 digital image fundamentals DIP
03 digital image fundamentals DIP
 
Types of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal GulatiTypes of keys in database management system by Dr. Kamal Gulati
Types of keys in database management system by Dr. Kamal Gulati
 

Similar to Les18[1]Interacting with the Oracle Server

Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
siavosh kaviani
 
Les09.ppt
Les09.pptLes09.ppt
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
siavosh kaviani
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
rehaniltifat
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
siavosh kaviani
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
siavosh kaviani
 
Sql intro
Sql introSql intro
Sql introglubox
 
Les01.ppt
Les01.pptLes01.ppt
Les01.ppt
sanasaeed84
 
Les01.pptx
Les01.pptxLes01.pptx
Les01.pptx
NishaTariq1
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
122 sql for-beginners
122 sql for-beginners122 sql for-beginners
122 sql for-beginners
suzzanj1990
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
siavosh kaviani
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
Hitesh Mohapatra
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
Vaibhav Kathuria
 
Lab
LabLab
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 

Similar to Les18[1]Interacting with the Oracle Server (20)

Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
Les09.ppt
Les09.pptLes09.ppt
Les09.ppt
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
02 Writing Executable Statments
02 Writing Executable Statments02 Writing Executable Statments
02 Writing Executable Statments
 
PL/SQL 3 DML
PL/SQL 3 DMLPL/SQL 3 DML
PL/SQL 3 DML
 
Les20[1]Working with Composite Datatypes
Les20[1]Working with Composite DatatypesLes20[1]Working with Composite Datatypes
Les20[1]Working with Composite Datatypes
 
Les09
Les09Les09
Les09
 
Les08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*PlusLes08[1] Producing Readable Output with SQL*Plus
Les08[1] Producing Readable Output with SQL*Plus
 
Sql intro
Sql introSql intro
Sql intro
 
Les01.ppt
Les01.pptLes01.ppt
Les01.ppt
 
Les01.pptx
Les01.pptxLes01.pptx
Les01.pptx
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
122 sql for-beginners
122 sql for-beginners122 sql for-beginners
122 sql for-beginners
 
Les10[1]Creating and Managing Tables
Les10[1]Creating and Managing TablesLes10[1]Creating and Managing Tables
Les10[1]Creating and Managing Tables
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Lab
LabLab
Lab
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 

More from siavosh kaviani

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
siavosh kaviani
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
siavosh kaviani
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
siavosh kaviani
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
siavosh kaviani
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
siavosh kaviani
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
siavosh kaviani
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
siavosh kaviani
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
siavosh kaviani
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
siavosh kaviani
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
siavosh kaviani
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
siavosh kaviani
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
siavosh kaviani
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptions
siavosh kaviani
 
Les22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor ConceptsLes22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor Concepts
siavosh kaviani
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursors
siavosh kaviani
 
Les19[1]Writing Control Structures
Les19[1]Writing Control StructuresLes19[1]Writing Control Structures
Les19[1]Writing Control Structures
siavosh kaviani
 
Les17[1] Writing Executable Statements
Les17[1] Writing Executable StatementsLes17[1] Writing Executable Statements
Les17[1] Writing Executable Statements
siavosh kaviani
 
Les16[1]Declaring Variables
Les16[1]Declaring VariablesLes16[1]Declaring Variables
Les16[1]Declaring Variables
siavosh kaviani
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
siavosh kaviani
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
siavosh kaviani
 

More from siavosh kaviani (20)

sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Applicationsara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
 
Introduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdfIntroduction-to-the-Lean-Canvas.pdf
Introduction-to-the-Lean-Canvas.pdf
 
Attaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptxAttaque chimique contre les écolières en Iran version 2.pptx
Attaque chimique contre les écolières en Iran version 2.pptx
 
Short CV BA.pdf
Short CV BA.pdfShort CV BA.pdf
Short CV BA.pdf
 
Faegh Omidi Resume.pdf
Faegh Omidi Resume.pdfFaegh Omidi Resume.pdf
Faegh Omidi Resume.pdf
 
Short CV CTO version 2.pdf
Short CV CTO version 2.pdfShort CV CTO version 2.pdf
Short CV CTO version 2.pdf
 
Short CV Marketing version 2.pdf
Short CV Marketing version 2.pdfShort CV Marketing version 2.pdf
Short CV Marketing version 2.pdf
 
Short CV prof version 2.pdf
Short CV prof version 2.pdfShort CV prof version 2.pdf
Short CV prof version 2.pdf
 
Siavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdfSiavosh Kaviani cv francais 2022 version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
 
SiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdfSiavoshKaviani-CV[2021] francais.pdf
SiavoshKaviani-CV[2021] francais.pdf
 
apex security demo.ppsx
apex security demo.ppsxapex security demo.ppsx
apex security demo.ppsx
 
Les14[1]Controlling User Access
Les14[1]Controlling User AccessLes14[1]Controlling User Access
Les14[1]Controlling User Access
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptions
 
Les22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor ConceptsLes22[1]Advanced Explicit Cursor Concepts
Les22[1]Advanced Explicit Cursor Concepts
 
Les21[1]Writing Explicit Cursors
Les21[1]Writing Explicit CursorsLes21[1]Writing Explicit Cursors
Les21[1]Writing Explicit Cursors
 
Les19[1]Writing Control Structures
Les19[1]Writing Control StructuresLes19[1]Writing Control Structures
Les19[1]Writing Control Structures
 
Les17[1] Writing Executable Statements
Les17[1] Writing Executable StatementsLes17[1] Writing Executable Statements
Les17[1] Writing Executable Statements
 
Les16[1]Declaring Variables
Les16[1]Declaring VariablesLes16[1]Declaring Variables
Les16[1]Declaring Variables
 
Les15[1]SQL Workshop
Les15[1]SQL WorkshopLes15[1]SQL Workshop
Les15[1]SQL Workshop
 
Les13[1]Other Database Objects
Les13[1]Other Database ObjectsLes13[1]Other Database Objects
Les13[1]Other Database Objects
 

Recently uploaded

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 

Recently uploaded (20)

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 

Les18[1]Interacting with the Oracle Server

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 18 Interacting with the Oracle Server
  • 2. 18-2 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Write a successful SELECT statement in PL/SQL • Declare the datatype and size of a PL/SQL variable dynamically • Write DML statements in PL/SQL • Control transactions in PL/SQL • Determine the outcome of SQL DML statements
  • 3. 18-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SQL Statements in PL/SQL • Extract a row of data from the database by using the SELECT command. Only a single set of values can be returned. • Make changes to rows in the database by using DML commands. • Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT command. • Determine DML outcome with implicit cursors.
  • 4. 18-4 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SELECT Statements in PL/SQL Retrieve data from the database with SELECT. Syntax SELECT select_list INTO {variable_name[, variable_name]... | record_name} FROM table WHERE condition;
  • 5. 18-5 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SELECT Statements in PL/SQL The INTO clause is required. Example DECLARE v_deptno NUMBER(2); v_loc VARCHAR2(15); BEGIN SELECT deptno, loc INTO v_deptno, v_loc FROM dept WHERE dname = 'SALES'; ... END;
  • 6. 18-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Retrieving Data in PL/SQL Retrieve the order date and the ship date for the specified order. Example DECLARE v_orderdate ord.orderdate%TYPE; v_shipdate ord.shipdate%TYPE; BEGIN SELECT orderdate, shipdate INTO v_orderdate, v_shipdate FROM ord WHERE id = 620; ... END;
  • 7. 18-7 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Retrieving Data in PL/SQL Return the sum of the salaries for all employees in the specified department. Example DECLARE v_sum_sal emp.sal%TYPE; v_deptno NUMBER NOT NULL := 10; BEGIN SELECT SUM(sal) -- group function INTO v_sum_sal FROM emp WHERE deptno = v_deptno; END;
  • 8. 18-8 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. INSERT UPDATE DELETE Manipulating Data Using PL/SQL Make changes to database tables by using DML commands: • INSERT • UPDATE • DELETE
  • 9. 18-9 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inserting Data Add new employee information to the EMP table. Example BEGIN INSERT INTO emp(empno, ename, job, deptno) VALUES (empno_sequence.NEXTVAL, 'HARDING', 'CLERK', 10); END;
  • 10. 18-10 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Updating Data Increase the salary of all employees in the EMP table who are Analysts. Example DECLARE v_sal_increase emp.sal%TYPE := 2000; BEGIN UPDATE emp SET sal = sal + v_sal_increase WHERE job = 'ANALYST'; END;
  • 11. 18-11 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Deleting Data Delete rows that belong to department 10 from the EMP table. Example DECLARE v_deptno emp.deptno%TYPE := 10; BEGIN DELETE FROM emp WHERE deptno = v_deptno; END;
  • 12. 18-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Naming Conventions • Use a naming convention to avoid ambiguity in the WHERE clause. • Database columns and identifiers should have distinct names. • Syntax errors can arise because PL/SQL checks the database first for a column in the table.
  • 13. 18-13 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Naming Conventions DECLARE orderdate ord.orderdate%TYPE; shipdate ord.shipdate%TYPE; ordid ord.ordid%TYPE := 601; BEGIN SELECT orderdate, shipdate INTO orderdate, shipdate FROM ord WHERE ordid = ordid; END; SQL> / DECLARE * ERROR at line 1: ORA-01422: exact fetch returns more than requested number of rows ORA-06512: at line 6
  • 14. 18-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. COMMIT and ROLLBACK Statements • Initiate a transaction with the first DML command to follow a COMMIT or ROLLBACK. • Use COMMIT and ROLLBACK SQL statements to terminate a transaction explicitly.
  • 15. 18-15 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SQL Cursor • A cursor is a private SQL work area. • There are two types of cursors: – Implicit cursors – Explicit cursors • The Oracle Server uses implicit cursors to parse and execute your SQL statements. • Explicit cursors are explicitly declared by the programmer.
  • 16. 18-16 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SQL Cursor Attributes Using SQL cursor attributes, you can test the outcome of your SQL statements. SQL%ROWCOUNT Number of rows affected by the most recent SQL statement (an integer value) SQL%FOUND Boolean attribute that evaluates to TRUE if the most recent SQL statement affects one or more rows SQL%NOTFOUND Boolean attribute that evaluates to TRUE if the most recent SQL statement does not affect any rows SQL%ISOPEN Always evaluates to FALSE because PL/SQL closes implicit cursors immediately after they are executed
  • 17. 18-17 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. SQL Cursor Attributes Delete rows that have the specified order number from the ITEM table. Print the number of rows deleted. Example VARIABLE rows_deleted VARCHAR2(30) DECLARE v_ordid NUMBER := 605; BEGIN DELETE FROM item WHERE ordid = v_ordid; :rows_deleted := (SQL%ROWCOUNT || ' rows deleted.'); END; / PRINT rows_deleted
  • 18. 18-18 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • Embed SQL in the PL/SQL block: SELECT, INSERT, UPDATE, DELETE • Embed transaction control statements in a PL/SQL block: COMMIT, ROLLBACK, SAVEPOINT
  • 19. 18-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • There are two cursor types: implicit and explicit. • Implicit cursor attributes verify the outcome of DML statements: – SQL%ROWCOUNT – SQL%FOUND – SQL%NOTFOUND – SQL%ISOPEN • Explicit cursors are defined by the programmer.
  • 20. 18-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Practice Overview • Creating a PL/SQL block to select data from a table • Creating a PL/SQL block to insert data into a table • Creating a PL/SQL block to update data in a table • Creating a PL/SQL block to delete a record from a table