Professional Documents
Culture Documents
Features of PL/SQL:
1. PL/SQL is basically a procedural language, which provides the
functionality of decision making, iteration and many more features of
procedural programming languages.
2. PL/SQL can execute a number of queries in one block using single
command.
3. One can create a PL/SQL unit such as procedures, functions, packages,
triggers, and types, which are stored in the database for reuse by
applications.
4. PL/SQL provides a feature to handle the exception which occurs in
PL/SQL block known as exception handling block.
5. Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
6. PL/SQL Offers extensive error checking.
Typically, each block performs a logical action in the program. A block has
the following structure:
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
Declare section starts with DECLARE keyword in which variables,
constants, records as cursors can be declared which stores data
temporarily. It basically consists definition of PL/SQL identifiers. This part
of the code is optional.
Execution section starts with BEGIN and ends with END keyword.This is
a mandatory section and here the program logic is written to perform any
task like loops and conditional statements. It supports
all DML commands, DDL commands and SQL*PLUS built-in functions as
well.
Exception section starts with EXCEPTION keyword.This section is
optional which contains statements that are executed when a run-time
error occurs. Any exceptions can be handled in this section.
Conditional Expressions:
Following are Conditional Expressions in SQL
COALESCE : Returns the first non-null argument. Null is returned only if all
arguments are null. It is often used to substitute a default value for null
values when data is retrieved for display.
NOTE: Same as CASE expressions, COALESCE also will not evaluate the
arguments to the right of the first non-null argument found.
Syntax:
COALESCE(value [, ......] )
GREATEST: Returns the largest value from a list of any number of
expressions. Comparison is case sensitive. If datatypes of all the
expressions in the list are not same, rest all expressions are converted to the
datatype of the first expression for comparison and if this conversion is not
possible, SQL will throw an error.
NOTE: Returns null if any expression in the list is null.
Syntax:
GREATEST(expr1, expr2 [, .....] )
IN: Checks whether a value is present within a set of values and can be used
with WHERE, CHECK and creation of views.
NOTE: Same as CASE and COALESCE expressions, IN also will not
evaluate the arguments to the right of the first non-null argument found.
Syntax:
WHERE column IN (x1, x2, x3 [,......] )
Input:
SELECT NULLIF(9995463931, contact_num)
from Employee;
Loop statement:
In SQL Server, a loop is the technique where a set of SQL
statements are executed repeatedly until a condition is met.
Syntax:
WHILE <condition>
SQL Statement | statement_block | BREAK | CONTINUE
BREAK: Causes the flow to exit from the innermost WHILE loop.
Statements after the END keyword are executed after the BREAK.
Select statement:
The SELECT statement is used to select data from a database.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
Here, column1, column2, ... are the field names of the table you want to
select data from. If you want to select all the fields available in the table, use
the following syntax:
SELECT * FROM table_name;
The SELECT statement is probably the most important SQL command. It’s used to return
results from our database(s) and no matter how easy that could sound, it could be really
very complex. In this article, we’ll give an intro to the SELECT statement and we’ll cover
more complex stuff in the upcoming articles.
Motivation
In this series, we had 4 articles so far and we’ve created a simple database, populated it with
some data and explained what are primary and foreign keys. These are prerequisites needed to
start “playing” with our data:
In real-life situations, you’ll probably won’t insert all the data into the database. Data shall be
either inserted manually by multiple users of your application/system or by some automated
process(es). In these cases, you’ll either: