Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

OPEN SQL

Sap R/3 is independent of database being used, i.e. sap can use any database to store the data.

SQL (STRUCTURED QUERY LANGUAGE):

We use databases to store mass amount of data.

Whenever we want to store the data into database, we need to use the respective database language
statements.

ORACLE database understands ORACLE statements.

SQL database understands SQL SERVER statements.

DB2 database understands DB2 SERVER statements.

R/3 INTERFACE LAYER

It is an interface which is given by standard SAP software which is used to convert the abap language
statements into respective database statements and vice versa.

There are two types of statements

1.OPEN SQL STATEMENTS.

2.NATIVE SQL STATEMENTS.

OPEN SQL STATEMENTS:

The statement which is converted by R/3 interface layer from abap to respective database statement
and viceversa are called open sql statements.

In real time we will work only on open sql statements.

NATIVE SQL STATEMENTS:

These statements directly communicate with the database without interacting with R3 interface layer
are called native sql statements.
SYNTAX OF OPEN SQL STATEMENTS

INSERT : INSERT <DB TABLE> FROM <WA>.

INSERT <DB TABLE> FROM TABLE <ITAB>.

MODIFY : MODIFY <DB TABLE> FROM <WA>.

MODIFY <DB TABLE> FROM TABLE <ITAB>.

UPDATE: UPDATE <DB TABLE> FROM <WA>.

UPDATE <DB TABLE> FROM TABLE <ITAB>.

DELETE: DELETE <DB TABLE> FROM <WA>.

DELETE <DB TABLE> FROM TABLE <ITAB>.

PRE-REQUISITIES FOR OPEN SQL STATEMENTS

1. The structure of internal table/work area must be same as database table.

Ex: Data: i_kna1 type table of kna1.(correct)

Data: i_kna1 type table of ty_kna1.(incorrect)

2. Always work with internal tables rather than work areas to insert /update/modify data in database
table

SYSTEM VARIABLES: There are two system variables which are updated automatically for every open sql
operation.

1) SY-SUBRC

It is a system variable which stores the return code of an open sql

operation. If SY-SUBRC = 0.

It means the operation is successfully executed.

If SY-SUBRC NE 0.

It means the operation failed.

2) SY-DBCNT

It is a system variable which stores the no. of records successfully processed.


Examples on OPEN SQL statements

INSERT: It is used to insert single record or multiple records from internal table to database table.

SY-SUBRC = 0 ,means all records were successfully inserted.

SY-SUBRC NE 0 ,means only few records are inserted or no records are inserted.

There are two forms of insert statements.

Insert <DB.TABLE> from table <itab>

Insert <DB.TABLE from table accepting duplicate keys.

INSERT FROM TABLE

This statement will insert the records into database tables.

Suppose if there are any duplicate records in the internal table it will throw an run time error.

INSERT FROM TABLE ACCEPTING DUPLICATE KEYS

This statement does not insert any duplicate records, but it avoids runtime error.

UPDATE

This statement will update the record which is available in database table.

There are two forms for update statement.

Row level update: Entire row is updated.

Update <db table> from <wa>

Or

update<db table> from table <itab>

Column level update: only particular field is updated.

Update <db table> set <f1> = <val>

<f2>= <val>

Where <condition>.

MODIFY: THIS STATEMENT IS USED TO MODIFY A RECORD .

DELETE: this statement is used to delete either a single record or multiple records from database tables.
EXAMPLES ON INSERT,MODIFY,UPDATE,DELETE

BUSINESS REQUIREMENT:

INSERT YHE FOLLOWING PLANTS DATA INTO SAP DATABASE TABLES.

TABLE NAME: T001W.

FIELDNAMES:WERKS – PLANT CODE

NAME1 – PLANT NAME

Data : I_T001W TYPE TABLE OF T001W.

Data : WA_T001W TYPE T001W.

INSERT

WA_T001W-WERKS = ‘HYD’.

WA_T001W-NAME1 = ‘HYD PLANT’.

APPEND WA_T001W TO I_T001W.

WA_T001W-WERKS = ‘HYD1’.

WA_T001W-NAME1 = ‘HYD PLANT1’.

APPEND WA_T001W TO I_T001W.

INSERT T001W FROM TABLE I_T001W ACCEPTING DUPLICATE KEYS.

IF SY-SUBRC EQ 0.

WRITE : / ‘PLANTS ARE SUCCESSFULLY ENTERED’,SY-DBCNT.

ENDIF.

UPDATE:

WA_T001W-WERKS = ‘HYD’.

WA_T001W-NAME1 = ‘HYDERABAD PLANT’.

APPEND WA_T001W TO I_T001W.

WA_T001W-WERKS = ‘HYD1’.

WA_T001W-NAME1 = ‘HYDERABAD PLANT1’.


APPEND WA_T001W TO I_T001W.

UPDATE T001W FROM TABLE I_T001W.

IF SY-SUBRC = 0

WRITE : / ‘PLANTSARE SUCCESSFULLY UPDATED’,SY-DBCNT.

ENDIF.

(OR)

Updating Single Column

UPDATE T001W SET BWKEY = ‘01’

WHERE WERKS = ‘HYD1’.

IF SY-SUBRC = 0

WRITE : / ‘PLANT IS SUCCESSFULLY UPDATED’,SY-DBCNT.

ENDIF.

MODIFY:

WA_T001W-WERKS = ‘HYD3’.

WA_T001W-NAME1 = ‘HYD PLANT3’.

APPEND WA_T001W TO I_T001W.

WA_T001W-WERKS = ‘HYD4’.

WA_T001W-NAME1 = ‘HYD PLANT4’.

APPEND WA_T001W TO I_T001W.

MODIFY T001W FROM TABLE I_T001W.

IF SY-SUBRC = 0

WRITE : / ‘PLANT ARE SUCCESSFULLY MODIFIED/INSERTED’,SY-DBCNT.

ENDIF.
DELETE:

WA_T001W-WERKS = ‘HYD3’.

WA_T001W-NAME1 = ‘HYD PLANT3’.

APPEND WA_T001W TO I_T001W.

WA_T001W-WERKS = ‘HYD4’.

WA_T001W-NAME1 = ‘HYD PLANT4’.

APPEND WA_T001W TO I_T001W.

DELETE T001W FROM TABLE I_T001W.

IF SY-SUBRC = 0

WRITE : / ‘PLANTSARE SUCCESSFULLY DELETED’,SY-DBCNT.

ENDIF.

You might also like