ओपन SQL और नेटिव SQL SAP ABAP

⚡ स्मार्ट सारांश

Open SQL and Native SQL in SAP ABAP describe the two ways an ABAP program reaches the database. Open SQL works on dictionary tables in a database independent way, while Native SQL passes database specific statements straight through to the platform.

  • 🧭 Core Distinction: Open SQL addresses tables declared in the ABAP Dictionary, and Native SQL addresses tables that the dictionary does not administer.
  • 🧱 Portability Principle: Open SQL supplies uniform syntax, semantics, and error messages, so a program runs unchanged on every database supported by SAP.
  • Command Set: SELECT, INSERT, UPDATE, MODIFY, DELETE, and the cursor statements cover every Open SQL operation on the central database.
  • 🔢 वापसी Codes: SY-SUBRC reports success with the value 0, and SY-DBCNT reports the number of database lines processed.
  • 🔌 Native SQL Interface: Statements are enclosed between EXEC SQL and ENDEXEC, carry no closing period, and exchange data through host variables preceded by a colon.
  • 🚀 Performance Discipline: Keep the result set small, restrict fields, avoid nested SELECT loops, use index fields, and buffer records in a HASHED internal table.

Open SQL and Native SQL in SAP ABAP

What are Open SQL and Native SQL?

इस ट्यूटोरियल का लक्ष्य आपको यह सिखाना नहीं है एसक्यूएल or database concepts but to introduce you to the SQL diversity in ABAP

ABAP/4 प्रोग्रामिंग भाषा में दो प्रकार के SQL का प्रयोग किया जाता है।

  1. मूल SQL
  2. ओपन एसक्यूएल.

ओपन SQL आपको ABAP शब्दकोश में घोषित डेटाबेस तालिकाओं तक पहुंचने की अनुमति देता है, भले ही R/3 सिस्टम किसी भी डेटाबेस प्लेटफॉर्म का उपयोग कर रहा हो।

नेटिव SQL आपको ABAP/4 प्रोग्राम में डेटाबेस-विशिष्ट SQL कथनों का उपयोग करने की अनुमति देता है। इसका मतलब है कि आप उन डेटाबेस तालिकाओं का उपयोग कर सकते हैं जो ABAP शब्दकोश द्वारा प्रशासित नहीं हैं, और इसलिए ऐसे डेटा को एकीकृत करते हैं जो R/3 सिस्टम का हिस्सा नहीं है।

ओपन SQL में ABAP कथनों का एक सेट होता है जो R/3 सिस्टम में केंद्रीय डेटाबेस पर संचालन करता है। संचालन के परिणाम और कोई भी त्रुटि संदेश उपयोग में आने वाले डेटाबेस सिस्टम से स्वतंत्र होते हैं। इस प्रकार ओपन SQL द्वारा समर्थित सभी डेटाबेस सिस्टम के लिए एक समान वाक्यविन्यास और अर्थविज्ञान प्रदान करता है SAPABAP प्रोग्राम जो केवल ओपन SQL कथनों का उपयोग करते हैं, वे किसी भी R/3 सिस्टम में काम करेंगे, चाहे उपयोग में आने वाला डेटाबेस सिस्टम कोई भी हो। ओपन SQL कथन केवल उन डेटाबेस तालिकाओं के साथ काम कर सकते हैं जिन्हें ABAP शब्दकोश में बनाया गया है।

Open SQL is the default choice in almost every program, so its command set is covered first.

बुनियादी ओपन SQL कमांड

  • चुनते हैं
  • सम्मिलित करें
  • अद्यतन
  • संशोधित
  • हटाएँ
  • कर्सर खोलें, लाएँ, कर्सर बंद करें

उदाहरण

TABLES SBOOK.

DATA C TYPE CURSOR,

WA LIKE SBOOK.

OPEN CURSOR C FOR SELECT * FROM SBOOK WHERE CARRID = 'LH '

AND CONNID = '0400'

AND FLDATE = '19950228'

ORDER BY PRIMARY KEY.

DO.

FETCH NEXT CURSOR C INTO WA.

IF SY-SUBRC <> 0.

CLOSE CURSOR C.

EXIT.

ENDIF.

WRITE: / WA-BOOKID, WA-CUSTOMID, WA-CUSTTYPE,

WA-SMOKER, WA-LUGGWEIGHT, WA-WUNIT,

WA-INVOICE.

ENDDO.

0400-28 को लुफ्थांसा उड़ान 02.1995 के लिए यात्री सूची आउटपुट करें:

Every one of these statements reports its result through two system fields, described next.

ओपन एसक्यूएल रिटर्न Codes

सभी ओपन SQL कथन निम्नलिखित दो सिस्टम फ़ील्ड को रिटर्न कोड से भरते हैं।

System field अर्थ
एसवाई-एसयूबीआरसी प्रत्येक ओपन SQL कथन के बाद, सिस्टम फ़ील्ड SY-SUBRC में मान 0 होता है यदि ऑपरेशन सफल रहा, तथा यदि नहीं तो 0 के अलावा कोई अन्य मान होता है।
एसवाई-डीबीसीएनटी ओपन SQL कथन के बाद, सिस्टम फ़ील्ड SY-DBCNT में संसाधित डेटाबेस लाइनों की संख्या शामिल होती है।

💡टिप: Always check SY-SUBRC immediately after the statement. A later ABAP command overwrites the field, and the original result is lost.

When a table lies outside the ABAP Dictionary, Open SQL cannot reach it, and Native SQL takes over.

देशी एसक्यूएल

जैसा कि पहले ही बताया जा चुका है, Native SQL आपको डेटाबेस-विशिष्ट SQL कथनों का उपयोग करने की अनुमति देता है। एबीएपी कार्यक्रम.

नेटिव SQL कथन का उपयोग करने के लिए, आपको उसके पहले EXEC SQL कथन, तथा उसके बाद ENDEXEC कथन लगाना होगा।

वाक्य - विन्यास

EXEC SQL [PERFORMING <form>].

  <Native SQL statement>

ENDEXEC.

नेटिव SQL स्टेटमेंट के बाद कोई अवधि नहीं होती। इसके अलावा, नेटिव SQL स्टेटमेंट में किसी लाइन की शुरुआत में उल्टे अल्पविराम (“) या तारांकन चिह्न (*) का उपयोग करने से कोई टिप्पणी नहीं आती है, जैसा कि सामान्य ABAP सिंटैक्स में होता है। आपको यह जानना होगा कि आपके द्वारा चुने गए टेबल और फ़ील्ड नाम केस-सेंसिटिव हैं या नहीं डेटाबेस.

नेटिव SQL कथनों में, डेटा को होस्ट वैरिएबल का उपयोग करके डेटाबेस टेबल और ABAP प्रोग्राम के बीच ले जाया जाता है। इन्हें ABAP प्रोग्राम में घोषित किया जाता है, और नेटिव SQL कथन में कोलन (:) से पहले रखा जाता है। आप होस्ट वैरिएबल के रूप में प्राथमिक संरचनाओं का उपयोग कर सकते हैं। अपवादस्वरूप, INTO क्लॉज में संरचनाओं को इस तरह से माना जाता है जैसे कि उनके सभी फ़ील्ड अलग-अलग सूचीबद्ध हों।

ओपन SQL की तरह, ENDEXEC कथन के बाद, SY-DBCNT में संसाधित की गई पंक्तियों की संख्या होती है। लगभग सभी मामलों में, SY-SUBRC में ENDEXEC कथन के बाद मान 0 होता है।

⚠️ चेतावनी: Native SQL bypasses the SAP table buffer and the automatic client handling of Open SQL. The client field has to be supplied by the program, and the statement only runs on the database platform it was written for.

Open SQL vs Native SQL: Key Differences

Both interfaces reach the same database, yet they differ in portability, buffering, and the objects they can address. The table below summarises the practical differences.

मापदंड एसक्यूएल खोलें देशी एसक्यूएल
Tables addressed Only tables declared in the ABAP Dictionary Any table of the database, including tables outside the dictionary
सुवाह्यता Runs on every database supported by SAP Tied to the syntax of one database platform
वाक्य - विन्यास ABAP statements with a closing period Native statements enclosed in EXEC SQL … ENDEXEC, with no closing period
आंकड़ा अंतरण Work areas and internal tables through INTO Host variables preceded by a colon
Client handling and buffering Automatic client handling and table buffering No automatic client handling, and the SAP buffer is bypassed
विशिष्ट उपयोग Everyday access to R/3 application data Integration of data that is not part of the R/3 system

Open SQL is therefore the default, and the rules below keep it fast.

ओपन SQL – प्रदर्शन नियम

SQL और ABAP प्रोग्राम के प्रदर्शन को बेहतर बनाने के लिए, निम्नलिखित नियमों का ध्यान रखना चाहिए-

परिणाम सेट छोटा रखें

  • where क्लॉज़ का उपयोग करना
  • यदि डेटाबेस से केवल एक रिकॉर्ड की आवश्यकता है, तो जब भी संभव हो SELECT SINGLE का उपयोग करें।

स्थानांतरित किए जाने वाले डेटा की मात्रा को न्यूनतम करें

  • पंक्तियों की संख्या सीमित करें
  • यदि किसी तालिका में केवल कुछ फ़ील्ड ही आवश्यक हैं, तो SELECT का उपयोग करें INTO… कथन
  • स्तंभों की संख्या सीमित करें
  • समग्र फ़ंक्शन का उपयोग करें

डेटा स्थानांतरण की संख्या न्यूनतम करें

  • नेस्टेड चयन लूप से बचें
  • एक वैकल्पिक विकल्प SELECT .. FOR ALL ENTRIES कथन का उपयोग करना है। यह कथन अक्सर किसी आंतरिक तालिका के LOOP के दौरान बड़ी संख्या में SELECT या SELECT SINGLE कथनों को निष्पादित करने की तुलना में बहुत अधिक कुशल हो सकता है।
  • शब्दकोश दृश्य का उपयोग करें
  • FROM क्लॉज़ में Joins का उपयोग करें
  • where क्लॉज़ में सबक्वेरीज़ का उपयोग करें

खोज ओवरहेड को न्यूनतम करें

  • where क्लॉज़ में इंडेक्स फ़ील्ड का उपयोग करें
  • डेटाबेस तक पहुंचते समय, हमेशा सुनिश्चित करें कि सही इंडेक्स का उपयोग किया जा रहा है।

डेटाबेस लोड कम करें

  • Bufferआईएनजी
  • तार्किक डेटाबेस
  • बार-बार डेटाबेस तक पहुंच से बचें

आंतरिक तालिकाओं का उपयोग करना Buffer अभिलेख

अक्सर पूछे जाने वाले प्रश्न

Not usually. Native SQL skips the SAP table buffer, so a buffered Open SQL read is often quicker. Native SQL only wins when a platform specific feature is needed that Open SQL does not offer.

Yes. Open SQL adds the current client to the WHERE clause of a client dependent table automatically. Native SQL does not, so the program has to supply MANDT itself to avoid reading foreign client data.

A nested loop sends one database call per record of the outer table, which multiplies network round trips. A join, a dictionary view, or SELECT … FOR ALL ENTRIES retrieves the same data in a single call.

Yes. AI assistants in ABAP development tools detect nested SELECT loops, missing WHERE conditions, and unused columns, then propose a rewritten statement. The suggestion still needs a runtime check in transaction ST05 or SAT.

AI scanners flag every EXEC SQL block, explain the platform specific syntax inside it, and draft an equivalent Open SQL statement. A developer confirms the dictionary tables exist and tests the result before transport.

इस पोस्ट को संक्षेप में इस प्रकार लिखें: