直联外部数据库,取数方法 DBCO DB02


REPORT  ztest001.

TABLES: ztest001.

TYPES: BEGIN OF st01,
          document_no TYPE ztest001-document_no,
          storage     TYPE ztest001-storage,
          drug_code   TYPE ztest001-drug_code,
       END   OF st01.
DATA:it_tab TYPE TABLE OF st01 WITH HEADER LINE,
     st_tab TYPE          st01.




DATA: g_con_name TYPE dbcon_name VALUE 'TEST',
     ex          TYPE REF TO cx_sy_native_sql_error.


**连接数据库
TRY.
    EXEC SQL.
      connect to :g_con_name
    ENDEXEC.

  CATCH cx_sy_native_sql_error INTO ex.
    RAISE connect_fails.
ENDTRY.

IF sy-subrc <> 0.
  RAISE connect_fails.
ENDIF.

*------------------------------------


**sql语句
EXEC SQL.
  OPEN lv_cur for
  select t1.document_no ,t1.storage,t2.drug_code
    from drug_export_master t1, drug_export_detail t2
   where t1.document_no = t2.document_no
     and t1.export_date between to_date(20000615, 'yyyy-mm-dd')
     and to_date(20000616235959, 'yyyy-mm-dd hh24miss')
ENDEXEC.


DO.
  EXEC SQL.
    FETCH NEXT lv_cur into :st_tab
  ENDEXEC.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.

*  WRITE:/ st_tab-document_no, st_tab-storage. ", st_tab-drug_code.
*  APPEND st_tab TO it_tab.

ENDDO.


*modify ztest001 from table it_tab.
EXEC SQL.
  close lv_cur.
ENDEXEC.

EXEC SQL.
  DISCONNECT :g_con_name
ENDEXEC.


*DATA: l_sql(72) TYPE c,
*      g_sql     TYPE zif_ttstr_tbl WITH HEADER LINE.
*DATA: it_tab1   TYPE TABLE OF ztest001.
*
*APPEND ' select  t1.document_no ,t1.storage,t2.drug_code,t1.receiver ' TO g_sql.
*APPEND ' from drug_export_master t1, drug_export_detail t2 ' TO g_sql.
*APPEND ' where t1.document_no = t2.document_no ' TO g_sql.
*APPEND ' and t1.export_date between to_date(20000615, ''yyyy-mm-dd'') ' TO g_sql.
*APPEND ' and to_date(20000616235959, ''yyyy-mm-dd hh24miss'')' TO g_sql.
*
*CALL FUNCTION 'ZIF_EXEC'
*  EXPORTING
*    iv_con_name = g_con_name
*    iv_tbtype   = 'ZTEST001'
*  TABLES
*    it_sql      = g_sql
*    et_table    = it_tab1.
*
*write:/ 'ok0'.

你可能感兴趣的:(SAP,date,Date,DATE,header,HEADER,Header,sql,SQL,Sql,table,数据库)