SlideShare a Scribd company logo
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Assigning SQL Query Results to PL/SQL Variables
You cannot write SQL select statements directly in your PL/SQL block. For example:
BEGIN
SELECT emp_name, emp_salary FROM employee WHERE emp_id=10;
END;
When you run this code this will throw an error.
So to get the result of SQL select statements you have to use INTO clause.
Note: INTO clause is applicable only for single row SELECT statements
So to get assign SQL query results to PL/SQL variables we can use like:
DECLARE
vName varchar2(50);
vSalary number;
BEGIN
SELECT emp_name, emp_salary INTO vName,vSalary
FROM employee WHERE emp_id=10;
dbms_output.put_line(‘Name: ‘||vName||’ and Salary: ‘||vSalary);
END;
This will assign the name of employee to variable vName and salary of employee to variable vSalary
whose employee id is 10. The output will be:
Name: Gurpreet Singh and Salary: 100000
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Now this INTO clause is applicable only for single rows, So if you need to access the multiple rows then?
Then you can use the CURSORS
CURSORS
Oracle creates a memory area, known as context area, for processing an SQL statement, which contains
all information needed for processing the statement, for example, number of rows processed, etc. A
cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds
the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is referred to as
the active set. There are two types of cursors:
 Implicit Cursors
 Explicit Cursors
Implicit Cursors
Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there
is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the
information in it.
When you write DML statements (INSERT, UPDATE, DELETE) then implicit cursor gets associated with it.
For example, when we write an INSERT statement then cursor holds the data to be inserted and when we
write DELETE or UPDATE statement then cursor holds the data that will be affected by those statements.
There are various attributes of implicit cursors:
Attribute Description
SQL%FOUND It tells whether any row has been affected with the statement. If one or
more is affected then it returns TRUE otherwise FALSE. For example, for an
update statement, if any row has been affected then it will return TRUE.
SQL%NOTFOUND It is opposite of SQL%FOUND. It returns TRUE if no row has been affected
SQL%ROWCOUNT It tells how many rows have been affected
For example:
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
BEGIN
UPDATE employee SET emp_salary=1000 WHERE dept_id=20;
IF SQL%FOUND THEN
dbms_output.put_line(‘Salary of ‘||SQL%ROWCOUNT||’ employees is updated’);
ELSE
dbms_output.put_line(‘Salary not updated of any employee’);
END IF;
END;
Output:
Salary of 4 employees is updated
Explicit Cursors
Explicit cursors are defined by programmer. Explicit cursors are used for those select statements which
return more than one rows.
Working with Explicit Cursors:
 Declare the cursor for initializing in the memory
 Opening the cursor for allocating memory
 Fetching the cursor for retrieving the data
 Closing the cursor to release the allocated memory
For example:
DECLARE OPEN FETCH CLOSE
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
DECALRE
CURSOR c1 IS
SELECT emp_id, emp_name FROM employee WHERE dept_id=10;
vId number;
vName varchar2(50);
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO vId, vName;
EXIT WHEN c1%NOTFOUND;
dbms_output.put_line(‘ID: ‘||vId||’ Name: ‘||vName);
END LOOP;
CLOSE c1;
END;
It will display the ID and Name of those employees whose dept_id is 10
We can skip the OPEN, FETCH and CLOSE by using the for loop
DECALRE
CURSOR c1 IS
SELECT emp_id, emp_name FROM employee WHERE dept_id=10;
BEGIN
OPEN c1;
FOR c11 IN c1 LOOP
dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name);
END LOOP;
END;
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
When we use for loop we can access the columns using the loop variable like we have used:
c11.emp_id and c11.emp_name
PASSING VALUES TO CURSORS
We can also pass the values to cursors. For example: in above example we can pass the dept_id rather
than just hard coding it to dept _id=10
DECALRE
CURSOR c1( id number) IS
SELECT emp_id, emp_name FROM employee WHERE dept_id=10;
BEGIN
OPEN c1;
FOR c11 IN c1(10) LOOP --Passing dept_id=10
dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name);
END LOOP;
END;
DECALRE
CURSOR c1( id number) IS
SELECT emp_id, emp_name FROM employee WHERE dept_id=10;
BEGIN
OPEN c1;
FOR c11 IN c1(20) LOOP --Passing dept_id=20
dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name);
END LOOP;
END;
P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h
Visit: http://gsbprogramming.blogspot.in
Similarly, we can pass other values.
Program to print the sum of salaries of the employees whose salary is greater than 1000
DECALRE
CURSOR c1 IS
SELECT emp_salary FROM employee ORDER BY salary DESC;
vSum number :=0;
BEGIN
OPEN c1;
FOR c11 IN c1 LOOP
IF c11.salary<=1000 THEN
EXIT;
vSum:= vSum+c11.emp_salary;
END LOOP;
dbms_output.put_line(‘Total Sum: ‘||vSum);
END;

More Related Content

Viewers also liked

Sistemas operativos prof.julia
Sistemas operativos prof.juliaSistemas operativos prof.julia
Sistemas operativos prof.julia
aerma5
 
Emprender
EmprenderEmprender
Emprenderbetete
 
TEFFICK RAGBEER Resume 01
TEFFICK RAGBEER Resume 01TEFFICK RAGBEER Resume 01
TEFFICK RAGBEER Resume 01Teffick Ragbeer
 
Analise de conteudo
Analise de conteudoAnalise de conteudo
Analise de conteudoLucila Pesce
 
Caminho MaríTimo Para O Brasil
Caminho MaríTimo Para O BrasilCaminho MaríTimo Para O Brasil
Caminho MaríTimo Para O Brasilcrie_historia8
 
Hallow'een 2012 | Teacher Isabel Ferreira
Hallow'een 2012 | Teacher Isabel FerreiraHallow'een 2012 | Teacher Isabel Ferreira
Hallow'een 2012 | Teacher Isabel FerreiraAEC-Inglês
 
Easter Egg Hunt - E.B. da Estalagem
Easter Egg Hunt - E.B. da EstalagemEaster Egg Hunt - E.B. da Estalagem
Easter Egg Hunt - E.B. da EstalagemAEC-Inglês
 

Viewers also liked (10)

Sistemas operativos prof.julia
Sistemas operativos prof.juliaSistemas operativos prof.julia
Sistemas operativos prof.julia
 
Fluzz pilulas 9
Fluzz pilulas 9Fluzz pilulas 9
Fluzz pilulas 9
 
Emprender
EmprenderEmprender
Emprender
 
La niñez
La niñezLa niñez
La niñez
 
TEFFICK RAGBEER Resume 01
TEFFICK RAGBEER Resume 01TEFFICK RAGBEER Resume 01
TEFFICK RAGBEER Resume 01
 
Analise de conteudo
Analise de conteudoAnalise de conteudo
Analise de conteudo
 
Caminho MaríTimo Para O Brasil
Caminho MaríTimo Para O BrasilCaminho MaríTimo Para O Brasil
Caminho MaríTimo Para O Brasil
 
Hallow'een 2012 | Teacher Isabel Ferreira
Hallow'een 2012 | Teacher Isabel FerreiraHallow'een 2012 | Teacher Isabel Ferreira
Hallow'een 2012 | Teacher Isabel Ferreira
 
Easter Egg Hunt - E.B. da Estalagem
Easter Egg Hunt - E.B. da EstalagemEaster Egg Hunt - E.B. da Estalagem
Easter Egg Hunt - E.B. da Estalagem
 
Slide historia
Slide historiaSlide historia
Slide historia
 

More from Gurpreet singh

Creating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP ReportsCreating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP Reports
Gurpreet singh
 
Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
Gurpreet singh
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
Gurpreet singh
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Gurpreet singh
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
Gurpreet singh
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
Gurpreet singh
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
Gurpreet singh
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
Gurpreet singh
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
Gurpreet singh
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
Gurpreet singh
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
Gurpreet singh
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
Gurpreet singh
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
Gurpreet singh
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
Gurpreet singh
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
Gurpreet singh
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
Gurpreet singh
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
Gurpreet singh
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
Gurpreet singh
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
Gurpreet singh
 

More from Gurpreet singh (20)

Creating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP ReportsCreating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP Reports
 
Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
 

Recently uploaded

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 

Recently uploaded (20)

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 

PL/SQL Part 6

  • 1. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in
  • 2. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in Assigning SQL Query Results to PL/SQL Variables You cannot write SQL select statements directly in your PL/SQL block. For example: BEGIN SELECT emp_name, emp_salary FROM employee WHERE emp_id=10; END; When you run this code this will throw an error. So to get the result of SQL select statements you have to use INTO clause. Note: INTO clause is applicable only for single row SELECT statements So to get assign SQL query results to PL/SQL variables we can use like: DECLARE vName varchar2(50); vSalary number; BEGIN SELECT emp_name, emp_salary INTO vName,vSalary FROM employee WHERE emp_id=10; dbms_output.put_line(‘Name: ‘||vName||’ and Salary: ‘||vSalary); END; This will assign the name of employee to variable vName and salary of employee to variable vSalary whose employee id is 10. The output will be: Name: Gurpreet Singh and Salary: 100000
  • 3. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in Now this INTO clause is applicable only for single rows, So if you need to access the multiple rows then? Then you can use the CURSORS CURSORS Oracle creates a memory area, known as context area, for processing an SQL statement, which contains all information needed for processing the statement, for example, number of rows processed, etc. A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is referred to as the active set. There are two types of cursors:  Implicit Cursors  Explicit Cursors Implicit Cursors Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the information in it. When you write DML statements (INSERT, UPDATE, DELETE) then implicit cursor gets associated with it. For example, when we write an INSERT statement then cursor holds the data to be inserted and when we write DELETE or UPDATE statement then cursor holds the data that will be affected by those statements. There are various attributes of implicit cursors: Attribute Description SQL%FOUND It tells whether any row has been affected with the statement. If one or more is affected then it returns TRUE otherwise FALSE. For example, for an update statement, if any row has been affected then it will return TRUE. SQL%NOTFOUND It is opposite of SQL%FOUND. It returns TRUE if no row has been affected SQL%ROWCOUNT It tells how many rows have been affected For example:
  • 4. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in BEGIN UPDATE employee SET emp_salary=1000 WHERE dept_id=20; IF SQL%FOUND THEN dbms_output.put_line(‘Salary of ‘||SQL%ROWCOUNT||’ employees is updated’); ELSE dbms_output.put_line(‘Salary not updated of any employee’); END IF; END; Output: Salary of 4 employees is updated Explicit Cursors Explicit cursors are defined by programmer. Explicit cursors are used for those select statements which return more than one rows. Working with Explicit Cursors:  Declare the cursor for initializing in the memory  Opening the cursor for allocating memory  Fetching the cursor for retrieving the data  Closing the cursor to release the allocated memory For example: DECLARE OPEN FETCH CLOSE
  • 5. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in DECALRE CURSOR c1 IS SELECT emp_id, emp_name FROM employee WHERE dept_id=10; vId number; vName varchar2(50); BEGIN OPEN c1; LOOP FETCH c1 INTO vId, vName; EXIT WHEN c1%NOTFOUND; dbms_output.put_line(‘ID: ‘||vId||’ Name: ‘||vName); END LOOP; CLOSE c1; END; It will display the ID and Name of those employees whose dept_id is 10 We can skip the OPEN, FETCH and CLOSE by using the for loop DECALRE CURSOR c1 IS SELECT emp_id, emp_name FROM employee WHERE dept_id=10; BEGIN OPEN c1; FOR c11 IN c1 LOOP dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name); END LOOP; END;
  • 6. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in When we use for loop we can access the columns using the loop variable like we have used: c11.emp_id and c11.emp_name PASSING VALUES TO CURSORS We can also pass the values to cursors. For example: in above example we can pass the dept_id rather than just hard coding it to dept _id=10 DECALRE CURSOR c1( id number) IS SELECT emp_id, emp_name FROM employee WHERE dept_id=10; BEGIN OPEN c1; FOR c11 IN c1(10) LOOP --Passing dept_id=10 dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name); END LOOP; END; DECALRE CURSOR c1( id number) IS SELECT emp_id, emp_name FROM employee WHERE dept_id=10; BEGIN OPEN c1; FOR c11 IN c1(20) LOOP --Passing dept_id=20 dbms_output.put_line(‘ID: ‘||c11.emp_id||’ Name: ‘||c11.emp_name); END LOOP; END;
  • 7. P L / S Q L T u t o r i a l - B y E r G u r p r e e t S i n g h Visit: http://gsbprogramming.blogspot.in Similarly, we can pass other values. Program to print the sum of salaries of the employees whose salary is greater than 1000 DECALRE CURSOR c1 IS SELECT emp_salary FROM employee ORDER BY salary DESC; vSum number :=0; BEGIN OPEN c1; FOR c11 IN c1 LOOP IF c11.salary<=1000 THEN EXIT; vSum:= vSum+c11.emp_salary; END LOOP; dbms_output.put_line(‘Total Sum: ‘||vSum); END;