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;

What do you mean by Trigger?

A Trigger in PL/SQL is a stored program that automatically executes (fires) in response to a specific event on a table or view.
It is used to maintain the integrity of the data or perform some automatic actions like inserting, updating, or deleting records.

Triggers are generally fired when any of the following actions occur:

  • INSERT
  • UPDATE
  • DELETE

Syntax:

CREATE OR REPLACE TRIGGER trigger_name
BEFORE | AFTER INSERT | UPDATE | DELETE
ON table_name
FOR EACH ROW
BEGIN
   -- Trigger logic here
END;

Example:

CREATE OR REPLACE TRIGGER trg_before_insert
BEFORE INSERT ON student
FOR EACH ROW
BEGIN
   DBMS_OUTPUT.PUT_LINE('A new student record is being inserted');
END;

Answer in Hindi:

Trigger का क्या मतलब है?

Trigger PL/SQL का एक stored program है जो अपने आप चल जाता है (automatically execute होता है) जब किसी table या view पर कोई विशेष event होता है।
इसका उपयोग data की integrity बनाए रखने और स्वचालित कार्य (automatic actions) करने के लिए किया जाता है जैसे कि insert, update या delete।

Trigger निम्नलिखित actions पर चलाया जा सकता है:

  • INSERT
  • UPDATE
  • DELETE

Syntax:

CREATE OR REPLACE TRIGGER trigger_name
BEFORE | AFTER INSERT | UPDATE | DELETE
ON table_name
FOR EACH ROW
BEGIN
   -- Trigger logic here
END;

उदाहरण:

CREATE OR REPLACE TRIGGER trg_before_insert
BEFORE INSERT ON student
FOR EACH ROW
BEGIN
   DBMS_OUTPUT.PUT_LINE('नया student record insert किया जा रहा है');
END;

Types of Trigger in PL/SQL:

There are mainly two categories of triggers —
1. Based on Time of Execution
2. Based on Level of Triggering


1. Based on Time of Execution:
Triggers can execute before or after an event occurs.

  • BEFORE Trigger:
    Executes before the event (INSERT, UPDATE, DELETE) happens.
    Used to check or modify values before saving into the table.
    Example:
  CREATE OR REPLACE TRIGGER before_insert_student
  BEFORE INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('Before inserting record');
  END;
  • AFTER Trigger:
    Executes after the event (INSERT, UPDATE, DELETE) has occurred.
    Used for logging or updating other tables.
    Example:
  CREATE OR REPLACE TRIGGER after_insert_student
  AFTER INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('After inserting record');
  END;

2. Based on Level of Triggering:

  • Row-Level Trigger:
    Executes for each row affected by the triggering event.
    Uses FOR EACH ROW keyword.
    Example:
  CREATE OR REPLACE TRIGGER trg_row
  BEFORE INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('Row level trigger fired');
  END;
  • Statement-Level Trigger:
    Executes once for the entire statement, regardless of the number of rows affected.
    Example:
  CREATE OR REPLACE TRIGGER trg_statement
  AFTER UPDATE ON student
  BEGIN
     DBMS_OUTPUT.PUT_LINE('Statement level trigger fired');
  END;

PL/SQL में Trigger के प्रकार:

मुख्यतः दो प्रकार के Trigger होते हैं —
1. Execution Time के आधार पर
2. Level के आधार पर


1. Execution Time के आधार पर:
ये इस बात पर निर्भर करता है कि Trigger कब चलेगा।

  • BEFORE Trigger:
    यह event से पहले चलता है (INSERT, UPDATE, DELETE)।
    इसका उपयोग डेटा चेक करने या बदलने के लिए किया जाता है।
    उदाहरण:
  CREATE OR REPLACE TRIGGER before_insert_student
  BEFORE INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('रिकॉर्ड insert होने से पहले');
  END;
  • AFTER Trigger:
    यह event के बाद चलता है।
    इसका उपयोग लॉगिंग या दूसरी टेबल अपडेट करने के लिए किया जाता है।
    उदाहरण:
  CREATE OR REPLACE TRIGGER after_insert_student
  AFTER INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('रिकॉर्ड insert होने के बाद');
  END;

2. Level के आधार पर:

  • Row-Level Trigger:
    यह हर एक रिकॉर्ड के लिए चलता है जिस पर action लिया गया हो।
    इसमें FOR EACH ROW लिखा जाता है।
    उदाहरण:
  CREATE OR REPLACE TRIGGER trg_row
  BEFORE INSERT ON student
  FOR EACH ROW
  BEGIN
     DBMS_OUTPUT.PUT_LINE('Row level trigger चला');
  END;
  • Statement-Level Trigger:
    यह पूरे SQL statement के लिए एक बार चलता है, चाहे कितनी भी rows प्रभावित हों।
    उदाहरण:
  CREATE OR REPLACE TRIGGER trg_statement
  AFTER UPDATE ON student
  BEGIN
     DBMS_OUTPUT.PUT_LINE('Statement level trigger चला');
  END;

Q. What do you mean by Cursor and Types of Cursor?

In English

A Cursor in PL/SQL is a temporary work area created in memory when a SQL statement is executed. It is used to store and process the result set of a query row by row. Cursors help in retrieving, updating, and deleting data one record at a time.

There are two types of cursors in PL/SQL:

1. Implicit Cursor:

  • Created automatically by Oracle when a DML statement (like INSERT, UPDATE, DELETE, or SELECT INTO) is executed.
  • No need to declare it explicitly.
  • Example:
  BEGIN
     INSERT INTO student VALUES (1, 'Rahul');
  END;

Here Oracle automatically creates an implicit cursor for the INSERT statement.

2. Explicit Cursor:

  • Declared by the programmer for queries that return more than one row.
  • Gives more control over fetching records.
  • Steps: Declare → Open → Fetch → Close.
  • Example:
  DECLARE
     CURSOR c1 IS SELECT name FROM student;
     v_name student.name%TYPE;
  BEGIN
     OPEN c1;
     LOOP
        FETCH c1 INTO v_name;
        EXIT WHEN c1%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(v_name);
     END LOOP;
     CLOSE c1;
  END;

हिन्दी में

Cursor PL/SQL में एक अस्थायी कार्य क्षेत्र (temporary work area) होता है जो तब बनता है जब कोई SQL statement चलाया जाता है। यह query के result set को row by row process करने में मदद करता है। Cursor का उपयोग डेटा को retrieve, update और delete करने के लिए किया जाता है।

PL/SQL में Cursor के दो प्रकार होते हैं:

1. Implicit Cursor (आंतरिक कर्सर):

  • Oracle इसे स्वतः (automatically) बनाता है जब कोई DML statement (INSERT, UPDATE, DELETE, या SELECT INTO) चलाया जाता है।
  • इसे अलग से declare करने की जरूरत नहीं होती।
  • उदाहरण:
  BEGIN
     INSERT INTO student VALUES (1, 'Rahul');
  END;

यहाँ Oracle अपने आप एक implicit cursor बनाता है।

2. Explicit Cursor (स्पष्ट कर्सर):

  • इसे प्रोग्रामर स्वयं declare करता है जब query एक से अधिक पंक्तियाँ लौटाती है।
  • इससे records को एक-एक करके fetch किया जा सकता है।
  • Steps: Declare → Open → Fetch → Close
  • उदाहरण:
  DECLARE
     CURSOR c1 IS SELECT name FROM student;
     v_name student.name%TYPE;
  BEGIN
     OPEN c1;
     LOOP
        FETCH c1 INTO v_name;
        EXIT WHEN c1%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(v_name);
     END LOOP;
     CLOSE c1;
  END;

Q. Difference between Trigger and Cursor

BasisTriggerCursor
MeaningA Trigger is a stored program that automatically executes in response to a specific event on a table or view.A Cursor is a temporary work area used to retrieve and process query results row by row.
ExecutionIt executes automatically when an event (INSERT, UPDATE, DELETE) occurs.It executes manually by the programmer using open, fetch, and close commands.
PurposeUsed to automate tasks such as maintaining logs, enforcing rules, or updating related tables.Used to process multiple rows returned by a query one by one.
CreationCreated using the CREATE TRIGGER statement.Created using the DECLARE CURSOR statement.
DependencyWorks based on database events.Works based on SQL queries.
ControlLess control — executes automatically.Full control — programmer defines when and how to fetch data.
ExampleCREATE TRIGGER before_insert ON student BEFORE INSERTDECLARE CURSOR c1 IS SELECT * FROM student;

हिन्दी में

आधारट्रिगर (Trigger)कर्सर (Cursor)
अर्थTrigger एक stored program है जो किसी टेबल या व्यू पर विशेष घटना (event) होने पर अपने आप चलता है।Cursor एक अस्थायी कार्य क्षेत्र (temporary work area) है जो query के परिणाम को एक-एक पंक्ति में process करता है।
निष्पादन (Execution)यह स्वतः (automatically) चलता है जब कोई event (INSERT, UPDATE, DELETE) होता है।यह मैन्युअली (manually) चलता है — प्रोग्रामर इसे open, fetch और close करता है।
उद्देश्य (Purpose)लॉग बनाए रखना, नियम लागू करना, या संबंधित टेबल अपडेट करने जैसे कार्यों को स्वचालित (automate) करने के लिए।Query से लौटे डेटा को row by row process करने के लिए।
निर्माण (Creation)CREATE TRIGGER कमांड से बनाया जाता है।DECLARE CURSOR कमांड से बनाया जाता है।
निर्भरता (Dependency)यह database events पर निर्भर करता है।यह SQL queries पर निर्भर करता है।
नियंत्रण (Control)कम नियंत्रण — अपने आप चलता है।पूर्ण नियंत्रण — प्रोग्रामर तय करता है कब और कैसे डेटा लाना है।
उदाहरण (Example)CREATE TRIGGER before_insert ON student BEFORE INSERTDECLARE CURSOR c1 IS SELECT * FROM student;

Q. Explain following attributes: %FOUND, %NOTFOUND, %ISOPEN, %ROWCOUNT

In English

These attributes are used in PL/SQL cursors to check the status of a cursor during execution. They help in controlling the flow of data while fetching rows from a table.

%FOUND

  • Returns TRUE if the last fetch operation was successful.
  • Returns FALSE if no row was returned.
  • Example:
  IF c1%FOUND THEN
     DBMS_OUTPUT.PUT_LINE('Record Found');
  END IF;

%NOTFOUND

  • Returns TRUE if the last fetch did not return any row.
  • It is opposite of %FOUND.
  • Example:
  IF c1%NOTFOUND THEN
     EXIT;
  END IF;

%ISOPEN

  • Returns TRUE if the cursor is open.
  • Returns FALSE if the cursor is closed.
  • Example:
  IF NOT c1%ISOPEN THEN
     OPEN c1;
  END IF;

%ROWCOUNT

  • Returns the number of rows fetched so far.
  • Example:
  DBMS_OUTPUT.PUT_LINE('Rows fetched: ' || c1%ROWCOUNT);

हिन्दी में

ये attributes PL/SQL Cursor के साथ उपयोग किए जाते हैं ताकि cursor की स्थिति (status) जानी जा सके। ये यह जांचने में मदद करते हैं कि डेटा सही से fetch हुआ या नहीं।

%FOUND

  • अगर पिछला fetch सफल रहा तो TRUE लौटाता है।
  • अगर कोई row नहीं मिली तो FALSE लौटाता है।
  • उदाहरण:
  IF c1%FOUND THEN
     DBMS_OUTPUT.PUT_LINE('रिकॉर्ड मिला');
  END IF;

%NOTFOUND

  • अगर पिछला fetch असफल रहा तो TRUE लौटाता है।
  • यह %FOUND का उल्टा होता है।
  • उदाहरण:
  IF c1%NOTFOUND THEN
     EXIT;
  END IF;

%ISOPEN

  • अगर cursor खुला है तो TRUE लौटाता है।
  • अगर cursor बंद है तो FALSE लौटाता है।
  • उदाहरण:
  IF NOT c1%ISOPEN THEN
     OPEN c1;
  END IF;

%ROWCOUNT

  • अब तक fetch की गई rows की संख्या लौटाता है।
  • उदाहरण:
  DBMS_OUTPUT.PUT_LINE('कुल rows: ' || c1%ROWCOUNT);

Q. What is Data Types in PL/SQL and its Types?

In English

In PL/SQL, a data type defines the type of data a variable can store such as number, text, or date. It helps the compiler to understand how much memory to allocate and what kind of operations can be performed on that data.

There are mainly four types of data types in PL/SQL:

  1. Scalar Data Types
  • Hold a single value.
  • Examples:
    • NUMBER – stores numeric values.
    • CHAR, VARCHAR2 – store character strings.
    • DATE – stores date and time values.
    • BOOLEAN – stores TRUE/FALSE values.
  1. Composite Data Types
  • Hold multiple values of same or different data types.
  • Examples:
    • RECORD
    • TABLE
  1. Reference Data Types
  • Used to store references (addresses) of other data.
  • Example: REF CURSOR
  1. Large Object (LOB) Data Types
  • Used to store large data such as images, videos, or long text.
  • Examples: BLOB, CLOB, NCLOB, BFILE

हिन्दी में

PL/SQL में Data Type यह बताता है कि कोई variable किस प्रकार का डेटा रख सकता है जैसे संख्या, अक्षर या तारीख। यह compiler को यह समझने में मदद करता है कि कितनी memory देनी है और कौन से operations लागू हो सकते हैं।

मुख्य रूप से चार प्रकार के Data Types होते हैं:

  1. Scalar Data Types
  • एक ही मान रखते हैं।
  • उदाहरण:
    • NUMBER – संख्यात्मक मान रखता है।
    • CHAR, VARCHAR2 – अक्षर या शब्द रखते हैं।
    • DATE – तारीख और समय रखता है।
    • BOOLEAN – TRUE/FALSE मान रखता है।
  1. Composite Data Types
  • कई मान (values) रखते हैं, समान या भिन्न प्रकार के।
  • उदाहरण:
    • RECORD
    • TABLE
  1. Reference Data Types
  • किसी अन्य डेटा का reference (address) रखते हैं।
  • उदाहरण: REF CURSOR
  1. Large Object (LOB) Data Types
  • बड़े आकार के डेटा जैसे image, video या लंबा text रखने के लिए।
  • उदाहरण: BLOB, CLOB, NCLOB, BFILE

Q. Difference between BEFORE and AFTER Trigger

In English

BEFORE TriggerAFTER Trigger
Executes before the triggering event (INSERT, UPDATE, DELETE).Executes after the triggering event (INSERT, UPDATE, DELETE).
Used to check or modify data before it is written to the table.Used to perform actions after the data has been changed in the table.
Mostly used for validation and data modification.Mostly used for logging or audit trail purposes.
Can prevent invalid data from being inserted.Cannot stop the execution once the action has been performed.
Example: To ensure salary is not less than minimum wage before inserting.Example: To record user activity after data insertion.
Syntax: CREATE OR REPLACE TRIGGER trg_name BEFORE INSERT ON table_name...Syntax: CREATE OR REPLACE TRIGGER trg_name AFTER INSERT ON table_name...
Executes prior to data change.Executes immediately after data change.
Commonly used to enforce business rules.Commonly used to generate reports or logs.

हिन्दी में

BEFORE TriggerAFTER Trigger
यह मुख्य घटना (INSERT, UPDATE, DELETE) से पहले चलता है।यह मुख्य घटना के बाद चलता है।
डेटा को table में लिखने से पहले जांचने या बदलने के लिए उपयोग किया जाता है।डेटा बदलने के बाद कुछ कार्य करने के लिए उपयोग किया जाता है।
अधिकतर data validation और modification के लिए उपयोग होता है।अधिकतर log या record रखने के लिए उपयोग होता है।
यह गलत डेटा को रोक सकता हैएक बार data change होने के बाद इसे रोका नहीं जा सकता।
उदाहरण: Insert से पहले salary को minimum wage से compare करना।उदाहरण: Insert के बाद user activity को log करना।
Syntax: CREATE OR REPLACE TRIGGER trg_name BEFORE INSERT ON table_name...Syntax: CREATE OR REPLACE TRIGGER trg_name AFTER INSERT ON table_name...
यह data change से पहले execute होता है।यह data change के तुरंत बाद execute होता है।
मुख्य रूप से business rules लागू करने के लिए।मुख्य रूप से report या log बनाने के लिए।

Q. Write a program using PL/SQL code to generate the following series up to 60:

1, 5, 9, 13, …

In English

Program:

DECLARE
   num NUMBER := 1;
BEGIN
   WHILE num <= 60 LOOP
      DBMS_OUTPUT.PUT_LINE(num);
      num := num + 4;
   END LOOP;
END;
/

Explanation:

  • The variable num starts with 1.
  • The WHILE loop continues until the value reaches 60.
  • Each time, 4 is added to generate the next number in the series (1, 5, 9, 13, …).
  • The DBMS_OUTPUT.PUT_LINE command displays each number.

हिन्दी में

प्रोग्राम:

DECLARE
   num NUMBER := 1;
BEGIN
   WHILE num <= 60 LOOP
      DBMS_OUTPUT.PUT_LINE(num);
      num := num + 4;
   END LOOP;
END;
/

व्याख्या:

  • Variable num की प्रारंभिक मान 1 है।
  • WHILE loop तब तक चलता है जब तक मान 60 से कम या बराबर रहता है।
  • हर बार 4 जोड़ा जाता है ताकि अगला नंबर मिले (1, 5, 9, 13, …)।
  • DBMS_OUTPUT.PUT_LINE प्रत्येक संख्या को प्रदर्शित करता है।

Q. Write a PL/SQL code to find the area of a rectangle


🟩 In English

Program:

DECLARE
   length NUMBER := 10;  -- you can change the value
   breadth NUMBER := 5;  -- you can change the value
   area NUMBER;
BEGIN
   area := length * breadth;
   DBMS_OUTPUT.PUT_LINE('Area of Rectangle = ' || area);
END;
/

Explanation:

  • length and breadth are declared as numeric variables.
  • The area formula is:
    👉 Area = length × breadth
  • The result is displayed using DBMS_OUTPUT.PUT_LINE.

Output:

Area of Rectangle = 50

🟦 हिन्दी में

प्रोग्राम:

DECLARE
   length NUMBER := 10;   -- आप चाहें तो मान बदल सकते हैं
   breadth NUMBER := 5;   -- आप चाहें तो मान बदल सकते हैं
   area NUMBER;
BEGIN
   area := length * breadth;
   DBMS_OUTPUT.PUT_LINE('Area of Rectangle = ' || area);
END;
/

व्याख्या:

  • length और breadth को नंबर टाइप के रूप में घोषित किया गया है।
  • क्षेत्रफल का सूत्र है:
    👉 Area = length × breadth
  • परिणाम को दिखाने के लिए DBMS_OUTPUT.PUT_LINE का प्रयोग किया गया है।

आउटपुट:

Area of Rectangle = 50

Q. Write a PL/SQL code which will accept three numbers and print the largest among them


🟩 In English

Program:

DECLARE
   a NUMBER := 25;
   b NUMBER := 40;
   c NUMBER := 30;
   largest NUMBER;
BEGIN
   IF (a > b) AND (a > c) THEN
      largest := a;
   ELSIF (b > a) AND (b > c) THEN
      largest := b;
   ELSE
      largest := c;
   END IF;

   DBMS_OUTPUT.PUT_LINE('The largest number is: ' || largest);
END;
/

Explanation:

  • We declared three variables a, b, and c.
  • Using IF…ELSIF…ELSE, we compared the three numbers.
  • The largest number is stored in the variable largest.
  • Finally, it is displayed using DBMS_OUTPUT.PUT_LINE.

Output:

The largest number is: 40

🟦 हिन्दी में

प्रोग्राम:

DECLARE
   a NUMBER := 25;
   b NUMBER := 40;
   c NUMBER := 30;
   largest NUMBER;
BEGIN
   IF (a > b) AND (a > c) THEN
      largest := a;
   ELSIF (b > a) AND (b > c) THEN
      largest := b;
   ELSE
      largest := c;
   END IF;

   DBMS_OUTPUT.PUT_LINE('The largest number is: ' || largest);
END;
/

व्याख्या:

  • तीन नंबर a, b, और c घोषित किए गए हैं।
  • IF…ELSIF…ELSE स्टेटमेंट की मदद से तीनों में तुलना की गई।
  • सबसे बड़ा नंबर largest में रखा गया।
  • अंत में परिणाम DBMS_OUTPUT.PUT_LINE से दिखाया गया।

आउटपुट:

The largest number is: 40

Q. Write a PL/SQL program to generate the following series up to 100:

2, 4, 6, 8, …


🟩 In English

Program:

DECLARE
   i NUMBER := 2;
BEGIN
   WHILE i <= 100 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 2;
   END LOOP;
END;
/

Explanation:

  • Variable i is initialized with 2.
  • WHILE LOOP is used to repeat statements until the value of i becomes greater than 100.
  • Each time, the value of i is printed and then increased by 2.
  • This generates even numbers from 2 to 100.

Output:

2
4
6
8
...
100

🟦 हिन्दी में

प्रोग्राम:

DECLARE
   i NUMBER := 2;
BEGIN
   WHILE i <= 100 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 2;
   END LOOP;
END;
/

व्याख्या:

  • वेरिएबल i की प्रारंभिक वैल्यू 2 रखी गई है।
  • WHILE LOOP तब तक चलता है जब तक i की वैल्यू 100 से कम या बराबर रहती है।
  • हर बार i को प्रिंट किया जाता है और फिर 2 से बढ़ाया जाता है।
  • इस प्रकार यह 2 से 100 तक सभी सम (even) नंबर प्रिंट करता है।

आउटपुट:

2
4
6
8
...
100

🟩 In English

Q. Write a PL/SQL code to find the area of a triangle.

Program:

DECLARE
   base NUMBER;
   height NUMBER;
   area NUMBER;
BEGIN
   base := 10;
   height := 5;

   area := (base * height) / 2;

   DBMS_OUTPUT.PUT_LINE('Area of Triangle = ' || area);
END;
/

Output:

Area of Triangle = 25

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो त्रिभुज का क्षेत्रफल (Area) निकाल सके।

प्रोग्राम:

DECLARE
   base NUMBER;
   height NUMBER;
   area NUMBER;
BEGIN
   base := 10;
   height := 5;

   area := (base * height) / 2;

   DBMS_OUTPUT.PUT_LINE('Area of Triangle = ' || area);
END;
/

आउटपुट:

Area of Triangle = 25

🟩 In English

Q. Write a PL/SQL code to print the series 1, 3, 5, 7, …, 25.

Program:

DECLARE
   i NUMBER := 1;
BEGIN
   WHILE i <= 25 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 2;
   END LOOP;
END;
/

Output:

1
3
5
7
9
11
13
15
17
19
21
23
25

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो 1, 3, 5, 7, …, 25 तक की श्रृंखला (Series) प्रिंट करे।

प्रोग्राम:

DECLARE
   i NUMBER := 1;
BEGIN
   WHILE i <= 25 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 2;
   END LOOP;
END;
/

आउटपुट:

1
3
5
7
9
11
13
15
17
19
21
23
25

🟩 In English

Q. Write a PL/SQL code to read 5 numbers and find their average.

Program:

DECLARE
   n1 NUMBER;
   n2 NUMBER;
   n3 NUMBER;
   n4 NUMBER;
   n5 NUMBER;
   avg NUMBER;
BEGIN
   n1 := &n1;
   n2 := &n2;
   n3 := &n3;
   n4 := &n4;
   n5 := &n5;

   avg := (n1 + n2 + n3 + n4 + n5) / 5;

   DBMS_OUTPUT.PUT_LINE('Average of 5 numbers is: ' || avg);
END;
/

Output Example:

Enter value for n1: 10
Enter value for n2: 20
Enter value for n3: 30
Enter value for n4: 40
Enter value for n5: 50
Average of 5 numbers is: 30

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो 5 संख्याएँ पढ़े और उनका औसत (Average) निकाले।

प्रोग्राम:

DECLARE
   n1 NUMBER;
   n2 NUMBER;
   n3 NUMBER;
   n4 NUMBER;
   n5 NUMBER;
   avg NUMBER;
BEGIN
   n1 := &n1;
   n2 := &n2;
   n3 := &n3;
   n4 := &n4;
   n5 := &n5;

   avg := (n1 + n2 + n3 + n4 + n5) / 5;

   DBMS_OUTPUT.PUT_LINE('पाँच संख्याओं का औसत है: ' || avg);
END;
/

आउटपुट उदाहरण:

Enter value for n1: 10
Enter value for n2: 20
Enter value for n3: 30
Enter value for n4: 40
Enter value for n5: 50
पाँच संख्याओं का औसत है: 30

Q. Write a PL/SQL code to display the following series: 100, 98, 96, ………, 4, 2

Program:

DECLARE
   i NUMBER := 100;
BEGIN
   WHILE i >= 2 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i - 2;
   END LOOP;
END;
/

Output Example:

100
98
96
94
...
4
2

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो निम्नलिखित श्रेणी (Series) दिखाए: 100, 98, 96, ………, 4, 2

प्रोग्राम:

DECLARE
   i NUMBER := 100;
BEGIN
   WHILE i >= 2 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i - 2;
   END LOOP;
END;
/

आउटपुट उदाहरण:

100
98
96
94
...
4
2

🟩 Q. Write a PL/SQL code which accepts three numbers and prints the smallest among them.

Program:

DECLARE
   a NUMBER;
   b NUMBER;
   c NUMBER;
   small NUMBER;
BEGIN
   a := &a;
   b := &b;
   c := &c;

   IF (a < b) AND (a < c) THEN
      small := a;
   ELSIF (b < a) AND (b < c) THEN
      small := b;
   ELSE
      small := c;
   END IF;

   DBMS_OUTPUT.PUT_LINE('Smallest number is: ' || small);
END;
/

Output Example:

Enter value for a: 10  
Enter value for b: 5  
Enter value for c: 8  
Smallest number is: 5

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो तीन संख्याएँ स्वीकार करे और उनमें से सबसे छोटी संख्या प्रदर्शित करे।

प्रोग्राम:

DECLARE
   a NUMBER;
   b NUMBER;
   c NUMBER;
   small NUMBER;
BEGIN
   a := &a;
   b := &b;
   c := &c;

   IF (a < b) AND (a < c) THEN
      small := a;
   ELSIF (b < a) AND (b < c) THEN
      small := b;
   ELSE
      small := c;
   END IF;

   DBMS_OUTPUT.PUT_LINE('Smallest number is: ' || small);
END;
/

आउटपुट उदाहरण:

Enter value for a: 10  
Enter value for b: 5  
Enter value for c: 8  
Smallest number is: 5

🟩 Write a PL/SQL code to determine whether the given number is odd or even.

Program:

DECLARE
   num NUMBER;
BEGIN
   num := &num;

   IF MOD(num, 2) = 0 THEN
      DBMS_OUTPUT.PUT_LINE(num || ' is Even');
   ELSE
      DBMS_OUTPUT.PUT_LINE(num || ' is Odd');
   END IF;
END;
/

Output Example:

Enter value for num: 7  
7 is Odd

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो यह निर्धारित करे कि दी गई संख्या विषम (Odd) है या सम (Even)।

प्रोग्राम:

DECLARE
   num NUMBER;
BEGIN
   num := &num;

   IF MOD(num, 2) = 0 THEN
      DBMS_OUTPUT.PUT_LINE(num || ' is Even');
   ELSE
      DBMS_OUTPUT.PUT_LINE(num || ' is Odd');
   END IF;
END;
/

आउटपुट उदाहरण:

Enter value for num: 7  
7 is Odd

🟩 Write a PL/SQL code to print series from 1 to 50.

Program:

DECLARE
   i NUMBER := 1;
BEGIN
   WHILE i <= 50 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 1;
   END LOOP;
END;
/

Output Example:

1  
2  
3  
...  
50

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो 1 से 50 तक की श्रृंखला (series) प्रदर्शित करे।

प्रोग्राम:

DECLARE
   i NUMBER := 1;
BEGIN
   WHILE i <= 50 LOOP
      DBMS_OUTPUT.PUT_LINE(i);
      i := i + 1;
   END LOOP;
END;
/

आउटपुट उदाहरण:

1  
2  
3  
...  
50

🟩 Q. Write a PL/SQL code to find the area of a rectangle.

Program:

DECLARE
   length NUMBER := 10;
   breadth NUMBER := 5;
   area NUMBER;
BEGIN
   area := length * breadth;
   DBMS_OUTPUT.PUT_LINE('Area of Rectangle = ' || area);
END;
/

Output Example:

Area of Rectangle = 50

🟦 हिन्दी में

प्र. एक PL/SQL प्रोग्राम लिखिए जो आयत (Rectangle) का क्षेत्रफल ज्ञात करे।

प्रोग्राम:

DECLARE
   length NUMBER := 10;
   breadth NUMBER := 5;
   area NUMBER;
BEGIN
   area := length * breadth;
   DBMS_OUTPUT.PUT_LINE('Area of Rectangle = ' || area);
END;
/

आउटपुट उदाहरण:

Area of Rectangle = 50

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 *