4 native and open sql in abap

Upload: raza887

Post on 03-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 4 Native and Open SQL in ABAP

    1/4

    3/18/13 Native and Open SQL in ABAP

    www.saptraininghub.com/native-open-sql/

    Sap Training Hub

    Free SAP Training & Tutorials

    What is SAP?

    ABAP

    HR

    FICO

    SD

    Video

    Payroll

    Quiz

    Java

    Testing

    Answers

    Native and Open SQL in ABAP

    The goal of this tutorial is not to teach you SQL or database concepts but to introduce you to the SQL diversity in

    ABAP

    In ABAP/4 programming language, there are two types of SQL being used.

    1. NATIVE SQL

    2. OPEN SQL.

    Open SQL allows you to access the database tables declared in the ABAP dictionary regardless of the database

    platform that the R/3 system is using.

    Native SQL allows you to use database-specific SQL statements in an ABAP/4 program. This means that you can use

    Create a Mobile Site

    www.GinWiz.com

    Convert Your Website to Mobile In Under 2

    Minutes. Start Now!

    http://www.saptraininghub.com/http://www.google.com/url?ct=abg&q=https://www.google.com/adsense/support/bin/request.py%3Fcontact%3Dabg_afc%26url%3Dhttp://www.saptraininghub.com/native-open-sql/%26gl%3DPK%26hl%3Den%26client%3Dca-pub-6330153051175486%26ai0%3DC-ouuXJpGUd3HO5CjigbN0IGICcfdnpMD1_Oj61C4t4PCQhABIJvM7yNQjbeV1AVgywSgAeGFweEDyAEBqAMByAPTBKoEhgFP0H_Ul_ndhexPw7KJWAbF8ulBi35hSlR8EIeiDrau669ljNelJ18uyt55iz1IMef-90qXPLmiFJ7hz9mg4uDynCP_t_ndw98neZZ1beHAUgV0g8IqZKoHvVS_97wFBctxUq3KPmu0N2MHOJagUpA97rw1zaUvtXZv8H7JNPdI-SriOfJv_ogGAYAHh_q-Hg&usg=AFQjCNFKvhSs1DN4baGoRl8O_4ZiEjW5Ewhttp://www.googleadservices.com/pagead/aclk?sa=L&ai=C-ouuXJpGUd3HO5CjigbN0IGICcfdnpMD1_Oj61C4t4PCQhABIJvM7yNQjbeV1AVgywSgAeGFweEDyAEBqAMByAPTBKoEhgFP0H_Ul_ndhexPw7KJWAbF8ulBi35hSlR8EIeiDrau669ljNelJ18uyt55iz1IMef-90qXPLmiFJ7hz9mg4uDynCP_t_ndw98neZZ1beHAUgV0g8IqZKoHvVS_97wFBctxUq3KPmu0N2MHOJagUpA97rw1zaUvtXZv8H7JNPdI-SriOfJv_ogGAYAHh_q-Hg&num=1&cid=5Ggf_x_KgparNeZFRjBt_jxR&sig=AOD64_2lpbquzDwyZzwqQKh7951eiLq9xQ&client=ca-pub-6330153051175486&adurl=http://www.GinWiz.com&nm=5http://www.83answers.com/http://www.guru99.com/http://www.javatutorialhub.com/http://www.saptraininghub.com/quiz/http://www.saptraininghub.com/sap-payroll/http://www.saptraininghub.com/sapvideos/http://www.saptraininghub.com/free-sap-sd-training-course/http://www.saptraininghub.com/sap-fico-training-tutorials/http://www.saptraininghub.com/sap-hcm/http://www.saptraininghub.com/abap-tutorial/http://www.saptraininghub.com/what-is-sap/http://feeds.feedburner.com/SapTrainingHubhttp://www.facebook.com/pages/Sap-Training-Hub/139632216092864http://twitter.com/SAPTHubhttp://www.saptraininghub.com/
  • 7/29/2019 4 Native and Open SQL in ABAP

    2/4

    3/18/13 Native and Open SQL in ABAP

    www.saptraininghub.com/native-open-sql/

    database tables that are not administered by ABAP dictionary, and therefore integrate data that is not part of the R/3

    system.

    Open SQL consists of a set of ABAP statements that perform operations on the central database in the R/3 system.

    The results of the operations and any error messages are independent of the database system in use. Open SQL thus

    provides a uniform syntax and semantics for all of the database systems supported by SAP. ABAP programs that only

    use Open SQL statements will work in any R/3 system, regardless of the database system in use. Open SQL statements

    can only work with database tables that have been been created in the ABAP dictionary.

    Basic Open SQL Commands

    SELECT

    INSERT

    UPDATE

    MODIFY

    DELETE

    OPEN CURSOR,FETCH, CLOSE CURSOR

    Example

    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.

  • 7/29/2019 4 Native and Open SQL in ABAP

    3/4

    3/18/13 Native and Open SQL in ABAP

    www.saptraininghub.com/native-open-sql/

    ENDDO.

    Output the passenger list for the Lufthansa flight 0400 on 28-02.1995:

    Open SQL Return Codes

    All Open SQL statements fill the following two system fields with return codes.

    SY-SUBRC

    After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the operation was successful, a

    value other than 0 if not.

    SY-DBCNT

    After an Open SQL statement, the system field SY-DBCNT contains the number of database lines processed.

    Native SQL

    As already mentioned, Native SQL allows you to use database-specific SQL statements in an ABAP program.

    To use Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the ENDEXEC

    statement.

    Syntax

    EXEC SQL [PERFORMING ].

    ENDEXEC.

    There is no period after Native SQL statements. Furthermore, using inverted commas () or an asterisk (*) at the

    beginning of a line in a native SQL statement does not introduce a comment as it would in normal ABAP syntax. You

    need to know whether table and field names are case-sensitive in your chosen database.

    In Native SQL statements, the data is transported between the database table and the ABAP program using host

    variables. These are declared in the ABAP program, and preceded in the Native SQL statement by a colon (:). You

    can use elementary structures as host variables. Exceptionally, structures in an INTO clause are treated as though all oftheir fields were listed individually.

    As in Open SQL, after the ENDEXEC statement, SY-DBCNT contains the number of lines processed. In nearly all

    cases, SY-SUBRC contains the value 0 after the ENDEXEC statement.

    Open SQL Performance Rules

    To improve the performance of the SQL and in turn of the ABAP program, one should take care of the following rules-

  • 7/29/2019 4 Native and Open SQL in ABAP

    4/4

    3/18/13 Native and Open SQL in ABAP

    www.saptraininghub.com/native-open-sql/

    Keep the Result Set Small

    Using the where clause

    If only one record is required from the database, use SELECT SINGLE whenever possible .

    Minimize the Amount of Data Transferred

    Restrict the number of lines

    If only certain fields are required from a table, use the SELECT INTO statementRestrict no of columns

    Use aggregate functions

    Minimize the Number of Data Transfers

    Avoid nested select loops

    An alternative option is to use the SELECT .. FOR ALL ENTRIES statement. This statement can often be a lot

    more efficient than performing a large number of SELECT or SELECT SINGLE statements during a LOOP of

    an internal table.

    Use dictionary viewsUse Joins in the FROM clause

    Use subqueries in the where clause

    Minimize the Search Overhead

    Use index fields in the where clause

    When accessing databases, always ensure that the correct index is being used .

    Reduce the Database Load

    BufferingLogical databases

    Avoid repeated database access

    Using Internal Tables to Buffer Records

    To avoid executing the same SELECT multiple times (and therefore have duplicate selects), an internal table of

    type HASHED can be used to improve performance.

    You might like:

    How to become SAP

    consultant

    All About

    Consignment

    Process

    What is User and

    Customer Exits

    Accounting Key Create Cus tomer

    Number Range for

    Sales and Ass ign to

    Customer Account

    Groups

    http://www.saptraininghub.com/create-customer-number-range-for-sales-and-assign-to-customer-account-groups/http://www.saptraininghub.com/accounting-key/http://www.saptraininghub.com/what-is-user-and-customer-exits/http://www.saptraininghub.com/all-about-consignment-process/http://www.saptraininghub.com/how-to-become-sap-consultant/http://www.saptraininghub.com/create-customer-number-range-for-sales-and-assign-to-customer-account-groups/http://www.saptraininghub.com/accounting-key/http://www.saptraininghub.com/what-is-user-and-customer-exits/http://www.saptraininghub.com/all-about-consignment-process/http://www.saptraininghub.com/how-to-become-sap-consultant/