cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP Native SQL not working

TMNielsen
Contributor
0 Kudos

I have a requirement to update an external database via ODBC and found that I can native SQL for this.

I also found that I can try out native sql without connection to an external database. If I just use "EXEC SQL" without making a connection, the commands will run on the default connection - that is the SAP database.

Finally I found simple tutorial examples, but I can make none of the examples work.

If I for example run this below code I  get this error:
TMNielsen_0-1715779302948.png

DATA: BEGIN OF wa,
        matnr TYPE matnr,
      END OF wa.

DATA: exc_ref    TYPE REF TO cx_sy_native_sql_error,
      error_text TYPE string.

PARAMETERS: p_matnr TYPE matnr.

TRY.
    EXEC SQL.
      open dbcur for
      select matnr
        from mara
       where mandt = :sy-mandt
         and matnr = :p_matnr
    ENDEXEC.

    DO.
      exec sql.
      FETCH NEXT dbcur into wa-matnr
      ENDEXEC.
      IF sy-subrc NE 0.
        EXIT.
      ELSE.
        WRITE: / wa-matnr.

      ENDIF.
    ENDDO.

  CATCH cx_sy_native_sql_error INTO exc_ref.
    error_text = exc_ref->get_text( ).
    MESSAGE error_text TYPE 'I'.
ENDTRY.
Sandra_Rossi
Active Contributor
0 Kudos
What software are you using? SAP ERP? SAP CRM? SAP SRM? ABAP trial?
Sandra_Rossi
Active Contributor
0 Kudos

Except the missing colon in the "fetch" line, your program works for me.

      FETCH NEXT dbcur into :wa-matnr

Accepted Solutions (1)

Accepted Solutions (1)

TMNielsen
Contributor
0 Kudos

So I found solution my self. If I write everything between EXEC SQL. and ENDSQL. in capital letters, then it works.

For example:

TRY.
    EXEC SQL PERFORMING LOOP_OUTPUT.
      SELECT MATNR
      INTO :WA-MATNR
      FROM MARA
      WHERE MATNR = :P_MATNR
    ENDEXEC.
  CATCH cx_sy_native_sql_error INTO exc_ref.
    error_text = exc_ref->get_text( ).
    MESSAGE error_text TYPE 'I'.
ENDTRY.

Answers (1)

Answers (1)

TMNielsen
Contributor
0 Kudos

Hi Sandre

I'm using SAP ERP - SAP_BASIS 7.50.

I did make the test samples works by writing all the native SQL in capital letters.

Sandra_Rossi
Active Contributor
0 Kudos
Weird.