plsql
plsql
What is PL/SQL?
PL/SQL is oracle’s procedural language extension to SQL. PL/SQL allows to mix SQL statements with procedural statements.
PL/SQL is superset of SQL.
It was developed by oracle’s corporation early 90’s to enhance the capabilities.
Features of PL/SQL
• PL/SQL has the following features
• PL/SQL is tightly integrated with SQL.
• It offers extensive error checking.
• It offers numerous data types.
• It offers a variety of programming structures.
• It supports structured programming through functions and procedures.
• It supports object-oriented programming.
• It supports the development of web applications and server pages.
Advantages of PL/SQL
PL/SQL has the following advantages −
• SQL is the standard database language and PL/SQL is strongly integrated with SQL.
• PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and
transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding DDL statements
in PL/SQL blocks.
• PL/SQL allows sending an entire block of statements to the database at one time. This reduces
network traffic and provides high performance for the applications.
• PL/SQL gives high productivity to programmers as it can query, transform, and update data in a
database.
• PL/SQL saves time on design and debugging by strong features, such as exception handling,
encapsulation, data hiding, and object-oriented data types.
• Applications written in PL/SQL are fully portable.
• PL/SQL provides high security level.
• PL/SQL provides access to predefined SQL packages.
• PL/SQL provides support for Object-Oriented Programming.
• PL/SQL provides support for developing Web Applications and Server Pages.
//Simple hello world program
CREATE PROCEDURE `hello_world`()
DECLARE
message VARCHAR(20);
SET message = 'Hello, World!’;
SELECT message AS OutputMessage;
end
Assignment Operator ( := )
Assignment operator allows a value to be stored in a variable.
Syntax: variable := expression;
The following are examples of assignment operator:
count := 1;
name := ‘Srikanth’;
DECLARE.:It is an optional section and defines all variables, cursors, subprograms, and other
elements to be used in the program.
Executable Commands: This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the program. It should have
at least one executable line of code, which may be just a NULL command to indicate that nothing
should be executed.
Exception Handling: This section starts with the keyword EXCEPTION. This optional section contains
exception(s) that handle errors in the program.
DECLARE
<declaration section>
BEGIN
<Executable commands>
EXCEPTION
<exception block>
//pl/sql program for declaring and assigning values
1.CREATE PROCEDURE `declare_variables`()
BEGIN
-- Variable declarations
DECLARE my_number INT;
DECLARE my_text VARCHAR(255);
DECLARE my_date DATE;
-- Your PL/SQL logic goes here
-- You can use and manipulate the declared variables within this block
SET my_number := 42; -- Assign a value to the variable
SET my_text := 'Hello, PL/SQL!';
SET my_date := CURRENT_DATE; -- Use CURRENT_DATE instead of SYSDATE in MySQL
-- Output the values (you can use SELECT or other methods in MySQL)
SELECT my_number, my_text, my_date;
END
2.CREATE PROCEDURE `declare_variables`()
BEGIN
DECLARE my_number INT DEFAULT 42;
DECLARE my_text VARCHAR(60) DEFAULT 'Hello, MySQL!';
DECLARE my_date DATE DEFAULT CURRENT_DATE;
Comments in PL/SQL
You can give comments in PL/SQL block in two ways.
First way is by preceding the comment with two hyphens (- -).
IF - THEN statement:
THEN
Statements;
END IF;
Ex:
CREATE PROCEDURE `simple_if_example`(IN value INT)
Begin
DECLARE result_message VARCHAR(50);
IF value > 0 THEN
SET result_message = 'Value is positive';
END IF ;
SELECT result_message AS Result;
END
2.IF-THEN-ELSE statement
In this user executes true block as well as false block statements.
Syntax for the IF-THEN-ELSE statement is:
IF condition
THEN
S1;
ELSE
S2;
END IF;
Where, S1 and S2 are different sequence of statements. In the IF-THEN-ELSE statements,
when the test condition is TRUE, the statement S1 is executed and S2 is skipped; when the
test condition is FALSE, then statement S2 is executed.
Example for if then else statement
CREATE PROCEDURE `if_then_else`(IN value INT)
BEGIN DECLARE result_message VARCHAR(50);
IF value > 0 THEN
SET result_message := 'Value is positive’;
else
SET result_message := 'Value is negative’;
end if;
SELECT result_message AS Result;
END
3. IF-THEN-ELSEIF statement
When we mention 2 or more conditions on one program we use this syntax.
The syntax of an IF-THEN-ELSIF Statement in PL/SQL programming language is:
IF (condition 1) THEN
S1; -- Executes when the condition 1 is true
ELSE IF (condition 2)
THEN
S2; -- Executes when the condition 2 is true
ELSE IF (condition 3)
THEN
S3; -- Executes when the condition 3 is true
ELSE
S4; -- executes when the none of the above condition is true END IF;
Ex:
CREATE PROCEDURE `if_then_else_if`(IN value INT)
BEGIN
DECLARE result_message VARCHAR(50);
IF value > 0 THEN
SET result_message = 'Value is positive';
ELSEIF value < 0 THEN
SET result_message = 'Value is negative';
ELSE
SET result_message = 'Value is one';
END IF;
SELECT result_message AS Result;
END
Nested IF-THEN
It is always legal in PL/SQL programming to nest IF-ELSE statements, which means you can use one IF or ELSE IF
statement inside another IF or ELSE IF statement(s).
Syntax:
IF( condition 1)
THEN
IF(condition 2)
THEN
sequence-of-statements;
END IF;
END IF;
Ex:
CREATE PROCEDURE `nested_if`()
BEGIN
DECLARE a INT DEFAULT 100;
DECLARE b INT DEFAULT 200;
IF a = 100 THEN
IF b = 200 THEN
SELECT 'Value of a is 100 and b is 200' AS message;
END IF;
END IF;
END
Case statement
Like the IF statement, the CASE statement selects one sequence of statements to execute. This statement will select any one
of several alternatives.
The syntax for case statement in PL/SQL is:
CASE selector
WHEN 'value1' THEN Statement1;
WHEN 'value2' THEN Statement2;
WHEN 'value3' THEN Statement3;
...
ELSE Statement n; -- default case
END CASE;
Ex:
CREATE PROCEDURE `example_case_statement`(IN value INT)
Begin
DECLARE result_message VARCHAR(50);
CASE WHEN value > 0 THEN
SET result_message = 'Value is positive';
WHEN value < 0 THEN
SET result_message = 'Value is negative';
ELSE
SET result_message = 'Value is zero’;
END CASE;
SELECT result_message AS Result;END
Looping Control Statements
PL/SQL Basic LOOP
Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements. With each
iteration, the sequence of statements is executed and then control resumes at the top of the loop.
Ex:
CREATE PROCEDURE `example_while_loop`()
BEGIN
DECLARE counter INT DEFAULT 1;
WHILE counter <= 3 DO
SELECT counter AS CurrentCounter;
SET counter = counter + 1;
END WHILE;
END
Nested loops in PL/SQL
PL/SQL allows using one loop inside another loop.
The syntax for a nested basic LOOP statement in PL/SQL is as follows:
LOOP
Sequence of statements1
LOOP
Sequence of statements2
END LOOP;
END LOOP;
1.Procedure
“Procedure is a subprogram is a program unit/module that performs a particular action”.
a stored procedure is a set of SQL statements that can be stored and executed on the server
syntax:
CREATE PROCEDURE procedure_name(IN parameter1 datatype, IN parameter2 datatype, OUT
output_parameter datatype)
BEGIN
-- SQL statements
-- You can use the input parameters and perform operations
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.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over the context area. An
explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a
SELECT Statement which returns more than one row.
The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other han
a procedure may or may not return a value.
•The stored function can return only a single value defined in the function header.
•The stored function may also be called within SQL statements.
•It may not produce a result set.