Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Native SQL

Former Member
0 Likes
756

Hi All,

How to write native sql.Can anyone explain me about way of writing native sql.

Thanks In Advance

6 REPLIES 6
Read only

Former Member
0 Likes
691

HI,

Native SQL Statements in ABAP Programs

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

EXEC SQL [PERFORMING form].

Native SQL Anweisung

ENDEXEC.

There is no period (.) after Native SQL statements. Furthermore, using inverted commas (“) in a Native SQL statement or an asterisk (*) at the beginning of a line 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 variables 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 of their fields were listed individually.

If the selection in a Native SQL SELECTstatement is a table, you can pass it to ABAP line by line using the PERFORMINGaddition. The program calls a subroutine form for each line read. You can process the data further within the subroutine.

As in Open SQL, sy-dbcnt contains the number of processed lines after the statement ENDEXEC. sy-subrc contains in almost all cases the value 0 after the statement ENDEXEC. Exceptions are cursor operations: after FETCH, sy-subrc is if not more records could be read. This also applies when you read a result set using EXEC SQL PERFORMING.

REPORT demo_native_sql.

DATA: BEGIN OF wa,

connid TYPE spfli-connid,

cityfrom TYPE spfli-cityfrom,

cityto TYPE spfli-cityto,

END OF wa.

DATA c1 TYPE spfli-carrid VALUE 'LH'.

EXEC SQL PERFORMING loop_output.

SELECT connid, cityfrom, cityto

INTO :wa

FROM spfli

WHERE carrid = :c1

ENDEXEC.

FORM loop_output.

WRITE: / wa-connid, wa-cityfrom, wa-cityto.

ENDFORM.

The output is as follows:

The work area wa and the field c1 Native SQL statement SELECT are used. wa is the target area into which the selected data is written. As a structure, wa is handled in the INTO clause as if all subfields were listed individually: [..] INTO :wa-connid, :wa-cityfrom, :wa-cityto. c1 is used in the WHERE clause. The subroutine loop_output writes the data from wa to the list.

Reward If Helpfull,

Naresh

Read only

Former Member
0 Likes
691

Native SQL

Open SQL allows you to access database tables declared in the ABAP Dictionary, regardless of the database platform you are using. Native SQL allows you to use database-specific SQL statements in an ABAP program. This means that you can use database tables that are not managed by the ABAP Dictionary, and therefore integrate data that is not part of the SAP Web AS ABAP System.

As a rule, an ABAP program containing database-specific SQL statements will not run under different database systems. If your program will be used on more than one database platform, only use Open SQL statements.

Native SQL Statements in ABAP Programs

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

EXEC SQL [PERFORMING form].

Native SQL Anweisung

ENDEXEC.

There is no period (.) after Native SQL statements. Furthermore, using inverted commas (“) in a Native SQL statement or an asterisk (*) at the beginning of a line 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 variables 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 of their fields were listed individually.

If the selection in a Native SQL SELECTstatement is a table, you can pass it to ABAP line by line using the PERFORMINGaddition. The program calls a subroutine form for each line read. You can process the data further within the subroutine.

As in Open SQL, sy-dbcnt contains the number of processed lines after the statement ENDEXEC. sy-subrc contains in almost all cases the value 0 after the statement ENDEXEC. Exceptions are cursor operations: after FETCH, sy-subrc is if not more records could be read. This also applies when you read a result set using EXEC SQL PERFORMING.

REPORT demo_native_sql.

DATA: BEGIN OF wa,

connid TYPE spfli-connid,

cityfrom TYPE spfli-cityfrom,

cityto TYPE spfli-cityto,

END OF wa.

DATA c1 TYPE spfli-carrid VALUE 'LH'.

EXEC SQL PERFORMING loop_output.

SELECT connid, cityfrom, cityto

INTO :wa

FROM spfli

WHERE carrid = :c1

ENDEXEC.

FORM loop_output.

WRITE: / wa-connid, wa-cityfrom, wa-cityto.

ENDFORM.

regards Sunil Kumar Mutyala

Read only

Former Member
0 Likes
691

REPORT demo_native_sql.

DATA: BEGIN OF wa,

connid TYPE spfli-connid,

cityfrom TYPE spfli-cityfrom,

cityto TYPE spfli-cityto,

END OF wa.

DATA c1 TYPE spfli-carrid VALUE 'LH'.

EXEC SQL PERFORMING loop_output.

SELECT connid, cityfrom, cityto

INTO :wa

FROM spfli

WHERE carrid = :c1

ENDEXEC.

FORM loop_output.

WRITE: / wa-connid, wa-cityfrom, wa-cityto.

ENDFORM.

Read only

Former Member
0 Likes
691

hi,

Native SQL statements define an area in an ABAP program in which one or more Native SQL statements are to be carried out. The area between EXEC and ENDEXEC is not completely checked by the syntax check. The statements entered there are passed to the Native SQL interface and processed there as follows:

Almost all SQL statements that are valid for the addressed database system can be included between EXEC and ENDEXEC, in particular the DDL statements. These SQL statements are passed from the Native SQL interface to the database system largely unchanged. The syntax rules are specified by the database system, in particular the case sensitivity rules for database objects. If the syntax allows a separator character between individual statements, you can include several Native SQL statements between EXEC and ENDEXEC. Generally, the semicolon ( is used as the separator character.

You can also include SAP-specific Native SQL language elements between EXEC and ENDEXEC. These statements are not passed directly from the Native SQL interface to the database, but are converted appropriately.

All Native SQL statements bypass SAP buffering.

The ENDEXEC statement sets sy-dbcnt to the number of table rows processed in the last Native SQL statement. After implicit cursor processing with PERFORMING, sy-dbcnt contains the total number of lines read.

Programs with Native SQL statements are generally dependent on the database system used, so that they cannot be executed in all ABAP systems. This is especially true for the examples in this section, which was written for Informix database systems.

Example

Inserting two rows in the database table SCARR. If neither of these rows exists, sy-subrc is set to 0 by ENDEXEC and sy-dbcnt to 1. Otherwise, an exception is raised and handled.

DATA: exc_ref TYPE REF TO cx_sy_native_sql_error,

error_text TYPE string.



TRY. 
EXEC SQL. 
INSERT INTO scarr 
(MANDT, CARRID, CARRNAME, CURRCODE, URL) 
VALUES ('000', 'FF', 'Funny Flyers', 'EUR', 
'http://www.ff.com'); 
INSERT INTO scarr 
(MANDT, CARRID, CARRNAME, CURRCODE, URL) 
VALUES ('000', 'EF', 'Easy Flyers', 'EUR', 
'http://www.ef.com'); 
ENDEXEC. 
CATCH cx_sy_native_sql_error INTO exc_ref. 
error_text = exc_ref->get_text( ). 
MESSAGE error_text TYPE 'I'. 
ENDTRY.

Hope this helps, Do reward.

Read only

0 Likes
691

Secondary Index wil work if use Native sql.

Read only

0 Likes
691

hi,

Yes it will work.

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 of their fields were listed individually.

Native SQL allows you to execute (nearly) all statements available through the SQL programming interface (usually known as SQL Call Interface or similar) for executing SQL program code directly (using EXEC IMMEDIATE or a similar command).

Hope this helps, Do reward.

Edited by: Runal Singh on Apr 23, 2008 4:24 PM