[ABAP] NATIVE SQL


๐Ÿ‘ OPEN SQL๋งŒ์œผ๋กœ๋Š” ์›ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์“ธ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, NATIVE SQL์„ ์‚ฌ์šฉํ•ด SELECT ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ๐Ÿ‘


TYPES: BEGIN OF gty_s_test,
    f1 TYPE ~,
    f2 TYPE ~,
    ...      ,
    END OF gty_s_test,
    gty_t_test TYPE TABLE OF gty_s_test.

DATA: gt_test   TYPE TABLE OF gty_s_test WITH HEADER LINE.
"or
DATA: gt_test   TYPE gty_t_test WITH HEADER LINE.

DATA: gt_test2  TYPE TABLE OF gty_s_test.

SELECT ํ•  ํ•„๋“œ๋ฅผ TYPES๋ฌธ์œผ๋กœ ์„ ์–ธํ•˜๊ณ  ํ…Œ์ด๋ธ”๋„ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ.



๐ŸŒ EXEC SQL PERFORMING โ€ฆ


์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ PERFORM ๊ตฌ๋ฌธ์œผ๋กœ Subroutine์„ ํ˜ธ์ถœํ•ด FORM ๊ตฌ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๋“ฏ์ด PERFORMING/FORM ๊ตฌ๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ.

EXEC SQL PERFORMING append_data.
    SELECT 
        f1, f2, ...
    INTO :gt_test
    FROM ...
ENDEXEC.

FORM append_data.
    APPEND gt_test.
    CLEAR gt_test.
ENDFORM.

ํ…Œ์ด๋ธ”์— ๋ฐ”๋กœ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ ,

EXEC SQL PERFORMING append_data.
    SELECT 
        f1, f2, ...
    INTO :gs_test
    FROM ...
ENDEXEC.

FORM append_data.
    APPEND gs_test TO gt_test.
    CLEAR gs_test.
ENDFORM.

์ŠคํŠธ๋Ÿญ์ณ์— ๋„ฃ๊ณ  ํ…Œ์ด๋ธ”์— append ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.



๐ŸŒ EXEC SQL. OPEN dbcur FOR โ€ฆ


DATA: ls_test TYPE gty_s_test.

EXEC SQL.
    OPEN dbcur FOR
    SELECT
        f1, f2, ...
    FROM ...
END EXEC.

DO.
    EXEC SQL.
        FETCH NEXT dbcur INTO   :ls_test-f1,
                                :ls_test-f2,
                                ...
    END EXEC.

    IF sy-subrc EQ 0.
        APPEND ls_test TO gt_test2.
        CLEAR ls_test.
    ELSE.
        EXIT.
    ENDIF.
ENDDO.

EXEC SQL.
    CLOSE dbcur
ENDEXEC.

์ด๊ฑด ์ด๋ฒˆ์— ์ฒ˜์Œ ์•Œ๊ฒŒ ๋œ ๋ฐฉ๋ฒ•. dbcur์ด๋ผ๋Š” database cursur๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ , ์„ฑ๊ณต ์‹œ append.


๐Ÿ’ฅ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋ฅผ NATIVE SQL ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋ณ€์ˆ˜ ์•ž์— โ€œ : โ€œ ๋„ฃ์–ด์ฃผ๊ธฐ!!
๐Ÿ’ฅ BETWEEN ๋“ฑ ์‚ฌ์šฉ ์‹œ ๋”ฐ์˜ดํ‘œ ์—†์ด ย ย  :๋ณ€์ˆ˜๋ช… ย ย  ์œผ๋กœ๋งŒ ์“ฐ๊ธฐ!! (โ€œโ€ ์ผ๋‹ค๊ฐ€ ์™œ์•ˆ๋˜์ง€ n์‹œ๊ฐ„ ํ•จใ…‹)

Categories:

SAP/ABAP