* SQL tool for SAP ABAP Programmers - BOTH OPEN & NATIVE SQLs
* very light - approx. 20KB
*
* Objective - to see JOINS in SAP to confirm or discover relationships
* and to see Data side by Side
* Read "SAP Table and Field search strategies"
*in http://sapabap.iespana.es/sapabap/sap/info/search_fields_tables.htm
* Use SAP_TABLES.exe Document in http://www.sap-img.com
*and many other excellent resources to navigate the cryptic tables &
*Colums of SAP
*
*SE16 is the best for 1 table inspection & you can open several sessions
*if
* you have more than 1 table.
* You may "hate joins" and prefer looping matches on Internal Tables.
* However if you wish to see the relationships in DATA VISIBLE format
* NOTHING succeeds like JOINs
* I came in with a Strong Oracle TOAD background and feel comfortable
* in seeing DATA together
*The decision to use JOIN or use iterative Internal Table match with
*single select
* does not detract from the visibility of tracking relationships
* SQL Must be SELECT
* List of Columns Selected before 1st FROM Must Have
* 1 Column per line in format TABLE~COLUMN if Open SQL
* 1 Column per line in format TABLE.COLUMN if Native SQL
* Naturally tables & Columns must exist
* as this used to dynamically create Internal Table for ALV Grid
*
*Count( * ) is NOT SUPPORTED but you could Use Native COUNT( any NOT
*NULL NUMERIC COLUMN )
* SUM MIN MAX AVG supported
* SUM( table~COLUMN ) or SUM( table.COLUMN )
*but there must be 1 space as indicated after ( and before ) -- even for
*native
*If you use NATIVE SQL make sure you have :SY-MANDT filter in WHERE
*Clause
*You CAN pick 2 COLUMNS having same name - this is important for
*inspection
* Program creates right aliases as you can see in c:\jnc.abap
*jnc.abap is the generated ABAP program for diagnostics and possible
*reuse
* JOINs and SUBQUERIES are NOT ALLOWED for
* Pooled Tables, Clustered Tables & Projection Views
*Even AGGREGATE Functions are NOT ALLOWED! -- thse restrictions are
*inherent in SAP
* So this tool is useful for TRANSPARENT TABLES only!
* Updated on 20070801
* Now supports all aggregated function, join(except natural join)
* and aliasing.
* Better performance (especially, response time).
* 'select *' and 'select distinct' works.
* 'select single' is not. Use 'Row count' option in ui
* Change dump file extension(jnc.abap)
*TextEdit Control Tool Code Copied from SAP Standard Example
*saptextedit_demo_3
*This is FREE software with FULL responsibility on USER & anyone
*changing sourcecode!
* define table type for data exchange
CONSTANTS c_line_length TYPE i VALUE 80.
TYPES: BEGIN OF mytable_line,
line(c_line_length) TYPE c,
END OF mytable_line.
TYPES mytable_type TYPE TABLE OF mytable_line.
*----------------------------------------------------------------------*
* CLASS ex_y4s DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS ex_y4s DEFINITION INHERITING FROM cx_static_check.
PUBLIC SECTION.
METHODS constructor IMPORTING txt TYPE string.
METHODS get_text REDEFINITION.
PRIVATE SECTION.
DATA txt TYPE string.
ENDCLASS. "ex_y4s DEFINITION
*----------------------------------------------------------------------*
* CLASS select DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS select DEFINITION.
PUBLIC SECTION.
INTERFACES query.
ALIASES getquery FOR query~getquery.
ALIASES init FOR query~init.
ALIASES execute FOR query~execute.
PRIVATE SECTION.
DATA: txts TYPE mytable_type.
DATA : BEGIN OF tblcol_lin,
aggname TYPE string,
distinct TYPE c,
tabname TYPE dd03l-tabname,
fieldname TYPE dd03l-fieldname,
END OF tblcol_lin.
DATA tblcol_tab LIKE TABLE OF tblcol_lin.
METHODS regulate RAISING ex_y4s.
ENDCLASS. "select DEFINITION
*----------------------------------------------------------------------*
* CLASS yes4sql DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS yes4sql DEFINITION.
PUBLIC SECTION.
METHODS constructor.
METHODS execute.
METHODS exit.
METHODS init.
METHODS setquery.
PRIVATE SECTION.
DATA q TYPE REF TO query.
DATA:
* reference to wrapper class of control based on OO Framework
g_editor TYPE REF TO cl_gui_textedit,
* reference to custom container: necessary to bind TextEdit Control
g_editor_container TYPE REF TO cl_gui_custom_container,
msg TYPE string.
* table to exchange text
DATA g_mytable TYPE mytable_type.
ENDCLASS. "yes4sql DEFINITION
*----------------------------------------------------------------------*
* GLOBAL Variable
*----------------------------------------------------------------------*
DATA ex TYPE REF TO ex_y4s.
DATA y4s TYPE REF TO yes4sql.
DATA:
rows TYPE i,
isopen TYPE c,
g_repid LIKE sy-repid,
g_ok_code LIKE sy-ucomm. " return code from screen
*----------------------------------------------------------------------*
* CLASS ex_y4s IMPLEMENTA╬кION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS ex_y4s IMPLEMENTATION.
METHOD constructor.
super->constructor( ).
me->txt = txt.
ENDMETHOD. "constructor
METHOD get_text.
result = me->txt.
ENDMETHOD. "get_text
ENDCLASS. "ex_y4s IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS select IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS select IMPLEMENTATION.
METHOD getquery.
txts = me->txts.
ENDMETHOD. "getquery
METHOD execute.
DATA:
first TYPE i,
off TYPE i,
numrows TYPE i,
rownum TYPE i,
mystring TYPE string,
aggcntfld TYPE string,
crows(8) TYPE c,
code TYPE TABLE OF rssource-line,
prog(8) TYPE c,
msg TYPE string,
sid TYPE string,
lin(3) TYPE c,
wrd(128) TYPE c,
txts_l LIKE LINE OF txts.
IF LINES( tblcol_tab ) = 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'Table~Column Open SQL is MUST'
txt1 = 'Table.Column Native SQL is MUST'.
RETURN.
ENDIF.
APPEND 'Program SubPool.' TO code.
APPEND '' TO code.
APPEND 'data : begin of I_TAB occurs 0,' TO code.
numrows = LINES( tblcol_tab ).
MOVE 0 TO rownum.
LOOP AT tblcol_tab INTO tblcol_lin.
ADD 1 TO rownum.
WRITE rownum TO crows LEFT-JUSTIFIED.
CONCATENATE 'WFLD' crows INTO mystring.
IF tblcol_lin-aggname CS 'count'.
IF tblcol_lin-fieldname = '*'.
aggcntfld = 'INTO I_TAB'.
ENDIF.
CONCATENATE mystring ' LIKE sy-dbcnt,' INTO mystring.
ELSE.
CONCATENATE mystring 'LIKE' tblcol_lin-tabname
INTO mystring SEPARATED BY space.
CONCATENATE mystring '-' tblcol_lin-fieldname ',' INTO mystring.
ENDIF.
APPEND mystring TO code.
ENDLOOP.
APPEND 'END OF I_TAB.' TO code.
APPEND '' TO code.
APPEND 'DATA : R_TAB LIKE LINE OF I_TAB,' TO code.
APPEND ' L_KOUNT type I.' TO code.
APPEND '' TO code.
APPEND 'DATA : MyString type STRING.' TO code.
APPEND 'DATA : MyTitle type LVC_TITLE.' TO code.
APPEND '' TO code.
APPEND 'Data: ROWS type I.' TO code.
APPEND '' TO code.
APPEND 'Form DoSQL.' TO code.
APPEND '' TO code.
WRITE rows TO crows.
REPLACE ALL OCCURRENCES OF ',' IN crows WITH ''.
CONCATENATE 'Move' crows 'to ROWS.' INTO mystring SEPARATED BY space
.
APPEND mystring TO code.
APPEND '' TO code.
MOVE 0 TO rownum.
LOOP AT tblcol_tab INTO tblcol_lin.
ADD 1 TO rownum.
IF rownum = 1.
CONCATENATE ' '
'Move ''' tblcol_lin-fieldname ''' to MyString.' INTO
mystring.
APPEND mystring TO code.
ELSE.
CONCATENATE ' Concatenate MyString '','' '''
tblcol_lin-fieldname ''' Into MyString.' INTO mystring.
APPEND mystring TO code.
ENDIF.
APPEND ' Move MyString to MyTitle.' TO code.
APPEND '' TO code.
ENDLOOP.
APPEND '' TO code.
APPEND 'Try.' TO code.
APPEND '' TO code.
IF isopen <> 'X'.
APPEND 'Move 0 to L_KOUNT.' TO code.
APPEND 'EXEC SQL.' TO code.
APPEND ' open c1 for ' TO code.
ENDIF.
MOVE 0 TO first.
LOOP AT txts INTO txts_l.
IF isopen = 'X' AND first = 0.
FIND FIRST OCCURRENCE OF REGEX '\s*from\s+'
IN txts_l IGNORING CASE.
IF sy-subrc = 0.
IF aggcntfld IS INITIAL.
APPEND 'INTO TABLE I_TAB UP TO ROWS rows' TO code.
ELSE.
APPEND aggcntfld TO code.
ENDIF.
MOVE 1 TO first.
ENDIF.
ENDIF.
APPEND txts_l TO code.
ENDLOOP.
IF isopen = 'X'.
IF first = 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'Open SQL Without a FROM'
txt1 = 'Correct & retry'.
RETURN.
ENDIF.
APPEND '.' TO code.
IF aggcntfld IS NOT INITIAL.
APPEND 'APPEND i_tab.' TO code.
ENDIF.
ELSE.
APPEND 'ENDEXEC.' TO code.
APPEND '' TO code.
APPEND 'DO.' TO code.
APPEND ' EXEC SQL.' TO code.
APPEND ' fetch next c1 INTO :R_TAB ' TO code.
APPEND ' ENDEXEC.' TO code.
APPEND ' IF sy-subrc <0>= ROWS.' TO code.
APPEND ' Exit.' TO code.
APPEND ' EndIf.' TO code.
APPEND 'ENDDO.' TO code.
APPEND '' TO code.
APPEND 'EXEC SQL.' TO code.
APPEND ' close c1' TO code.
APPEND 'ENDEXEC.' TO code.
APPEND '' TO code.
ENDIF.
APPEND '' TO code.
APPEND 'PERFORM zjnc_dump_list ' &
'USING ''I_TAB[]'' ''I_TAB'' MyTitle.' TO code.
APPEND '' TO code.
APPEND 'Catch CX_ROOT.' TO code.
IF isopen <> 'X'.
APPEND 'EXEC SQL.' TO code.
APPEND ' close c1' TO code.
APPEND 'ENDEXEC.' TO code.
ENDIF.
APPEND 'CALL FUNCTION ''POPUP_TO_INFORM''' TO code.
APPEND ' EXPORTING' TO code.
APPEND ' titel = ''jncDynamicSub''' TO code.
APPEND ' txt2 = ''Generate SUBROUTINE POOL ' &
'Succeeded BUT SQL failed''' TO code.
APPEND ' txt1 = ''Possible Wrong SQL - see c:\jnc.abap''.'
TO code.
APPEND 'EndTry.' TO code.
APPEND 'EndForm. "DoSQL.' TO code.
APPEND '' TO code.
APPEND
'*&------------------------------------------------------------------' &
'--*'
TO code.
APPEND
'*& Form ZJNC_DUMP_LIST Our Good Old ALV list - RECOMMENDED!'
TO code.
APPEND
'*&------------------------------------------------------------------' &
'--*'
TO code.
APPEND 'FORM zjnc_dump_list USING value(p_it_name) TYPE c' TO code.
APPEND ' value(p_wa_name) TYPE c' TO code.
APPEND ' value(p_heading) TYPE c.' TO code.
APPEND '' TO code.
APPEND ' TYPE-POOLS: slis.' TO code.
APPEND '' TO code.
APPEND ' DATA:' TO code.
APPEND ' stru_ref TYPE REF TO cl_abap_structdescr,' TO code.
APPEND ' comp_tab TYPE abap_compdescr_tab,' TO code.
APPEND ' one_comp TYPE abap_compdescr,' TO code.
APPEND ' lt_fcat TYPE slis_t_fieldcat_alv,' TO code.
APPEND ' wa_fcat TYPE slis_fieldcat_alv,' TO code.
APPEND ' ls_layo TYPE slis_layout_alv,' TO code.
APPEND ' l_alv TYPE REF TO cl_gui_alv_grid.' TO code.
APPEND ' DATA : BEGIN OF tblcol_lin,' TO code.
APPEND ' aggname TYPE string,' TO code.
APPEND ' tabname TYPE dd03l-tabname,' TO code.
APPEND ' fieldname TYPE dd03l-fieldname,' TO code.
APPEND ' END OF tblcol_lin.' TO code.
APPEND ' DATA:' TO code.
APPEND ' tblcol_tab LIKE TABLE OF tblcol_lin,' TO code.
APPEND ' idx TYPE i.' TO code.
APPEND '' TO code.
APPEND ' FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE,' TO code.
APPEND ' <fs_line> TYPE ANY.' TO code.
APPEND '' TO code.
APPEND ' ASSIGN (p_it_name) TO <fs_table>.' TO code.
APPEND '' TO code.
APPEND ' ASSIGN (p_wa_name) TO <fs_line>.' TO code.
APPEND '' TO code.
APPEND ' ls_layo-colwidth_optimize = ''X''.' TO code.
APPEND ' ls_layo-zebra = ''X''.' TO code.
APPEND ' ls_layo-window_titlebar = p_heading.' TO code.
APPEND ' ls_layo-box_tabname = p_it_name.' TO code.
APPEND '' TO code.
APPEND
' stru_ref ?= cl_abap_structdescr=>describe_by_data( <fs_line> ).' TO
code.
APPEND '' TO code.
APPEND ' comp_tab = stru_ref->components.' TO code.
APPEND '' TO code.
LOOP AT tblcol_tab INTO tblcol_lin.
CONCATENATE ' tblcol_lin-aggname = '''
tblcol_lin-aggname '''.' INTO mystring.
APPEND mystring TO code.
CONCATENATE ' tblcol_lin-tabname = '''
tblcol_lin-tabname '''.' INTO mystring.
APPEND mystring TO code.
CONCATENATE ' tblcol_lin-fieldname = '''
tblcol_lin-fieldname '''.' INTO mystring.
APPEND mystring TO code.
APPEND ' APPEND tblcol_lin TO tblcol_tab.' TO code.
ENDLOOP.
APPEND '' TO code.
APPEND ' LOOP AT comp_tab INTO one_comp.' TO code.
APPEND ' CLEAR wa_fcat.' TO code.
APPEND ' idx = idx + 1.' TO code.
APPEND ' READ TABLE tblcol_tab INDEX idx INTO tblcol_lin.'
TO code.
APPEND ' wa_fcat-tabname = p_it_name.' TO code.
APPEND ' wa_fcat-fieldname = one_comp-name.' TO code.
APPEND '' TO code.
APPEND '* IF tblcol_lin-aggname NS ''count''.' TO code.
APPEND ' wa_fcat-ref_tabname = tblcol_lin-tabname.' TO code.
APPEND ' wa_fcat-ref_fieldname = tblcol_lin-fieldname.'
TO code.
APPEND '* ENDIF.' TO code.
APPEND '' TO code.
APPEND ' APPEND wa_fcat TO lt_fcat.' TO code.
APPEND ' ENDLOOP.' TO code.
APPEND '' TO code.
APPEND ' CALL FUNCTION ''REUSE_ALV_GRID_DISPLAY''' TO code.
APPEND ' EXPORTING' TO code.
APPEND ' is_layout = ls_layo' TO code.
APPEND ' it_fieldcat = lt_fcat' TO code.
APPEND ' TABLES' TO code.
APPEND ' t_outtab = <fs_table>.' TO code.
APPEND '' TO code.
APPEND 'ENDFORM. "ZJNC_DUMP_LIST' TO code.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = 'c:\jnc.abap'
TABLES
data_tab = code.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
GENERATE SUBROUTINE POOL code NAME prog
MESSAGE msg
LINE lin
WORD wrd
OFFSET off
SHORTDUMP-ID sid.
IF sy-subrc = 0.
PERFORM dosql IN PROGRAM (prog).
IF sy-subrc <0>execute( ).
CATCH ex_y4s INTO ex.
msg = ex->get_text( ).
MESSAGE msg TYPE 'E'.
ENDTRY.
ENDMETHOD. "execute
METHOD exit.
* Destroy Control.
IF NOT g_editor IS INITIAL.
CALL METHOD g_editor->free
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'g_editor->free Failed'
txt1 = 'Exiting Program'.
LEAVE PROGRAM.
ENDIF.
* free ABAP object also
FREE g_editor.
ENDIF.
* destroy container
IF NOT g_editor_container IS INITIAL.
CALL METHOD g_editor_container->free
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <0>flush
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'cl_gui_cfw=>flush Failed'
txt1 = 'Exiting Program'.
LEAVE PROGRAM.
ENDIF.
LEAVE PROGRAM.
ENDMETHOD. "exit
METHOD init.
IF g_editor IS INITIAL.
* create control container
CREATE OBJECT g_editor_container
EXPORTING
container_name = 'MYEDIT'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
IF sy-subrc NE 0.
* add your handling
ENDIF.
* create calls constructor, which initializes, creats and links
* a TextEdit Control
CREATE OBJECT g_editor
EXPORTING
parent = g_editor_container
wordwrap_mode = cl_gui_textedit=>wordwrap_at_fixed_position
wordwrap_to_linebreak_mode = cl_gui_textedit=>true
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'Create Object Failed'
txt1 = 'to make TextEditor Control'.
LEAVE PROGRAM.
ENDIF.
cl_gui_textedit=>set_focus( control = g_editor ).
ENDIF. " Editor is initial
* remember: there is an automatic flush at the end of PBO!
ENDMETHOD. "init
METHOD setquery.
* retrieve table from control
CLEAR g_mytable.
CALL METHOD g_editor->get_text_as_r3table
IMPORTING
table = g_mytable
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'Get_Text_As_R3Table Failed'
txt1 = 'Unable to Store SQL'.
LEAVE PROGRAM.
ENDIF.
* if you would like to work with the table contents
* perform a explicit flush hereallthough the method
* flush╬кs internally (at least up to release 4.6D).
* The reason: don't rely on internal flushes of control
* wrappers. These might vanish in the future leading to a
* malfunction of your transaction. The additional flush here
* does no harm. The autmation queue is empty and NO additional
* roundtrip to the frontend will be triggered.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = g_repid
txt2 = 'cl_gui_cfw=>flush Failed'
txt1 = 'Exiting Program'.
LEAVE PROGRAM.
ENDIF.
TRY.
q->init( g_mytable ).
CATCH ex_y4s INTO ex.
msg = ex->get_text( ).
MESSAGE msg TYPE 'E'.
ENDTRY.
ENDMETHOD. "setquery
ENDCLASS. "yes4sql IMPLEMENTATION
* necessary to flush the automation queue
CLASS cl_gui_cfw DEFINITION LOAD.
START-OF-SELECTION.
* initilize local variable with sy-repid, since sy-repid doesn't work
* as parameter directly.
g_repid = sy-repid.
CREATE OBJECT y4s.
CALL SCREEN 100.
************************************************************************
* P B O
************************************************************************
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'TITLEYES4SQL'.
y4s->init( ).
ENDMODULE. " PBO
************************************************************************
* P A I
************************************************************************
MODULE pai INPUT.
PERFORM user_command.
ENDMODULE. " PAI
************************************************************************
* F O R M S
************************************************************************
*&---------------------------------------------------------------------*
*& Form user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM user_command.
CASE g_ok_code.
WHEN 'EXIT'.
y4s->exit( ).
WHEN 'EXEC'.
y4s->setquery( ).
y4s->execute( ).
ENDCASE.
CLEAR g_ok_code.
ENDFORM. "user_command
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.