Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

1 Intro To PLSQL

Uploaded by

sharma.pihu7171
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

1 Intro To PLSQL

Uploaded by

sharma.pihu7171
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PL/SQL

PL/SQL is a block structured language that can have multiple blocks in it. The programs of
PL/SQL are logical blocks that can contain any number of nested sub-blocks. Pl/SQL stands
for "Procedural Language extension of SQL" that is used in Oracle. Although PL/SQL is
closely integrated with SQL language, yet it adds some programming constraints that are not
available in SQL.

The PL/SQL is known for its combination of data manipulating power of SQL with
data processing power of procedural languages. It inherits the robustness, security,
and portability of the Oracle Database.

PL/SQL is not case sensitive so you are free to use lower case letters or upper case
letters except within string and character literals. A line of PL/SQL text contains
groups of characters known as lexical units. It can be classified as follows:

o Delimeters
o Identifiers
o Literals
o Comments

How to declare variable in PL/SQL


You must declare the PL/SQL variable in the declaration section or in a package as a
global variable. After the declaration, PL/SQL allocates memory for the variable's
value and the storage location is identified by the variable name.

Syntax for declaring variable:

Following is the syntax for declaring variable:

1. variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]

Here, variable_name is a valid identifier in PL/SQL and datatype must be valid PL/SQL
data type. A data type with size, scale or precision limit is called a constrained
declaration. The constrained declaration needs less memory than unconstrained
declaration.

Example:

Radius Number := 5;
Date_of_birth date;

Naming rules for PL/SQL variables


The variable in PL/SQL must follow some naming rules like other programming
languages.

o The variable_name should not exceed 30 characters.


o Variable name should not be the same as the table table's column of that block.
o The name of the variable must begin with ASCII letter. The PL/SQL is not case
sensitive so it could be either lowercase or uppercase. For example: v_data and
V_DATA refer to the same variables.
o You should make your variable easy to read and understand, after the first character,
it may be any number, underscore (_) or dollar sign ($).
o NOT NULL is an optional specification on the variable.

Example of initializing variable


Let's take a simple example to explain it well:

1. DECLARE
2. a integer := 30;
3. b integer := 40;
4. c integer;
5. f real;
6. BEGIN
7. c := a + b;
8. dbms_output.put_line('Value of c: ' || c);
9. f := 100.0/3.0;
10. dbms_output.put_line('Value of f: ' || f);
11. END;

After the execution, this will produce the following result:

Value of c: 70
Value of f: 33.333333333333333333

PL/SQL procedure successfully completed.


Variable Scope in PL/SQL:
PL/SQL allows nesting of blocks. A program block can contain another inner block. If
you declare a variable within an inner block, it is not accessible to an outer block.
There are two types of variable scope:

o Local Variable: Local variables are the inner block variables which are not accessible
to outer blocks.
o Global Variable: Global variables are declared in outermost block.

Example of Local and Global variables


Let's take an example to show the usage of Local and Global variables in its simple
form:

1. DECLARE
2. -- Global variables
3. num1 number := 95;
4. num2 number := 85;
5. BEGIN
6. dbms_output.put_line('Outer Variable num1: ' || num1);
7. dbms_output.put_line('Outer Variable num2: ' || num2);
8. DECLARE
9. -- Local variables
10. num1 number := 195;
11. num2 number := 185;
12. BEGIN
13. dbms_output.put_line('Inner Variable num1: ' || num1);
14. dbms_output.put_line('Inner Variable num2: ' || num2);
15. END;
16. END;
17. /

After the execution, this will produce the following result:

Outer Variable num1: 95


Outer Variable num2: 85
Inner Variable num1: 195
Inner Variable num2: 185

PL/SQL procedure successfully completed.


PL/SQL Constants
A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of
actual values.

Let's take an example to explain it well:

Suppose, you have to write a program which will increase the salary of the
employees upto 30%, you can declare a constant and use it throughout the program.
Next time if you want to increase the salary again you can change the value of
constant than the actual value throughout the program.

Syntax to declare a constant:

1. constant_name CONSTANT datatype := VALUE;


o Constant_name:it is the name of constant just like variable name. The constant word
is a reserved word and its value does not change.
o VALUE: it is a value which is assigned to a constant when it is declared. It can not be
assigned later.

Example of PL/SQL constant


Let's take an example to explain it well:

1. DECLARE
2. -- constant declaration
3. pi constant number := 3.141592654;
4. -- other declarations
5. radius number(5,2);
6. dia number(5,2);
7. circumference number(7, 2);
8. area number (10, 2);
9. BEGIN
10. -- processing
11. radius := 9.5;
12. dia := radius * 2;
13. circumference := 2.0 * pi * radius;
14. area := pi * radius * radius;
15. -- output
16. dbms_output.put_line('Radius: ' || radius);
17. dbms_output.put_line('Diameter: ' || dia);
18. dbms_output.put_line('Circumference: ' || circumference);
19. dbms_output.put_line('Area: ' || area);
20. END;
21. /

After the execution of the above code at SQL prompt, it will produce the following
result:.

1. Radius: 9.5
2. Diameter: 19
3. Circumference: 59.69
4. Area: 283.53
5.
6. Pl/SQL procedure successfully completed.

PL/SQL Literals
Literals are the explicit numeric, character, string or boolean values which are not
represented by an identifier. For example: TRUE, NULL, etc. are all literals of type
boolean. PL/SQL literals are case-sensitive. There are following kinds of literals in
PL/SQL:

o Numeric Literals
o Character Literals
o String Literals
o BOOLEAN Literals
o Date and Time Literals

Example of these different types of


Literals:
Literals Examples

Numeric 75125, 3568, 33.3333333 etc.


Character 'A' '%' '9' ' ' 'z' '('

String Hello world

Boolean TRUE, FALSE, NULL etc.

Date and Time '26-11-2002' , '2012-10-29 12:01:01'

Introduction to PL/SQL Comments

PL/SQL comments allow you to describe the purpose of a line or a block of PL/SQL
code.

When compiling the PL/SQL code, the Oracle precompiler ignores comments.
However, you should always use comments to make your code more readable and to
help you and other developers understand it better in the future.

Single-line comments

A single-line comment starts with a double hyphen ( --) that can appear anywhere on
a line and extends to the end of the line.

Multi-line comments

A multi-line comment starts with a slash-asterisk ( /* ) and ends with an asterisk-slash


( */ ), and can span multiple lines:

We often use a multi-line comment to describe the purpose of a block of code like
the following example:

/*
This code allow users to enter the customer id and
return the corresponding customer name and credit limit
*/
DECLARE
l_customer_name customers.name%TYPE;
l_credit_limit customers.credit_limit%TYPE;
BEGIN
...
END;
/

You might also like