REPORT ZSCHEDULE.
DATA:
CREATE_FIELDS TYPE STRING,
CREATE_PKEY TYPE STRING,
INSERT_VALUES TYPE STRING,
IT_INSERT TYPE STRINGTAB,
DATUM TYPE DATUM,
UZEIT TYPE UZEIT.
FIELD-SYMBOLS: <LT_SALV> TYPE ANY TABLE,
<VALUE> TYPE ANY.
DATA:
LS_VBAK TYPE VBAK,
LS_VBAP TYPE VBAP.
SELECT-OPTIONS:
SO_VKORG FOR LS_VBAK-VKORG,
SO_VTWEG FOR LS_VBAK-VTWEG,
SO_SPART FOR LS_VBAK-SPART,
SO_VSTEL FOR LS_VBAP-VSTEL,
SO_VKBUR FOR LS_VBAK-VKBUR.
START-OF-SELECTION.
TRY.
CL_SALV_BS_RUNTIME_INFO=>SET( DISPLAY = ABAP_FALSE
METADATA = ABAP_TRUE
DATA = ABAP_TRUE ).
SUBMIT Z_RPT_PROG
WITH SO_VKORG IN SO_VKORG
WITH SO_VTWEG IN SO_VTWEG
WITH SO_SPART IN SO_SPART
WITH SO_VSTEL IN SO_VSTEL
WITH SO_VKBUR IN SO_VKBUR
AND RETURN.
DATA(SALV_META) = CL_SALV_BS_RUNTIME_INFO=>GET_METADATA( ).
CL_SALV_BS_RUNTIME_INFO=>GET_DATA_REF( IMPORTING R_DATA = DATA(SALV_DATA) ).
ASSIGN SALV_DATA->* TO <LT_SALV>.
CL_SALV_BS_RUNTIME_INFO=>CLEAR_ALL( ).
...
...
LOOP AT SALV_META-T_FCAT INTO DATA(WA_FCAT).
REPLACE ALL OCCURRENCES OF '/' IN WA_FCAT-FIELDNAME WITH '_'. " forward slash / is considered COMMENT in SQL
MODIFY SALV_META-T_FCAT FROM WA_FCAT TRANSPORTING FIELDNAME.
CASE WA_FCAT-DATATYPE.
WHEN 'CHAR'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } varchar({ WA_FCAT-INTLEN })|.
WHEN 'CUKY'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } varchar(5)|.
WHEN 'UNIT'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } varchar(3)|.
WHEN 'DATS'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } date|.
WHEN 'TIMS'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } time|.
WHEN 'RAW'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } nvarchar(max)|.
WHEN 'NUMC' OR 'INT4' OR 'DEC' OR 'QUAN' OR 'CURR'.
CREATE_FIELDS = CREATE_FIELDS && |{ WA_FCAT-FIELDNAME } float|.
ENDCASE.
CREATE_FIELDS = CREATE_FIELDS && ','.
IF WA_FCAT-KEY EQ 'X'.
IF CREATE_PKEY IS NOT INITIAL.
CREATE_PKEY = CREATE_PKEY && ','.
ENDIF.
CREATE_PKEY = CREATE_PKEY && |[{ WA_FCAT-FIELDNAME }]|.
ENDIF.
ENDLOOP.
DATA(TABLE) = `SAP_ZREPORT`.
IF CREATE_PKEY IS INITIAL.
DATA(CREATE_TABLE) = |CREATE TABLE { TABLE } ( { CREATE_FIELDS } )|.
ELSE.
CREATE_TABLE = |CREATE TABLE { TABLE } ( { CREATE_FIELDS } PRIMARY KEY ({ CREATE_PKEY }) )|.
ENDIF.
...
...
LOOP AT SALV_META-T_FCAT ASSIGNING FIELD-SYMBOL(<FIELD>).
CASE <FIELD>-DATATYPE.
WHEN 'CHAR'.
<FIELD>-TOOLTIP = |varchar({ <FIELD>-INTLEN })|.
WHEN 'CUKY'.
<FIELD>-TOOLTIP = |varchar(5)|.
WHEN 'UNIT'.
<FIELD>-TOOLTIP = |varchar(3)|.
WHEN 'DATS'.
<FIELD>-TOOLTIP = |date|.
WHEN 'TIMS'.
<FIELD>-TOOLTIP = |time|.
WHEN 'RAW'.
<FIELD>-TOOLTIP = |nvarchar(max)|.
WHEN 'NUMC' OR 'INT4' OR 'DEC' OR 'QUAN' OR 'CURR'.
<FIELD>-TOOLTIP = |float|.
ENDCASE.
REPLACE ALL OCCURRENCES OF '/' IN <FIELD>-SELTEXT WITH '-'. " forward slash / is considered COMMENT in SQL
IF <FIELD>-SELTEXT IS INITIAL. " COMMENT/DESCRIPTION cannot be blank in ALTER TABLE statement
<FIELD>-SELTEXT = <FIELD>-FIELDNAME.
ENDIF.
ENDLOOP.
LOOP AT SALV_META-T_FCAT INTO DATA(WA_TABLE_FIELDS).
DATA(ALTER_TABLE) = |ALTER TABLE { TABLE } ALTER COLUMN "{ WA_TABLE_FIELDS-FIELDNAME }" { WA_TABLE_FIELDS-TOOLTIP } NULL|.
DATA(ALTER) = |{ ALTER_TABLE } EXECUTE sp_addextendedproperty 'MS_Description', '{ WA_TABLE_FIELDS-SELTEXT }', 'Schema', 'dbo', 'table', '{ TABLE }', 'column', '{ WA_TABLE_FIELDS-FIELDNAME }';|.
APPEND ALTER TO IT_INSERT.
ENDLOOP.
...
ALTER TABLE <tablename> ALTER COUMN "<fieldname>" <datatype> NULL EXECUTE sp_addextendedproperty 'MS_Description', '<comment/description>', 'Schema', '<DBschema>', 'table', '<tablename>', 'column', '<fieldname>';
DATA(DBCONN) = CL_SQL_CONNECTION=>GET_CONNECTION( 'SQL' ).
DATA(CREATE_STATEMENT) = DBCONN->CREATE_STATEMENT( ).
CREATE_STATEMENT->EXECUTE_DDL( CREATE_TABLE ). " create table statement
LOOP AT IT_INSERT INTO DATA(ROW).
CREATE_STATEMENT->EXECUTE_DDL( ROW ). " alter table statment
ENDLOOP.
DBCONN->COMMIT( ).
DBCONN->CLOSE( ).
...
LOOP AT <LT_SALV> ASSIGNING FIELD-SYMBOL(<WA>).
LOOP AT SALV_META-T_FCAT ASSIGNING <FIELD>.
IF INSERT_VALUES IS NOT INITIAL.
INSERT_VALUES = INSERT_VALUES && ','.
ENDIF.
ASSIGN COMPONENT <FIELD>-FIELDNAME OF STRUCTURE <WA> TO <VALUE>.
IF <VALUE> IS ASSIGNED.
IF <FIELD>-DATATYPE = 'DATS'.
IF <VALUE> IS INITIAL.
INSERT_VALUES = INSERT_VALUES && |''|.
ELSE.
DATUM = <VALUE>.
DATA(DATE_STR) = |{ DATUM DATE = ISO }|.
INSERT_VALUES = INSERT_VALUES && |'{ DATE_STR }'|.
ENDIF.
ELSEIF <FIELD>-DATATYPE = 'TIMS'.
IF <VALUE> IS INITIAL.
INSERT_VALUES = INSERT_VALUES && |''|.
ELSE.
UZEIT = <VALUE>.
DATA(TIME_STR) = |{ UZEIT TIME = ISO }|.
INSERT_VALUES = INSERT_VALUES && |'{ TIME_STR }'|.
ENDIF.
ELSE.
INSERT_VALUES = INSERT_VALUES && |'{ <VALUE> }'|.
ENDIF.
ENDIF.
ENDLOOP.
DATA(INSERT) = |INSERT INTO { TABLE } VALUES ({ INSERT_VALUES })|.
APPEND INSERT TO IT_INSERT.
CLEAR: INSERT, INSERT_VALUES.
ENDLOOP.
IF IT_INSERT IS NOT INITIAL.
DATA(DBCONN) = CL_SQL_CONNECTION=>GET_CONNECTION( 'SQL' ).
DATA(INSERT_STATEMENT) = DBCONN->CREATE_STATEMENT( ).
LOOP AT IT_INSERT INTO DATA(ROW).
INSERT_STATEMENT->EXECUTE_UPDATE( ROW ).
ENDLOOP.
ENDIF.
DBCONN->COMMIT( ).
DBCONN->CLOSE( ).
IF SY-SUBRC = 0.
MESSAGE 'SQL operation successfull!' TYPE 'I' DISPLAY LIKE 'S'.
ENDIF.
CATCH CX_ROOT INTO DATA(CX_ERROR).
DATA(ERROR) = CX_ERROR->GET_LONGTEXT( ).
IF ERROR IS INITIAL.
ERROR = CX_ERROR->GET_TEXT( ).
ENDIF.
MESSAGE ERROR TYPE 'I'.
ENDTRY.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
9 | |
7 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 | |
4 |