Your cart is currently empty!
Category: plsql
-
Khóa học miễn phí PL/SQL – Variables nhận dự án làm có lương
PL/SQL – Variables
In this chapter, we will discuss Variables in Pl/SQL. A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in PL/SQL has a specific data type, which determines the size and the layout of the variable”s memory; the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
The name of a PL/SQL variable consists of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not exceed 30 characters. By default, variable names are not case-sensitive. You cannot use a reserved PL/SQL keyword as a variable name.
PL/SQL programming language allows to define various types of variables, such as date time data types, records, collections, etc. which we will cover in subsequent chapters. For this chapter, let us study only basic variable types.
Variable Declaration in PL/SQL
PL/SQL variables must be declared in the declaration section or in a package as a global variable. When you declare a variable, PL/SQL allocates memory for the variable”s value and the storage location is identified by the variable name.
The syntax for declaring a variable is −
variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]
Where, variable_name is a valid identifier in PL/SQL, datatype must be a valid PL/SQL data type or any user defined data type which we already have discussed in the last chapter. Some valid variable declarations along with their definition are shown below −
sales number(10, 2); pi CONSTANT double precision := 3.1415; name varchar2(25); address varchar2(100);
When you provide a size, scale or precision limit with the data type, it is called a constrained declaration. Constrained declarations require less memory than unconstrained declarations. For example −
sales number(10, 2); name varchar2(25); address varchar2(100);
Initializing Variables in PL/SQL
Whenever you declare a variable, PL/SQL assigns it a default value of NULL. If you want to initialize a variable with a value other than the NULL value, you can do so during the declaration, using either of the following −
-
The DEFAULT keyword
-
The assignment operator
For example −
counter binary_integer := 0; greetings varchar2(20) DEFAULT ''Have a Good Day
You can also specify that a variable should not have a NULL value using the NOT NULL constraint. If you use the NOT NULL constraint, you must explicitly assign an initial value for that variable.
It is a good programming practice to initialize variables properly otherwise, sometimes programs would produce unexpected results. Try the following example which makes use of various types of variables −
DECLARE a integer := 10; b integer := 20; c integer; f real; BEGIN c := a + b; dbms_output.put_line(''Value of c: '' || c); f := 70.0/3.0; dbms_output.put_line(''Value of f: '' || f); END; /
When the above code is executed, it produces the following result −
Value of c: 30 Value of f: 23.333333333333333333 PL/SQL procedure successfully completed.
Variable Scope in PL/SQL
PL/SQL allows the nesting of blocks, i.e., each program block may contain another inner block. If a variable is declared within an inner block, it is not accessible to the outer block. However, if a variable is declared and accessible to an outer block, it is also accessible to all nested inner blocks. There are two types of variable scope −
-
Local variables − Variables declared in an inner block and not accessible to outer blocks.
-
Global variables − Variables declared in the outermost block or a package.
Following example shows the usage of Local and Global variables in its simple form −
DECLARE -- Global variables num1 number := 95; num2 number := 85; BEGIN dbms_output.put_line(''Outer Variable num1: '' || num1); dbms_output.put_line(''Outer Variable num2: '' || num2); DECLARE -- Local variables num1 number := 195; num2 number := 185; BEGIN dbms_output.put_line(''Inner Variable num1: '' || num1); dbms_output.put_line(''Inner Variable num2: '' || num2); END; END; /
When the above code is executed, it produces the following result −
Outer Variable num1: 95 Outer Variable num2: 85 Inner Variable num1: 195 Inner Variable num2: 185 PL/SQL procedure successfully completed.
Assigning SQL Query Results to PL/SQL Variables
You can use the SELECT INTO statement of SQL to assign values to PL/SQL variables. For each item in the SELECT list, there must be a corresponding, type-compatible variable in the INTO list. The following example illustrates the concept. Let us create a table named CUSTOMERS −
(For SQL statements, please refer to the )
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Table Created
Let us now insert some values in the table −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, ''Khilan'', 25, ''Delhi'', 1500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, ''kaushik'', 23, ''Kota'', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (5, ''Hardik'', 27, ''Bhopal'', 8500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (6, ''Komal'', 22, ''MP'', 4500.00 );
The following program assigns values from the above table to PL/SQL variables using the SELECT INTO clause of SQL −
DECLARE c_id customers.id%type := 1; c_name customers.name%type; c_addr customers.address%type; c_sal customers.salary%type; BEGIN SELECT name, address, salary INTO c_name, c_addr, c_sal FROM customers WHERE id = c_id; dbms_output.put_line (''Customer '' ||c_name || '' from '' || c_addr || '' earns '' || c_sal); END; /
When the above code is executed, it produces the following result −
Customer Ramesh from Ahmedabad earns 2000 PL/SQL procedure completed successfully
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí PL/SQL – Home nhận dự án làm có lương

PL/SQL Tutorial
PL/SQL Tutorial
PL/SQL, which stands for Procedural Language extensions to the (SQL). It is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90”s to enhance the capabilities of SQL. PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and .
In this tutorial, we”ll give you a great understanding of PL/SQL to proceed with the Oracle database and other advanced .
Purpose of PL/SQL
The purpose of PL/SQL is to merge database commands with procedural programming language. It offers more complete programming solutions for building critical applications that operate on the Oracle database.
Features of PL/SQL
PL/SQL has the following features −
- PL/SQL is tightly integrated with SQL.
- It offers extensive extensive error checking mechanisms.
- It supports numerous for flexible data handling.
- Includes a variety of programming structures, such as and . Includes a variety of programming structures, such as loops and conditionals.
- It supports structured programming through functions and procedures.
- It supports object-oriented programming, enabling more complex data handling and manipulation.
- It supports the web application development and server pages.
Why to learn PL/SQL?
Learning PL/SQL is an essential skill for persons who are interested in databases and other advanced RDBMS technologies. PL/SQL offers various benefits, making it an essential skill for database developers −
- Ease of Use: PL/SQL is straightforward to write and read, featuring block-structured syntax which simplifies programming and debugging.
- Portability: Programs written in PL/SQL are fully portable across different Oracle databases, ensuring consistency and ease of migration.
- Tight SQL Integration: PL/SQL is tightly integrated with SQL, allowing for efficient querying, transforming, and updating of data within a database.
- High Performance: It reduces network traffic by sending entire blocks of statements to the database at once, thus improving performance.
- Security: It includes robust security features to protect database integrity.
- Object-Oriented Support: It supports object-oriented programming, and allows you to define object types that can be used in object-oriented designs.
PL/SQL Block Structured
PL/SQL follows a block-structured approach, dividing programs into logical blocks of code. Each block consists of three main sections −
- Declarations: This section, starting with the keyword DECLARE, is optional and used for defining , , subprograms, and other elements required within the block.
- Executable Commands: Enclosed between the keywords BEGIN and END, this mandatory section contains executable PL/SQL statements. It must include at least one executable line of code, even if it”s just a NULL command indicating no action.
- Exception Handling: This starts with the keyword EXCEPTION, this optional section deals with handling errors in the program through defined exceptions.
PL/SQL statements are terminated with a semicolon(;). Additionally, blocks can be nested within each other using BEGIN and END keywords.
Applications of PL/SQL
PL/SQL is widely used in various applications, including −
- Database Security: It implements robust security measures within the database.
- XML Management: Generating and managing XML documents within the database.
- Linking Databases to Web Pages: Integrates databases with web applications.
- Automation: Automating database administration tasks for efficient management.
Who Should Learn PL/SQL?
This tutorial is designed for Software Professionals, who are willing to learn PL/SQL Programming Language in simple and easy steps. This tutorial will give you a great understanding of PL/SQL Programming concepts, and after completing this tutorial, you will be at an intermediate level of expertise from where you can take yourself to a higher level of expertise.
Prerequisites to learn PL/SQL
Before proceeding with this tutorial, you should have a basic understanding of software concepts like what is database, source code, text editor, and execution of programs, etc. If you already have an understanding of SQL and other computer programming languages, then it will be an added advantage to proceed. Let”s get started!
PL/SQL Jobs and Opportunities
Proficiency in PL/SQL opens up various career opportunities, such as −
- Oracle PL/SQL Programmer
- PL/SQL Developer
- Database Developer
- Data Analyst
- Database Testers
- Data Scientist
- ETL Developer
- Database Migration Expert
- Cloud Database Expert etc
By mastering PL/SQL, you can increase your career opportunities in database management and development, as well as in creating secure and scalable applications.
Frequently Asked Questions about PL/SQL
There are some very Frequently Asked Questions(FAQ) about PL/SQL, this section tries to answer them briefly.
PL/SQL records are data structures designed to hold multiple data items of different types. They consist of various fields, much like a row in a database table.
SQL (Structured Query Language) is a standard language used for creating, manipulating, and retrieving data from relational databases. SQL is mainly used to write queries, as well as create and execute DDL (Data Definition Language) and DML (Data Manipulation Language) statements.
Whereas, PL/SQL (Procedural Language/SQL) is an extension of SQL. And it adds procedural capabilities to SQL, enabling the creation of more complex and powerful database applications. PL/SQL supports variables, data types, and control structures such as loops and conditionals, which SQL does not. This makes PL/SQL more efficient for writing program blocks, functions, procedures, triggers, and packages.
When an exception is raised in PL/SQL, the current PL/SQL block stops its regular execution and transfers control to the exception section. The exception is then handled by an exception handler within the current PL/SQL block or passed to the enclosing block if not handled locally.
To manage an exception after it is raised, you need to write an exception handler for it. This handler should be placed after all executable statements in your PL/SQL block but before the END statement. The EXCEPTION keyword indicates the start of the exception-handling section.
The best place to learn PL/SQL is through our comprehensive and user-friendly tutorial. Our PL/SQL tutorial provides an excellent starting point for understanding database programming with PL/SQL. You can explore our simple and effective learning materials at your own pace.
A PL/SQL table, also known as an associative array or index-by table, is a collection of key-value pairs where each key is a unique index used to access the corresponding value. This table functions similarly to a relational table, storing data in rows and columns. Each column represents a different attribute or value, while each row contains individual records with values for all the columns.
Following are some tips to learn PL/SQL −
- The first and most crucial step is to decide to learn PL/SQL and stay committed to your goal.
- Install the necessary tools like SQL*Plus or Oracle SQL Developer on your computer.
- Start with our PL/SQL tutorial and progress step by step from the basics.
- Read more articles, watch online courses, or buy a book on PL/SQL to deepen your understanding.
- Apply what you’ve learned by developing small projects that incorporate PL/SQL and other technologies.
SQL*Plus and PL/SQL Developer are commonly used for writing and executing PL/SQL code.
In PL/SQL, there are two types of parameters −
- Actual Parameters: Actual parameters are the values or expressions provided in the parameter list when calling a module. In this only the variable is mentioned, not the data types.
- Formal Parameters: Formal parameters are the names declared in the parameter list of a module”s header.
Learning PL/SQL is crucial for a person who is interested in databases and advanced RDBMS technologies. It offers numerous benefits, making it an essential skill for database developers. With PL/SQL, you can efficiently manipulate and manage data within Oracle databases, which increases your ability to work with large datasets and complex queries.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc