Вы находитесь на странице: 1из 6

4 Open SQL vs Native SQL 1. What is Open SQL vs Native SQL?

If you write a business application, there is always a database on backend. So SAP R/3 uses a database too. It is a special database? No. SAP uses standard databases like racle, I!" #!$, "S S%& Ser'er, etc. If you ha'e a database on backend, it is ine'itable that you (ust use S%&. SAP uses S%& to select, insert and update data inside database. )owe'er, the proble( is that if you use different databases, your code whate'er it is whether A!AP or not, S%& can 'ary. In that situation althou*h pro*ra((ers tend to use Standard S%& which is 'alid for all databases, the proble(s so(eti(es occur to switch one database to different database. 1.1 Open SQL pen S%& consists of a set of A!AP state(ents that perfor( operation on central database in the R/3 Syste(. +he results of the operations and any error (essa*es are independent of the database syste( in use. pen S%& thus pro'ides a unifor( synta, and se(antics for all of database syste(s supported by SAP. A!AP pro*ra(s that only use pen S%& state(ents will work in any SAP R/3 Syste(, re*ardless of the database syste( in use. pen S%& state(ents can work with database tables that ha'e been created in the A!AP #ictionary. +he (ethod actually is si(ple that when a pro*ra((er writes an A!AP pro*ra( with pen S%& state(ents, the kernel SAP pro*ra(s con'ert pen S%& state(ents to real / nati'e S%& state(ents for database in use. So like that write once, run for all databases and e'en for all operatin* syste(s. &ike -a'a.s /Write Once. Run Anywhere/. +hink about -a'a, e'en the -a'a uses the sa(e principal that is Java Virtual Machine which looks like SAP.s kernel pro*ra(s. Ri*ht? 0an we say SAP did /Write Once. Run Anywhere1 before -a'a? pen S%& contains the followin* keywords2

SELECT 3 Reads data fro( database tables. INSERT 3 Adds lines to database tables. !"#TE 3 0han*es the contents of lines of database tables. MO"I$% 3 Inserts lines into database tables or chan*es the contents of e,istin* lines. "ELETE 3 #elete lines fro( database tables. O!EN C RSOR& $ETC'& CLOSE C RSOR 3 Reads lines of database tables usin* the cursor.

All pen S%& state(ents fill the followin* two syste( fields with return codes2

$ Open SQL vs Native SQL

S%(S )RC After e'ery pen S%& state(ent, the syste( field S53S6!R0 contains 7 if the operation was successful, a 'alue other than 7 if not. S%(")CNT After an P8N S%& state(ent, the syste( field S53#!0N+ contains the nu(ber of database lines processed.

1.* Native SQL Nati'e S%& is real S%& for database in use. It (eans beside P8N S%&, if you need you can use the nati'e S%& for databases. Nati'e S%& allows you to use database3specific S%& state(ents in an A!AP pro*ra(. +his (eans that you can use database tables that are not ad(inistered by the A!AP #ictionary, and therefore inte*rate data that is not part of the R/3 Syste(. As a rule, an A!AP pro*ra( containin* database3specific S%& state(ents will not run under different database syste(s. If your pro*ra( will be used on (ore than one database platfor(, only use pen S%& state(ents. All A!AP pro*ra(s in SAP R/3 Syste( ha'e been written with pen S%&. I think if you ha'e a different database instant in the sa(e database, you can use Nati'e S%& state(ent to connect and do operation on this database instant. &et.s assu(e you ha'e an SAP R/3 syste( that uses racle database instant R04. 5ou ha'e another application, e'en it uses the sa(e database racle, but as nor(ally different database instant R0$. So like data inside A!AP pro*ra(, you can use Nati'e S%& state(ents to connect R0$, non3SAP database instant, to inte*rate SAP R/3 and non3 SAP syste(. It is kind of an inte*ration acti'ity. If you create a table by usin* database tools, without A!AP #ictionary, you are not able to use pen S%& to reach this table. 5ou 9ust can use Nati'e S%& to do that. Nati'e S%& state(ents bypass the R/3 database interface. +here is no table lo**in*, and no synchroni:ation with the database buffer on the application ser'er. ;or this reason, you should, where'er possible, use pen S%& to chan*e database tables declared in the A!AP #ictionary. In particular, tables declared in the A!AP #ictionary that contain lo* colu(ns with types &0)R and &RA< should only be addressed usin* pen S%&, since the colu(ns contain e,tra, database3specific len*th infor(ation for the colu(n. Nati'e S%& does not take this infor(ation into account, and (ay therefore produce incorrect results. ;urther(ore, Nati'e S%& does not support auto(atic client handlin*. Instead, you (ust treat client fields like any other. +o ensure that transactions in the R/3 Syste( are consistent, you should not use any transaction control state(ents =0 ""I+, R &&!A0> < R>?, or any state(ents that set transaction para(eters =isolation le'el@? usin* Nati'e S%&.

3 Open SQL vs Native SQL 6sin* Nati'e S%&, you can


+ransfer 'alues fro( A!AP fields to the database Read data fro( the database and process it in A!AP pro*ra(s.

Nati'e S%& works without the ad(inistrati'e data about database tables stored in the A!AP #ictionary. 0onseAuently, it cannot perfor( all of the consistency check used in pen S%&. +his places a lar*er de*ree responsibility on application de'elopers to work with A!AP fields of the correct type. 5ou should always ensure that the A!AP data type and the type of database colu(n are identical. *. What +,es an E-EC SQL state.ent +, in #)#!? EXEC SQL Syntax EXEC SQL [PERFORMING subr]. ... ENDEXEC. Effect These 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 comp ete y chec!ed by the syntax chec!. The statements entered there are passed to the Native SQL interface and processed there as fo ows" Almost all SQL statements that are valid for the addressed database system can be included between EXEC and ENDEXEC, in particular the DDL statements !hese SQL statements are passed from the Native SQL interface to the database system lar"ely unchan"ed !he synta# rules are specified by the database system, in particular the case sensitivity rules for database ob$ects %f the synta# allows a separator character between individual statements, you can include several Native SQL statements between EXEC and ENDEXEC &enerally, the semicolon ';( is used as the separator character #ou can a so inc ude SAP$specific Native SQL anguage e ements between EXEC and ENDEXEC. These statements are not passed direct y from the Native SQL interface to the database% but are converted appropriate y. These SAP$specific anguage e ements are"" &ost variab es Statements for cursor processing 'atabase procedure ca s Statements for estab ishing database connections

B Open SQL vs Native SQL A Native SQL statements bypass SAP buffering. Automatic c ient hand ing is not performed. System fields The statement ENDEXEC sets the system fie ds sy-subrc and sy-dbcnt. (hen using the addition PERFORMING% note that imp icit cursor processing is carried out and the system fie ds are set for every read process. sysubrc 7 B Meanin/ +he state(ents between EXEC and ENDEXEC were e,ecuted successfully. +he state(ents between EXEC and ENDEXEC were not e,ecuted. After i(plicit cursor processin* with PERFORMING, sy-subrc always contains the 'alue B.

The ENDEXEC statement sets sy-dbcnt to the number of tab e rows processed in the ast Native SQL statement. After imp icit cursor processing with PERFORMING% sy-dbcnt contains the tota number of ines read. Note Programs with Native SQL statements are genera y dependent on the database system used% so that they cannot be executed in a ABAP systems. This is especia y true for the examp es in this section% which was written for )nformix database systems. 0. What is the +isa+vanta/e ,1 usin/ it? Native SQL #+vanta/es an+ "isa+vanta/es ( E-EC SQL state.ent Ad'anta*es

+ables are not declared in A!AP #ictionary can be accessed. =e.*. +ables belon*in* to sys or syste( user of racle, etc.? +o use so(e of the special features supported by the database3specific S%&. =e.*. Passin* hints to racle opti(i:er.?

#isad'anta*es

No synta, check is perfor(ed whate'er is written between 8C80 and 8N#8C80.

K Open SQL vs Native SQL


A!AP pro*ra( containin* database3specific S%& state(ents will not run under different database syste(s. +here is no auto(atic client handlin* for client dependent tables. 0are has to be taken durin* (i*ration to hi*her 'ersions.

E2a.ple 1 Insertin* two rows in the database table S0ARR. If neither of these rows e,ists, s3(su4rc is set to 7 by EN"E-EC and s3(+4cnt to 4. therwise, an e,ception is raised and handled. #A+A2 e,cDref +5P8 R8; + c,DsyDnati'eDsAlDerror, errorDte,t +5P8 strin*. +R5. 8C80 S%&. INS8R+ IN+ scarr ="AN#+, 0ARRI#, 0ARRNA"8, 06RR0 #8, 6R&? EA&68S =F777F, F;;F, F;unny ;lyersF, F86RF, Fhttp2//www.ff.co(F?G INS8R+ IN+ scarr ="AN#+, 0ARRI#, 0ARRNA"8, 06RR0 #8, 6R&? EA&68S =F777F, F8;F, F8asy ;lyersF, F86RF, Fhttp2//www.ef.co(F?G 8N#8C80. 0A+0) c,DsyDnati'eDsAlDerror IN+ e,cDref. errorDte,t H e,cDref3I*etDte,t= ?. "8SSAJ8 errorDte,t +5P8 FIF. 8N#+R5. E2a.ple * Readin* se'eral rows fro( the database table SP;&I usin* cursor handlin* and host 'ariables in nati'e S%&. If rows were found, s3(su4rc is set to 7 and s3(+4cnt is increased by one for each row read. !#R#METERS p5carri+ T%!E sp1li(carri+. "#T#6 c,nni+ T%!E sp1li(c,nni+& cit31r,. T%!E sp1li(cit31r,.& cit3t, T%!E sp1li(cit3t,. E-EC SQL. O!EN +4cur $OR SELECT c,nni+& cit31r,.& cit3t, $ROM sp1li W'ERE carri+ 7 6p5carri+

L Open SQL vs Native SQL EN"E-EC. "O. E-EC SQL. $ETC' NE-T +4cur INTO 6c,nni+& 6cit31r,.& 6cit3t, EN"E-EC. write2 / connid,cityfro(,cityto. I$ s3(su4rc 89 :. E-IT. ELSE. ... EN"I$. EN""O. E-EC SQL. CLOSE +4cur EN"E-EC.

Вам также может понравиться