Passion for technology

Challenging the challenges over a decade. Place for Oracle DBAs, Oracle Apps DBAs, Unix Administrators and Developers

Archive for the ‘SQL’ Category

PL/SQL Part-1

Posted by Sanjay on October 12, 2011

What is PL/SQL?

PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation to enhance the capabilities of SQL.
What is PL/SQL Engine?

Oracle uses PL/SQL engine to processes the PL/SQL statements, A PL/SQL code can be stored in the client system (client-side) or in the database (server-side).

What are advantages of PL/SQL?

A. Block Structures: PL/SQL consists of blocks of code, which can be nested within each other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be stored in the database and reused.
B. Procedural Language Capability: PL/SQL consists of procedural language constructs such as conditional statements and loops.
C. Better Performance: PL SQL engine processes multiple SQL statements simultaneously as a single block, thereby reducing network traffic.
D. Error Handling: PL/SQL handles errors or exceptions effectively during the execution of a PL/SQL program. Once an exception is caught, specific actions can be taken depending upon the type of the exception or it can be displayed to the user with a message.

What are PL/SQL Placeholders?
Placeholders are temporary storage area, those can be any of Variables, Constants and Records. Oracle defines placeholders to store data temporarily, which are used to manipulate data during the execution of a PL SQL block. Depending on the kind of data you want to store, you can define placeholders with a name and a data type. Few of the data types used to define placeholders are as under.
Number (n,m) , Char (n) , Varchar2 (n) , Date , Long , Long raw, Raw, Blob, Clob, Nclob, Bfile

What is a PL/SQL Block?
Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block and a PL/SQL block consist three sections:
A. The Declaration section (optional).
B. The Execution section (mandatory).
C. The Exception or Error handling section (optional).

A. The Declaration section (optional):
The Declaration section of a PL/SQL Block starts with the reserved keyword DECLARE. This section is optional and is used to declare any placeholders. Placeholders (may be any of below type) which stores data temporarily.
1. Variables
2. Constants
3. Cursors
4. Records
5. Records those are used to manipulate data in the execution section.

B. The Execution section (mandatory):
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends with END. This is a MUST section where the program logic is written to perform any task/s. The programmatic constructs like form the part of execution section.
1. Conditional statement
2. SQL statements
3. Loops

C. The Exception or Error handling section (optional):
The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION. Any errors in the program can be handled in this section, so that the PL/SQL Blocks terminates gracefully. If the PL/SQL Block contains exceptions that cannot be handled, the Block terminates abruptly with errors.
1. Every statement in the above three sections must end with ; (semicolon).
2. PL/SQL blocks can be nested within other PL/SQL blocks.
3. Comments can be used to document code.
This is how a sample PL/SQL Block looks.

1. HOW to count and group together.

select distinct(column_1),count(*) from table_name group by column_1 ;

Posted in SQL | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.