What is PL/SQL?

What is PL/SQL?

Q. What is PL/SQL?

In English

PL/SQL stands for Procedural Language extension of SQL. It is an advanced programming language developed by Oracle that combines SQL with procedural programming features. It allows users to write complex database programs using variables, loops, conditions, and exceptions.

Features:

  • Combines SQL with procedural language.
  • Supports variables, loops, and conditions.
  • Supports exception handling for errors.
  • Increases performance by sending blocks instead of single queries.
  • Portable and secure.

Syntax:

DECLARE  
   variable_name datatype;  
BEGIN  
   SQL statements;  
EXCEPTION  
   WHEN exception_name THEN  
      statements;  
END;  

Example:

DECLARE  
   v_total NUMBER := 100;  
BEGIN  
   v_total := v_total + 50;  
   DBMS_OUTPUT.PUT_LINE('Total = ' || v_total);  
EXCEPTION  
   WHEN OTHERS THEN  
      DBMS_OUTPUT.PUT_LINE('An error occurred');  
END;  

हिन्दी में

PL/SQL का पूरा नाम है Procedural Language extension of SQL। यह Oracle द्वारा विकसित एक उन्नत प्रोग्रामिंग भाषा है जो SQL को procedural फीचर्स के साथ जोड़ती है। इसमें हम variables, loops, conditions और exceptions का प्रयोग कर जटिल प्रोग्राम बना सकते हैं।

विशेषताएँ:

  • SQL और procedural भाषा का संयोजन।
  • Variables, loops और conditions का उपयोग।
  • Error के लिए exception handling की सुविधा।
  • एक साथ पूरा block भेजने से performance बेहतर।
  • Portable और सुरक्षित भाषा।

Syntax:

DECLARE  
   variable_name datatype;  
BEGIN  
   SQL statements;  
EXCEPTION  
   WHEN exception_name THEN  
      statements;  
END;  

उदाहरण:

DECLARE  
   v_total NUMBER := 100;  
BEGIN  
   v_total := v_total + 50;  
   DBMS_OUTPUT.PUT_LINE('Total = ' || v_total);  
EXCEPTION  
   WHEN OTHERS THEN  
      DBMS_OUTPUT.PUT_LINE('An error occurred');  
END;  

Q. What are Advantages and Disadvantages of PL/SQL?

In English

Advantages:

  • Combines SQL and procedural language for better programming.
  • Reduces network traffic by sending blocks instead of single queries.
  • Supports error handling using exceptions.
  • Increases performance and efficiency of applications.
  • Provides modular programming using functions, procedures, and packages.
  • Offers high security and reusability of code.

Disadvantages:

  • Works only with Oracle Database, not portable to others.
  • Difficult to debug large programs.
  • Requires knowledge of both SQL and procedural programming.
  • Slower execution compared to compiled languages like C or Java.

हिन्दी में

फायदे:

  • SQL और procedural भाषा का संयोजन करता है जिससे प्रोग्रामिंग आसान होती है।
  • एक बार में पूरा block भेजने से नेटवर्क ट्रैफिक कम होता है।
  • Exception के माध्यम से error handling की सुविधा।
  • एप्लिकेशन की performance और efficiency बढ़ती है।
  • Functions, procedures और packages द्वारा modular programming की सुविधा।
  • कोड की सुरक्षा और पुन: उपयोग (reusability) आसान।

नुकसान:

  • केवल Oracle Database में कार्य करता है, अन्य डेटाबेस में नहीं।
  • बड़े प्रोग्रामों को debug करना कठिन।
  • SQL और procedural दोनों की जानकारी आवश्यक।
  • C या Java जैसी compiled भाषाओं की तुलना में execution धीमा।

Q. Difference between SQL and PL/SQL

In English

BasisSQLPL/SQL
Full FormStructured Query LanguageProcedural Language/Structured Query Language
TypeNon-procedural languageProcedural language
ExecutionExecutes one command at a timeExecutes a block of code at a time
UseUsed to perform operations like SELECT, INSERT, UPDATE, DELETEUsed to write complete programs with logic, loops, and conditions
Error HandlingNo error handlingSupports exception handling
VariablesCannot use variablesSupports variables and constants
PerformanceMore network traffic, slower for multiple queriesLess network traffic, faster as blocks are sent
ExampleSELECT * FROM STUDENTS;BEGIN SELECT * FROM STUDENTS; END;

हिन्दी में

आधारSQLPL/SQL
पूरा नामStructured Query LanguageProcedural Language/Structured Query Language
प्रकारNon-procedural भाषाProcedural भाषा
निष्पादनएक समय में एक कमांड चलती हैपूरा कोड ब्लॉक एक साथ चलता है
उपयोगडेटा जोड़ने, बदलने, हटाने के लिएलॉजिक, लूप और कंडीशन के साथ पूरा प्रोग्राम लिखने के लिए
Error HandlingError handling नहीं होतीException handling की सुविधा होती है
VariablesVariables का उपयोग नहीं कर सकतेVariables और constants का उपयोग कर सकते हैं
Performanceज़्यादा network traffic और धीमाकम network traffic और तेज़
उदाहरणSELECT * FROM STUDENTS;BEGIN SELECT * FROM STUDENTS; END;

Q. Explain PL/SQL Block Structure

In English

A PL/SQL block is the basic unit of a PL/SQL program. It groups statements logically and executes them together. Every block has three main sections: Declaration, Execution, and Exception Handling.

Structure of PL/SQL Block:

DECLARE
   -- Declaration of variables, constants, cursors
BEGIN
   -- Executable statements
EXCEPTION
   -- Error handling statements
END;

Explanation:

  • DECLARE Section: Optional part where variables, constants, and cursors are declared.
  • BEGIN Section: Mandatory part where actual code or SQL statements are written.
  • EXCEPTION Section: Optional part that handles runtime errors.
  • END: Marks the end of the PL/SQL block.

Example:

DECLARE
   num NUMBER := 10;
BEGIN
   DBMS_OUTPUT.PUT_LINE('The number is: ' || num);
EXCEPTION
   WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('Error occurred');
END;

हिन्दी में

PL/SQL ब्लॉक एक प्रोग्राम की मूल इकाई होती है। इसमें सभी स्टेटमेंट्स को तार्किक रूप से समूहित किया जाता है और एक साथ चलाया जाता है। हर ब्लॉक के तीन मुख्य भाग होते हैं — Declaration, Execution, और Exception Handling

PL/SQL ब्लॉक की संरचना:

DECLARE
   -- वेरिएबल्स, कॉन्स्टेंट्स, कर्सर की घोषणा
BEGIN
   -- निष्पादित होने वाले स्टेटमेंट्स
EXCEPTION
   -- Error हैंडलिंग के स्टेटमेंट्स
END;

व्याख्या:

  • DECLARE भाग: वैकल्पिक भाग जहाँ वेरिएबल्स और कॉन्स्टेंट्स घोषित किए जाते हैं।
  • BEGIN भाग: आवश्यक भाग जहाँ मुख्य कोड लिखा जाता है।
  • EXCEPTION भाग: वैकल्पिक भाग जहाँ रनटाइम त्रुटियों को संभाला जाता है।
  • END: ब्लॉक का अंत दर्शाता है।

उदाहरण:

DECLARE
   num NUMBER := 10;
BEGIN
   DBMS_OUTPUT.PUT_LINE('The number is: ' || num);
EXCEPTION
   WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('Error occurred');
END;

Q. What is Variable and its Naming Rules?

In English

A variable in PL/SQL is a named memory location used to store data that can change during program execution. It allows storing values like numbers, characters, or dates, which can be used and modified in the program.

Example:

DECLARE
   name VARCHAR2(20);
   age NUMBER := 25;
BEGIN
   name := 'Gopal';
   DBMS_OUTPUT.PUT_LINE('Name: ' || name || ', Age: ' || age);
END;

Naming Rules of Variables:

  • The variable name must begin with a letter.
  • It can include letters, digits, and underscore (_).
  • It cannot start with a number.
  • Variable names are not case-sensitive.
  • Reserved keywords of PL/SQL cannot be used as variable names.
  • The maximum length of a variable name is 30 characters.

हिन्दी में

वेरिएबल (Variable) PL/SQL में एक नामित मेमोरी स्थान होता है, जिसका उपयोग डेटा को संग्रहीत करने के लिए किया जाता है जो प्रोग्राम के चलने के दौरान बदल सकता है। इसमें संख्या, अक्षर या तिथि जैसे मानों को रखा और बदला जा सकता है।

उदाहरण:

DECLARE
   name VARCHAR2(20);
   age NUMBER := 25;
BEGIN
   name := 'Gopal';
   DBMS_OUTPUT.PUT_LINE('Name: ' || name || ', Age: ' || age);
END;

वेरिएबल के नामकरण के नियम:

  • वेरिएबल का नाम अक्षर से शुरू होना चाहिए।
  • इसमें अक्षर, अंक और अंडरस्कोर (_) शामिल हो सकते हैं।
  • नाम किसी संख्या से शुरू नहीं हो सकता।
  • वेरिएबल नाम case-sensitive नहीं होते।
  • PL/SQL के रिज़र्व कीवर्ड्स का उपयोग नाम के रूप में नहीं किया जा सकता।
  • वेरिएबल नाम की अधिकतम लंबाई 30 अक्षर होती है।

Q. What do you mean by Control Structure?

In English

A Control Structure in PL/SQL is a set of statements that control the flow of execution of a program. It helps in making decisions, repeating actions, and executing code based on certain conditions.

There are mainly three types of control structures in PL/SQL:

  1. Sequential Control Structure – Statements are executed one after another in sequence.
  2. Selection Control Structure – Used for decision-making using IF...THEN, IF...THEN...ELSE, or CASE statements.
  3. Iterative Control Structure – Used for repeating tasks using loops like FOR, WHILE, and LOOP.

Example:

DECLARE
   marks NUMBER := 75;
BEGIN
   IF marks >= 40 THEN
      DBMS_OUTPUT.PUT_LINE('Pass');
   ELSE
      DBMS_OUTPUT.PUT_LINE('Fail');
   END IF;
END;

हिन्दी में

कंट्रोल स्ट्रक्चर (Control Structure) PL/SQL में ऐसे स्टेटमेंट्स का समूह होता है जो प्रोग्राम के निष्पादन के प्रवाह (flow) को नियंत्रित करता है। यह निर्णय लेने, कार्यों को दोहराने और शर्तों के आधार पर कोड चलाने में मदद करता है।

मुख्य रूप से तीन प्रकार के कंट्रोल स्ट्रक्चर होते हैं:

  1. Sequential Control Structure – स्टेटमेंट्स क्रम से एक के बाद एक चलाए जाते हैं।
  2. Selection Control Structure – निर्णय लेने के लिए उपयोग होता है जैसे IF...THEN, IF...THEN...ELSE, या CASE
  3. Iterative Control Structure – कार्यों को बार-बार दोहराने के लिए उपयोग होता है जैसे FOR, WHILE, और LOOP

उदाहरण:

DECLARE
   marks NUMBER := 75;
BEGIN
   IF marks >= 40 THEN
      DBMS_OUTPUT.PUT_LINE('Pass');
   ELSE
      DBMS_OUTPUT.PUT_LINE('Fail');
   END IF;
END;

Q. What do you mean by IF Statement? Explain its Types with Example.

In English

The IF statement in PL/SQL is used to make decisions in a program. It allows the program to execute certain statements only when a given condition is true. It helps in controlling the flow of execution based on conditions.

There are three types of IF statements in PL/SQL:

  1. Simple IF Statement – Executes statements only when the condition is true.
   IF condition THEN  
      statement;  
   END IF;  

Example:

   IF marks > 40 THEN  
      DBMS_OUTPUT.PUT_LINE('Pass');  
   END IF;  
  1. IF…THEN…ELSE Statement – Executes one block when the condition is true and another block when it is false.
   IF condition THEN  
      statement1;  
   ELSE  
      statement2;  
   END IF;  

Example:

   IF marks >= 40 THEN  
      DBMS_OUTPUT.PUT_LINE('Pass');  
   ELSE  
      DBMS_OUTPUT.PUT_LINE('Fail');  
   END IF;  
  1. IF…THEN…ELSIF…ELSE Statement – Used when there are multiple conditions to check.
   IF condition1 THEN  
      statement1;  
   ELSIF condition2 THEN  
      statement2;  
   ELSE  
      statement3;  
   END IF;  

Example:

   IF marks >= 80 THEN  
      DBMS_OUTPUT.PUT_LINE('Grade A');  
   ELSIF marks >= 60 THEN  
      DBMS_OUTPUT.PUT_LINE('Grade B');  
   ELSE  
      DBMS_OUTPUT.PUT_LINE('Grade C');  
   END IF;  

हिन्दी में

IF स्टेटमेंट PL/SQL में निर्णय लेने के लिए उपयोग किया जाता है। यह प्रोग्राम को केवल तभी कोई कार्य करने देता है जब दी गई शर्त (condition) सही होती है। इससे प्रोग्राम का प्रवाह नियंत्रित किया जा सकता है।

PL/SQL में IF स्टेटमेंट के तीन प्रकार होते हैं:

  1. Simple IF Statement – केवल तब स्टेटमेंट चलाता है जब शर्त सही हो।
   IF condition THEN  
      statement;  
   END IF;  

उदाहरण:

   IF marks > 40 THEN  
      DBMS_OUTPUT.PUT_LINE('Pass');  
   END IF;  
  1. IF…THEN…ELSE Statement – अगर शर्त सही है तो एक ब्लॉक चलाता है, अन्यथा दूसरा ब्लॉक।
   IF condition THEN  
      statement1;  
   ELSE  
      statement2;  
   END IF;  

उदाहरण:

   IF marks >= 40 THEN  
      DBMS_OUTPUT.PUT_LINE('Pass');  
   ELSE  
      DBMS_OUTPUT.PUT_LINE('Fail');  
   END IF;  
  1. IF…THEN…ELSIF…ELSE Statement – जब कई शर्तें चेक करनी हों तब उपयोग किया जाता है।
   IF condition1 THEN  
      statement1;  
   ELSIF condition2 THEN  
      statement2;  
   ELSE  
      statement3;  
   END IF;  

उदाहरण:

   IF marks >= 80 THEN  
      DBMS_OUTPUT.PUT_LINE('Grade A');  
   ELSIF marks >= 60 THEN  
      DBMS_OUTPUT.PUT_LINE('Grade B');  
   ELSE  
      DBMS_OUTPUT.PUT_LINE('Grade C');  
   END IF;  

What do you mean by Loop Statements / Iterative Control?

In PL/SQL, a loop statement or iterative control is used to execute a group of statements repeatedly as long as a given condition is true.
It helps in performing repetitive tasks without writing the same code again and again.

There are three main types of loops in PL/SQL:

  1. Simple Loop:
  • It executes a set of statements repeatedly.
  • The loop ends when the EXIT statement condition becomes true.
    Example:
   DECLARE
     i NUMBER := 1;
   BEGIN
     LOOP
       DBMS_OUTPUT.PUT_LINE(i);
       i := i + 1;
       EXIT WHEN i > 5;
     END LOOP;
   END;
  1. WHILE Loop:
  • The loop runs while the given condition is true.
  • The condition is checked before entering the loop.
    Example:
   DECLARE
     i NUMBER := 1;
   BEGIN
     WHILE i <= 5 LOOP
       DBMS_OUTPUT.PUT_LINE(i);
       i := i + 1;
     END LOOP;
   END;
  1. FOR Loop:
  • This loop runs for a fixed number of times.
  • It automatically increments the counter variable.
    Example:
   BEGIN
     FOR i IN 1..5 LOOP
       DBMS_OUTPUT.PUT_LINE(i);
     END LOOP;
   END;

Answer in Hindi:

Loop Statements / Iterative Control का क्या मतलब है?

PL/SQL में Loop Statement या Iterative Control का उपयोग किसी समूह के statements को बार-बार चलाने के लिए किया जाता है, जब तक कोई दी गई condition true रहती है।
यह बार-बार कोड लिखने की जरूरत को कम करता है और कार्य को आसान बनाता है।

PL/SQL में तीन प्रकार के Loop होते हैं:

  1. Simple Loop:
  • यह statements को बार-बार चलाता है।
  • Loop तब तक चलता है जब तक EXIT की condition true नहीं हो जाती।
    उदाहरण:
   DECLARE
     i NUMBER := 1;
   BEGIN
     LOOP
       DBMS_OUTPUT.PUT_LINE(i);
       i := i + 1;
       EXIT WHEN i > 5;
     END LOOP;
   END;
  1. WHILE Loop:
  • यह loop तब तक चलता है जब तक दी गई condition true रहती है।
  • Condition को loop शुरू होने से पहले check किया जाता है।
    उदाहरण:
   DECLARE
     i NUMBER := 1;
   BEGIN
     WHILE i <= 5 LOOP
       DBMS_OUTPUT.PUT_LINE(i);
       i := i + 1;
     END LOOP;
   END;
  1. FOR Loop:
  • यह loop एक निश्चित संख्या (fixed number) बार चलता है।
  • Counter variable अपने आप बढ़ता है।
    उदाहरण:
   BEGIN
     FOR i IN 1..5 LOOP
       DBMS_OUTPUT.PUT_LINE(i);
     END LOOP;
   END;

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *