Introduction to PL/SQL (Definition & Block Structure)

PL/SQL सामान्य SQL को variables, loops, और सुंदर error handlers वाले एक शक्तिशाली programming shell के अंदर लपेटता है ताकि कई steps एक ही बार में execute हों।

11 min read · 11 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक भूली Due Date की क़ीमत

कल्पना कीजिए एक student library counter पर एक ऐसी किताब के साथ आता है जो 15 दिन overdue है। student assistant के तौर पर, आप बस table देखकर एक बेतरतीब fine type नहीं कर सकते। आपको एक छोटा program लिखना होगा: पहले, जाँचें कि due date से कितने दिन बीते हैं; दूसरे, एक fine tier इस्तेमाल करके penalty calculate करें; तीसरे, जाँचें कि क्या student का बुरे व्यवहार का इतिहास है; और आख़िर में, उनका account status update करें।

Standard SQL rows fetch या modify कर सकता है, पर हम इस step-by-step logic को database के अंदर ही कैसे बनाते हैं?

Theory

Automated Billing Counter

सामान्य SQL को एक उत्सुक assistant की तरह सोचिए जो केवल shelves से ख़ास किताबें ला सकता है या command पर उन्हें वापस रख सकता है। यह फ़ैसले नहीं ले सकता या multi-step procedures नहीं चला सकता। PL/SQL उस assistant को एक automated self-checkout kiosk में बदलने जैसा है। kiosk सिर्फ़ barcode नहीं पढ़ता; यह eligibility rules जाँचता है, calculation logic चलाता है, receipts print करता है, और card swipe विफल होने पर errors सँभालता है, सब एक seamless, structured workflow में।

Theory

PL/SQL क्या है?

औपचारिक रूप से, PL/SQL (Procedural Language extension to SQL) Oracle का proprietary extension है जो standard SQL में loops, conditional branching, और variables जैसे programming controls पेश करता है।

queries को network पर database server को एक-एक करके भेजने के बजाय, PL/SQL आपको कई SQL statements और procedural logic को code के एक अकेले block में bundle करने देता है। यह network traffic को नाटकीय रूप से घटाता है, execution speed सुधारता है, और engine के अंदर सीधे मज़बूत security और error handling mechanisms देता है।

At a glance

एक standard PL/SQL block की चार-भाग संरचनात्मक बनावट।

Section BlockKeywordPurposeक्या यह ज़रूरी है?
Declaration BlockDECLAREvariables, constants, और cursors के लिए memory allocate करता है।Optional
Executable BlockBEGINdata process करने के लिए मूल programming logic और SQL statements रखता है।Mandatory
Exception BlockEXCEPTIONruntime errors या database warnings को सुंदर ढंग से पकड़ता और सँभालता है।Optional
Termination MarkerEND;PL/SQL block code structure के समापन का संकेत देता है।Mandatory

Practical

एक पूर्ण CampusLib PL/SQL Block

-- An anonymous block to fetch a member's name and handle missing records
DECLARE
  v_member_name members.name%TYPE;
  v_target_id   members.member_id%TYPE := 101;
BEGIN
  SELECT name INTO v_member_name
  FROM members
  WHERE member_id = v_target_id;
  
  DBMS_OUTPUT.PUT_LINE('Library Member Name: ' || v_member_name);
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Error: Member ID ' || v_target_id || ' does not exist.');
END;
/

Copy करके Oracle FreeSQL खोलें
Oracle FreeSQL, Oracle SQL का मुफ़्त online editor है। Code पहले copy हो जाता है: उसे वहां paste करके Run करें।

Follow along

Execution की बनावट

  1. 1. Allocation DECLARE section %TYPE इस्तेमाल करके एक variable बनाता है, members table column से बिल्कुल data type structure copy करते हुए।
  2. 2. Data Retrieval BEGIN के अंदर, SELECT INTO statement query result को सीधे हमारे declared variable में मजबूर करता है। सामान्य SQL PL/SQL के बाहर INTO इस्तेमाल नहीं कर सकता।
  3. 3. Safe Landing अगर member ID मौजूद नहीं, execution crash से बचने के लिए तुरंत BEGIN section से बाहर कूदता है।
  4. 4. Error Resolution EXCEPTION section NO_DATA_FOUND trigger पकड़ता है, एक साफ़ error message print करता है, और सामान्य रूप से बाहर निकलता है।

Quiz

एक valid anonymous PL/SQL block चलाने के लिए ज़रूरी परम न्यूनतम syntactical structure निम्न में से कौन सी line दर्शाती है?

  1. DECLARE ... END;
  2. BEGIN ... END;
  3. DECLARE ... BEGIN ... END;
  4. BEGIN ... EXCEPTION ... END;
Show the answer

BEGIN ... END;

केवल BEGIN और END; से घिरा executable block ज़रूरी है। DECLARE और EXCEPTION sections पूरी तरह optional हैं और अगर आपको variables या ख़ास error handling loops नहीं चाहिए तो छोड़े जा सकते हैं।

Watch out

घातक SELECT INTO जाल

सामान्य SQL में, एक query जो शून्य rows पाती है बस 'no rows selected' print करती है। PL/SQL में, एक block के अंदर हर एक SELECT statement को बिल्कुल एक row return करनी होगी! अगर आपकी query शून्य rows return करती है, Oracle तुरंत एक NO_DATA_FOUND error के साथ execution रोक देता है। अगर यह कई rows return करती है, यह TOO_MANY_ROWS के साथ crash होती है। आपको हमेशा इन नतीजों को एक स्पष्ट EXCEPTION block इस्तेमाल करके सँभालना होगा या कई records process करने के लिए cursors पर निर्भर रहना होगा।

Think first

Syntax Assignment जाँच

एक PL/SQL block के अंदर इस assignment statement को ध्यान से देखिए: 'v_fine_amount = 50;'। क्या यह Oracle SQL में सफलतापूर्वक compile होगी, या यह एक exam-बर्बाद करने वाली syntax error पैदा करेगी? tap करने से पहले variable assignment syntax को ध्यान से सोचिए।

Show the answer

यह एक सीधी compile error trigger करेगी! PL/SQL में, single equal sign (=) सख़्ती से WHERE clauses या conditionals में एक comparison operator के रूप में इस्तेमाल होता है। एक variable को एक value assign करने के लिए, आपको assignment operator (:=) इस्तेमाल करना होगा। सही statement 'v_fine_amount := 50;' है।

Theory

असली दुनिया के Stored Procedures

Anonymous PL/SQL blocks testing के लिए बेहतरीन हैं, पर Semester 4 या enterprise applications में, आप इस block structure को Procedures, Functions, और Triggers जैसे named database constructs के अंदर लपेटेंगे। Banking systems funds transfers सुरक्षित रूप से सँभालने के लिए इन बिल्कुल blocks का उपयोग करते हैं: एक account से पैसे घटाना और दूसरे में जोड़ना एक अकेले protected execution container के अंदर जो बीच में विफल नहीं हो सकता।

Summary

Key takeaways

  • PL/SQL variables, loops, और conditions जैसे procedural concepts जोड़कर standard SQL को बढ़ाता है।
  • structure में चार blocks होते हैं: DECLARE, BEGIN, EXCEPTION, और END;।
  • BEGIN और END; keywords एकमात्र ज़रूरी operational section बनाते हैं।
  • एक block के अंदर SELECT statements को एक INTO clause चाहिए और बिल्कुल एक row return करनी होगी।
  • Variable assignments को एक साधारण equal sign के बजाय स्पष्ट assignment operator (:=) चाहिए।
  • Memory hook: अपने tools declare करो, काम begin करो, ग़लतियाँ handle करो, और एक end से समेटो!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from PL/SQL and Conditional and Iterative Statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Introduction to PL/SQL (Definition & Block Structure) · Concepts of Relational Database Management Systems · Gri-Learn