2013-02-23 56 views
0

是否可以直接將SQL Server表中的數據導入SAP ABAP itab(表)?如何導入SQL Server表到SAP ABAP itab?

我想在SE37功能使用此ITAB ..謝謝提前

+0

是的。不,也許。也許,也許不是。你能否給我們一個更清晰的畫面,說明你想要做什麼以及你已經嘗試和研究過什麼? – vwegert 2013-02-23 15:56:54

+0

'SELECT * FROM INTO TABLE '?但是,你必須確保這個數據庫表非常小,並且不包含太多的記錄。 – Jagger 2013-02-24 14:59:34

回答

2

對於你的任務,你可以使用一個功能模塊(SE37),也可以使用模塊池,或報告(SE38,SE80) 。檢查此報告:

report zinsertdb. 

data: oref type ref to cx_root, 
     p_conex type dbcon_name value 'YOUR_DB_CONNECTION_STRING', " Ask the basis for this 
     lv_sw type c, 
     errormessage type string. 

data it_mydata type standard table of mara. 

start-of-selection. 

    perform connect. 
    perform insert_into_db. 
    perform disconnect. 

form connect. 
    try. 
     EXEC SQL. 
     CONNECT TO :p_conex 
     ENDEXEC. 
     lv_sw = 'X'. 
    catch cx_sy_native_sql_error into oref. 
     lv_sw = space. 
     errormessage = oref->get_text(). 
    endtry. 
endform. 

form disconnect. 
    clear errormessage. 
    if lv_sw = 'X'. 
    try. 
     EXEC SQL. 
      COMMIT 
     ENDEXEC. 
     catch cx_sy_native_sql_error into oref. 
     errormessage = oref->get_text().  
    endtry. 
    endif. 
endform. 

form insert_into_db. 

    data wa_mydata like line of it_mydata. 
    data zcount type i. 

    select * from mara into corresponding fields of table it_mydata. 

    if sy-subrc EQ 0. 

    loop at it_mydata into wa_mydata. 

     try . 

      exec sql. 
       EXECUTE PROCEDURE sp_get_data (IN :wa_mydata-EAN11, IN :wa_mydata-MEINS, OUT :zcount) 
      endexec. 

      if zcount eq 0. 
       exec sql. 
       INSERT INTO "Your_Database".dbo.[Your_table] 
       (Field1, Field2, Field3, FieldN) 
       VALUES (:wa_mydata-matnr, 
         :wa_mydata-ean11, 
         :wa_mydata-matkl, 
         :wa_mydata-meins) 
       endexec. 
       lv_sw = 'X'. 
      endif. 

      catch cx_sy_native_sql_error into oref. 
      lv_sw = space. 
      errormessage = oref->get_text(). 
     endtry. 

    endloop. 

    endif. 

endform. 

希望它有幫助。

1

你不知道你想做什麼。我假設你的意思是你想要將整個SQL表(或其中的某些條目)讀入程序存儲器中?也許你可以在這裏擺脫更多的光線?

如果出現這種情況,則可以簡單地聲明一個內部表,該表的結構中包含數據的SQL表。

DATA: table_name TYPE STANDARD/HASHED/SORTED TABLE OF name_of_sql_table.

FIELD-SYMBOLS <structure> TYPE name_of_sql_table.

SELECT * FROM name_of_sql_table INTO TABLE table_name.

從那裏它只是從內部表中讀取數據。

READ TABLE table_name ASSIGNING <structure> WITH KEY table_key_field(s) = condition(s).

但作爲賈格爾說:小心你的表有多大。