SQL in PL - SQL
SQL in PL - SQL
SQL in PL/SQL
In this tutorial, we are going to learn how to use SQL (/sql.html)in PL/SQL. SQL (/sql.html)is the
actual component that takes care of fetching and updating of data in the database whereas
PL/SQL is the component that processes these data. Further in this article we will also discuss
how to combine the SQL (/sql.html)within the PL/SQL block.
Cursor Attributes
Autonomous Transaction
Data Insertion
Data Update
Data Deletion
Data Projection/Fetching
In PL/SQL, we can do the data manipulation only by using the SQL (/sql.html)commands.
Data Inser on
In PL/SQL, we can insert the data into any table using the SQL (/sql.html)command INSERT
INTO. This command will take the table name, table column and column values as the input
and insert the value in the base table.
The INSERT command can also take the values directly from another table using 'SELECT'
statement rather than giving the values for each column. Through 'SELECT' statement, we can
insert as many rows as the base table contains.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL1.png)
Syntax Explanation:
The above syntax shows the INSERT INTO command. The table name and values are
mandatory field, whereas column names are not mandatory if the insert statements have
values for all the column of the table.
The keyword 'VALUES' is mandatory if the values are given separately as shown above.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL2.png)
Syntax Explanation:
The above syntax shows the INSERT INTO command that takes the values directly from the
<table_name2> using the SELECT command.
The keyword 'VALUES' should not be present in this case as the values are not given
separately.
Data Update
Data update simply means an update of the value of any column in the table. This can be done
using 'UPDATE' statement. This statement takes the table name, column name and value as
the input and updates the data.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL3.png)
Syntax Explanation:
The above syntax shows the UPDATE. The keyword 'SET' instruct that PL/SQL engine to
update the value of the column with the value given.
'WHERE' clause is optional. If this clause is not given, then the value of the mentioned
column in the entire table will be updated.
Data Dele on
Data deletion means to delete one full record from the database table. The 'DELETE' command
is used for this purpose.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL4.png)
Syntax Explanation:
The above syntax shows the DELETE command. The keyword 'FROM' is optional and with
or without 'FROM' clause the command behaves in the same way.
'WHERE' clause is optional. If this clause is not given, then the entire table will be deleted.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL5.png)
Syntax Explanation:
The above syntax shows the SELECT-INTO command. The keyword 'FROM' is mandatory
that identifies the table name from which the data needs to be fetched.
'WHERE' clause is optional. If this clause is not given, then the data from the entire table will
be fetched.
Example 1: In this example, we are going to see how to perform DML operations in PL/SQL.
We are going to insert the below 4 records into emp table.
Then we are going to update the salary of 'XXX' to 15000, and we are going to delete the
employee record 'ZZZ'. Finally, we are going to project the details of the employee 'XXX'.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL6.png)
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL7.png)
Code Explanation:
PL/SQL allows the programmer to control the context area through the cursor. A cursor holds
the rows returned by the SQL (/sql.html)statement. The set of rows the cursor holds is referred
as active set. These cursors can also be named so that they can be referred from other place of
the code.
Implicit Cursor
Explicit Cursor
Implicit Cursor
Whenever any DML operations occur in the database, an implicit cursor is created that holds
the rows affected, in that particular operation. These cursors cannot be named and hence they
cannot be controlled or referred from other place of the code. We can refer only to the most
recent cursor through the cursor attributes.
Explicit Cursor
Programmers are allowed to create named context area to execute their DML operations to get
more control over it. The explicit cursor should be defined in the declaration section of the
PL/SQL block, and it is created for the 'SELECT' statement that needs to be used in the code.
Opening Cursor
Opening the cursor will instruct the PL/SQL to allocate the memory for this cursor. It will
make the cursor ready to fetch the records.
Fetching Data from the Cursor
In this process, the 'SELECT' statement is executed and the rows fetched is stored in the
allocated memory. These are now called as active sets. Fetching data from the cursor is a
record-level activity that means we can access the data in record-by-record way.
Each fetch statement will fetch one active set and holds the information of that particular
record. This statement is same as 'SELECT' statement that fetches the record and assign to
the variable in the 'INTO' clause, but it will not throw any exceptions.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL8.png)
Syntax Explanation:
In the above syntax, the declaration part contains the declaration of the cursor and the
cursor variable in which the fetched data will be assigned.
The cursor is created for the 'SELECT' statement that is given in the cursor declaration.
In execution part, the declared cursor is opened, fetched and closed.
Cursor A ributes
Both Implicit cursor and the explicit cursor has certain attributes that can be accessed. These
attributes give more information about the cursor operations. Below are the different cursor
attributes and their usage.
Cursor Description
Attribute
%FOUND It returns the Boolean result 'TRUE' if the most recent fetch operation
fetched a record successfully, else it will return FALSE
%NOTFOUND This works in the opposite way to %FOUND it will return 'TRUE' if the most
recent fetch operation could not able to fetch any record.
%ISOPEN It returns Boolean result 'TRUE' if the given cursor is already opened, else it
returns 'FALSE'
%ROWCOUNT It returns the numerical value. It gives the actual count of records that got
affected by the DML activity.
Example 1: In this example, we are going to see how to declare, open, fetch and close the
explicit cursor.
We will project all the employees name from emp table using a cursor. We will also use cursor
attribute to set the loop to fetch all the record from the cursor.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL9.png)
Code Explanation:
Code line 2: Declaring the cursor guru99_det for statement 'SELECT emp_name FROM
emp'.
Code line 3: Declaring variable lv_emp_name.
Code line 5: Opening the cursor guru99_det.
Code line 6: Setting the Basic loop statement to fetch all the records in the 'emp' table.
Code line 7: Fetches the guru99_det data and assign the value to lv_emp_name.
Code line 9: Using the cursor attribute '%NOTFOUND' to find whether all the record in the
cursor is fetched. If fetched then it will return 'TRUE' and control will exit from the loop, else
the control will keep on fetching the data from the cursor and print the data.
Code line 11: EXIT condition for the loop statement.
Code line 12: Print the fetched employee name.
Code line 14: Using the cursor attribute '%ROWCOUNT' to find the total number of records
that got affected/fetched in the cursor.
Code line 15: After exiting from the loop the cursor is closed and the memory allocated is
set free.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL10.png)
Syntax Explanation:
In the above syntax, the declaration part contains the declaration of the cursor.
The cursor is created for the 'SELECT' statement that is given in the cursor declaration.
In execution part, the declared cursor is setup in the FOR loop and the loop variable 'I' will
behave as cursor variable in this case.
Example 1: In this example, we will project all the employee name from emp table using a
cursor-FOR loop.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL11.png)
Code Explanation:
Code line 2: Declaring the cursor guru99_det for statement 'SELECT emp_name FROM
emp'.
Code line 4: Constructing the 'FOR' loop for the cursor with the loop variable lv_emp_name.
Code line 5: Printing the employee name in each iteration of the loop.
Code line 8: Exit the loop
Note: In Cursor-FOR loop, cursor attributes cannot be used since opening, fetching and closing
of the cursor is done implicitly by FOR loop.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL12.png)
Syntax Explanation:
In the above syntax, BULK COLLECT is used in collect the data from 'SELECT' and
'FETCH' statement.
FORALL Clause
The FORALL allows to perform the DML operations on data in bulk. It is similar to that of FOR
loop statement except in FOR loop things happen at the record-level whereas in FORALL there
is no LOOP concept. Instead the entire data present in the given range is processed at the
same time.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL13.png)
Syntax Explanation:
In the above syntax, the given DML operation will be executed for the entire data that is
present between lower and higher range.
LIMIT Clause
The bulk collect concept loads the entire data into the target collection variable as a bulk i.e. the
whole data will be populated into the collection variable in a single-go. But this is not advisable
when the total record that needs to be loaded is very large, because when PL/SQL tries to load
the entire data it consumes more session memory. Hence, it is always good to limit the size of
this bulk collect operation.
However, this size limit can be easily achieved by introducing the ROWNUM condition in the
'SELECT' statement, whereas in the case of cursor this is not possible.
To overcome this Oracle has provided 'LIMIT' clause that defines the number of records that
needs to be included in the bulk.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL14.png)
Syntax Explanation:
In the above syntax, the cursor fetch statement uses BULK COLLECT statement along with
the LIMIT clause.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL15.png)
Code Explanation:
Code line 2: Declaring the cursor guru99_det for statement 'SELECT emp_name FROM
emp'.
Code line 3: Declaring lv_emp_name_tbl as table type of VARCHAR2(50)
Code line 4: Declaring lv_emp_name as lv_emp_name_tbl type.
Code line 6: Opening the cursor.
Code line 7: Fetching the cursor using BULK COLLECT with the LIMIT size as 5000 intl
lv_emp_name variable.
Code line 8-11: Setting up FOR loop to print all the record in the collection lv_emp_name.
Code line 12: Using FORALL updating the salary of all the employee by 5000.
Code line 14: Committing the transaction.
SAVEPOINT Creates a point in the transaction till which rollback can be done later
ROLLBACK TO Discard all the pending transaction till the specified <save point>
Autonomous Transac on
In PL/SQL, all the modifications done on data will be termed as a transaction. A transaction is
considered as complete when the save/discard is applied to it. If no save/discard is given, then
the transaction will not be considered as complete and the modifications done on the data will
not be made permanent in the server.
Irrespective of a number of modifications done during a session, PL/SQL will treat the whole
modification as a single transaction and saving/discard this transaction affects to the entire
pending changes in that session. Autonomous Transaction provides a functionality to the
developer in which it allows to do changes in a separate transaction and to save/discard that
particular transaction without affecting the main session transaction.
Syntax Explanation:
In the above syntax, the block has been made as an autonomous transaction.
Example 1: In this example, we are going to understand how the autonomous transaction is
working.
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL17.png)
(//cdn.guru99.com/images/PL-SQL/110215_1042_SQLinPLSQL18.png)
Code Explanation:
Summary
In this tutorial, we have learnt how to combine the SQL (/sql.html)in PL/SQL for data
manipulation, Cursor Concepts, Cursor-FOR loop and Bulk collect usages. We have also
discussed the TCL statements and how to save/discard transactions separately.
Next (/packages-pl-sql.html)
(/pl-sql- (/loops-pl-
identifiers.html) sql.html)
PL/SQL Iden ers Loops in PL/SQL
(/pl-sql-identifiers.html) (/loops-pl-sql.html)
(/pl-sql-first-
Identifiers are nothing but a In this chapter, we are going
name that is given for a to see the loop concept in
program-helloworld.html) PL/SQL object. The object PL/SQL and flow of control in
could be... loops. You...
PL SQL First Program: Hello
World Read more Read more
(/pl-sql-first-program-
helloworld.html)
In this tutorial, we will
introduce SQL (/sql.html)plus
and learn how to connect it to
the database. After...
Read more
PL/SQL Tutorials
Introduction to PL/SQL (/introduction-pl-sql.html)
About
About us (/about-us.html)
Advertise with Us (/advertise-us.html)
Jobs (/freelancing.html)
Privacy Policy (/privacy-policy.html)
Contact Us
Contact us (/contact-us.html)
FAQ (/faq.html)
Write For Us (/become-an-instructor.html)
Follow Us
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://forms.aweber.com/form/46/724807646.htm)
(https://play.google.com/store/apps/details?
id=com.vector.guru99&hl=en)
Cer ca ons
ISTQB Certification (/istqb.html)
MySQL Certification (/mysql-certification-guide.html)
QTP Certification (/qtp-quality-center-certification-
details.html) Testing Certification (/software-testing-
certification.html)
CTAL Exam (/ctal-certified-exam-preparation.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
Interes ng
Books to Read! (/books.html)
Quiz (/tests.html)