cancel
Showing results for 
Search instead for 
Did you mean: 

Connection to SQL

Former Member
0 Kudos

Hi experts

Could somebody sent me an example of the code to do the connection to SQL

correctly... THANKS

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi ARACELI GONZALEZ,

you can find a quite helpful example for this in SAP note #323151:

First you need to open the connection to the server:

EXEC SQL.

CONNECT TO <con_name> [ AS <alias_name> ]

ENDEXEC.

Then you need to set the connection (just in case you went back to the default connection or another connection in the meantime)

EXEC SQL.

SET CONNECTION { <alias_name> DEFAULT }

ENDEXEC.

And in the end, as soon as you don't need the connection anymore, you can close it again:

EXEC SQL.

SET CONNECTION { <alias_name> DEFAULT }

ENDEXEC.

Complete coding example: (can be found in note 323151 too ...)

  • Test if connection 'con_name' has already been opened

EXEC SQL.

SET CONNECTION :con_name

ENDEXEC.

IF SY-SUBRC <> 0.

  • Connection not yet opened.

EXEC SQL.

CONNECT TO :con_name

ENDEXEC.

IF SY-SUBRC <> 0.

  • error handling

ENDIF.

ENDIF.

  • Do something on connection 'con_name'

EXEC SQL.

...

ENDEXEC.

  • Commit the changes on 'con_name'

EXEC SQL.

COMMIT

ENDEXEC.

  • Reset to "default connection"

EXEC SQL.

SET CONNECTION DEFAULT

ENDEXEC.

ENDFORM.

Best regards,

Beate

Former Member
0 Kudos

Hi thenk you very much for your answe I have made my connection correctly because SY-SUBRC is setting 4 after the statment CONNECT

I have made the following code.. But when I run the aplication appears this error before close the connection to the BD

The error is DBIF_DSQL2_SQL_ERROR

SQL error 170

I don't know what happen... Could you help me??? Please

this is my code:

REPORT Z_CONEXBD

DATA: c_pilot TYPE FLIGHTS-pilot.

DATA: con_name LIKE dbcon-con_name VALUE 'CON'.

EXEC SQL.

SET CONNECTION :con_name

ENDEXEC.

IF sy-subrc <> 0.

  • Connection not yet opened.

EXEC SQL.

CONNECT TO :con_name

ENDEXEC.

endif.

  • Do something on connection 'con_name'

EXEC SQL.

select pilot

into :c_pilot

from flights.

ENDEXEC.

*Commit the changes on 'con_name'

EXEC SQL.

COMMIT

ENDEXEC.

EXEC SQL.

SET CONNECTION :con_name

ENDEXEC.

EXEC SQL.

DISCONNECT :con_name

ENDEXEC.

ENDFORM.

-


This is the DBCO DATA

Connection name : CON

DBMS: MSS

Conn. info: MSSQL_SERVER=NENITA MSSQL_DBNAME=MBS

I'm not ussing user neither password.

I 'm using minisap 4.6 with Gui Patch 6.2, WIndows Xp, and SQL 2000

THANKS FOR YOUR HELP

Answers (0)