如何实现SAP跨系统取数 SAP读取ORACLE或者SQL server数据库
创建连接参数
公司在实施SAP后,可能会与其他业务系统进行集成。这样,就需要不同系统间进行同步数据,下面介绍SAP如何从其他数据库读取数据。以SQL为例。
第一步:创建数据库连接参数,键入DBCO(或使用SM30维护表DBCON的内容),进入数据库连接总览界面。添加一个新的连接参数,输入连接名,DBS是指连接数据库的类型,SQL的为MSS,用户名,需在数据库系统中存在的,密码,连接信息:MSSQL_SERVER=IP adress MSSQL_DBNAME=dbname OBJECT_SOURCE=dbname;
ABAP编码
然后键入se38,新建一个报表程序。以下是代码内容。
report ZCMH0026.
*--------------------------------------------------------------------*
*------- DBCO 里的连接信息 -------------------------------*
*--MSSQL_SERVER=10.1.7.7 MSSQL_DBNAME=saptest OBJECT_SOURCE=saptest ------------*
*----连接名称: YANGTEST ----------------------------------------*
*-----------------------------------------------------------------*
DATA g_conexion LIKE dbcon-con_name VALUE 'YANGTEST'.
DATA: exec_ref TYPE REF TO cx_sy_native_sql_error,
error_text TYPE string.
DATA: g_errorstr(250) TYPE c.
DATA: BEGIN OF WA_TRANS ,
NAME(30) TYPE C,
LIST(30) TYPE C,
END OF WA_TRANS.
DATA: dbcur TYPE CURSOR.
START-OF-SELECTION.
*首先建立数据库连接
PERFORM sub_conndb USING g_conexion.
PERFORM getdata.
"PERFORM updatedata.
"PERFORM INSERTDATA.
END-OF-SELECTION.
FORM sub_conndb USING conn LIKE dbcon-con_name.
CLEAR g_errorstr.
g_conexion = conn.
TRY.
"-- 连接SQL SERVER
EXEC SQL.
CONNECT TO :G_CONEXION
ENDEXEC.
EXEC SQL.
SET CONNECTION :G_CONEXION
ENDEXEC.
CATCH cx_sy_native_sql_error INTO exec_ref.
CONCATENATE '无法连接至SQL数据库 连接名:' g_conexion INTO g_errorstr.
ENDTRY.
ENDFORM.
"-- 获取 SQL SERVER 里的表数据 方法一
FORM getdata.
*-- 表 TABLE_2 是SQL 数据库 saptest 里存在的表
TRY.
EXEC SQL.
OPEN dbcur FOR SELECT * FROM TABLE_2
ENDEXEC.
WRITE:/ 'NAME' , 'LIST'.
DO.
EXEC SQL.
FETCH NEXT dbcur INTO :wa_trans.
ENDEXEC.
IF sy-subrc <> 0.
EXIT.
ELSE.
WRITE:/ WA_TRANS-NAME , WA_TRANS-LIST.
ENDIF.
ENDDO.
EXEC SQL.
CLOSE dbcur
ENDEXEC.
CATCH cx_sy_native_sql_error INTO exec_ref.
g_errorstr = exec_ref->get_text( ).
CONCATENATE '读取数据出错,' g_errorstr INTO g_errorstr.
WRITE:/ g_errorstr.
ENDTRY.
ENDFORM.
"-- 获取 SQL SERVER 里的表数据 方法二 : 不使用游标
FORM getdata2.
TRY.
EXEC SQL PERFORMING LOOP_OUTPUT.
SELECT *
INTO :WA_TRANS
FROM TABLE_2
ENDEXEC.
CATCH cx_sy_native_sql_error INTO exec_ref.
g_errorstr = exec_ref->get_text( ).
CONCATENATE '读取数据出错,' g_errorstr INTO g_errorstr.
WRITE:/ g_errorstr.
ENDTRY.
ENDFORM.
FORM LOOP_OUTPUT.
WRITE :/ WA_TRANS-NAME,' ',WA_TRANS-LIST.
ENDFORM.
FORM updatedata.
TRY.
EXEC SQL .
update TABLE_2 set list = 11 where name = 1
ENDEXEC.
CATCH cx_sy_native_sql_error INTO exec_ref.
g_errorstr = exec_ref->get_text( ).
CONCATENATE '更新数据出错,' g_errorstr INTO g_errorstr.
WRITE:/ g_errorstr.
ENDTRY.
PERFORM getdata2.
ENDFORM.
FORM INSERTDATA.
TRY.
EXEC SQL .
insert TABLE_2(name,list) values(5,55)
ENDEXEC.
CATCH cx_sy_native_sql_error INTO exec_ref.
g_errorstr = exec_ref->get_text( ).
CONCATENATE '插入数据出错,' g_errorstr INTO g_errorstr.
WRITE:/ g_errorstr.
ENDTRY.
PERFORM getdata2.
ENDFORM.