๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง‘‍๐Ÿ’ผ SAP/ABAP

[SAP ABAP] OPEN SQL ๋ฐ์ดํ„ฐ ์ฝ๋Š” ๋ฐฉ๋ฒ•(SELECT, INTO, FROM ๋“ฑ๋“ฑ)

by Jay Din 2023. 11. 3.
728x90
๋ฐ˜์‘ํ˜•

ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์˜ค๋Š” ๊ตฌ๋ฌธ 

Reading Data ๋ช…๋ น์–ด

๊ตฌ๋ฌธ ๊ธฐ๋Šฅ
SELECT <result> SELECT ๊ตฌ๋ฌธ์€ ์กฐํšŒํ•˜๊ณ ์ž ํ•˜๋Š” ํ…Œ์ด๋ธ” ํ•„๋“œ๋ช…์„ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ•œ ๊ฑด ๋˜๋Š” ์—ฌ๋Ÿฌ ๋ผ์ธ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
INTO <target> SELECT์—์„œ ์ฝ์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋ฉฐ, ์ด ๋ณ€์ˆ˜๋ฅผ ABAP ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
FROM <source> FROM ๊ตฌ๋ฌธ์€ SELECT ํ•  ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.
์œ„์น˜๋Š” INTO ์ด์ „/์ดํ›„ ๋ชจ๋‘ ๋‘˜ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
WHERE <cond> ์กฐํšŒํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
GROUP BY <fields> ์—ฌ๋Ÿฌ ๋ผ์ธ์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋ฃน์œผ๋กœ ์ง€์ •ํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค๋ฉด, ๊ทธ๋ฃน๋ณ„ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ๊ฒฝ์šฐ FROUP BY๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
HAVING <cond> HAVING์€ GROUP์˜ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๋Š” WHERE ๊ตฌ๋ฌธ์ž…๋‹ˆ๋‹ค.
ORDER BY <fields> ์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌ(sort)ํ•ฉ๋‹ˆ๋‹ค.

 

SELECT ๊ตฌ๋ฌธ

SELECT ๊ตฌ๋ฌธ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์˜ต๋‹ˆ๋‹ค.

SELECT ๊ตฌ๋ฌธ์€ ๋‘ ๊ฐ€์ง€ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค.

<lines>๋Š” ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ ๋ผ์ธ์„ ์„ ํƒํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ ํ•˜๋‚˜์˜ ๋ผ์ธ์ผ ๋•Œ๋Š” SINGLE ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

<columns>๋Š” ํ…Œ์ด๋ธ” ์นผ๋Ÿผ์„ ๊ธฐ์ˆ ํ•ฉ๋‹ˆ๋‹ค.

SELECT <lines> <columns> ...

 

Single Line

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ•˜๋‚˜์˜ ๋ผ์ธ ๊ฐ’์„ ์ฝ์–ด์˜ค๊ณ ์ž ํ•  ๊ฒฝ์šฐ SINGLE์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SINGLE๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๊ฑด๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์กฐ๊ฑด์„ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, WHERE ์กฐ๊ฑด์— ์œ ์ผํ•œ ํ‚ท๊ฐ’์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ๋งŒ์•ฝ์— WHERE๊ตฌ๋ฌธ์ด ์ž˜๋ชป๋˜์–ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ผ์ธ์„ ์ฝ์–ด์˜ค๋ฉด ์ž„์˜์˜ ๋ผ์ธ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ์นผ๋Ÿผ์„ ์ฝ์„ ๋•Œ๋Š” *์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT SINGLE <cols> ...WHERE

 

Several Lines

์—ฌ๋Ÿฌ ๋ผ์ธ์„ ์กฐํšŒํ•  ๋•Œ๋Š” SELECT ๊ฒฐ๊ณผ๊ฐ€ ๋‚ด๋ถ€ ํ…Œ์ด๋ธ”์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์„ ABAP์—์„œ๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”(Internal Table)์ด๋ผ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

DISTINCT๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์ค‘๋ณต๋œ ๊ฐ’์„ ์ œ์™ธํ•ฉ๋‹ˆ๋‹ค.

์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”(Internal Table)
์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์€ ABAP ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์„ฑ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค.
SELECT [DISTINCT] <cols> ...WHERE

 

INTO ๊ตฌ๋ฌธ์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ์ด ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ์•„๋‹ˆ๋ผ ํ•„๋“œ ๋˜๋Š” Work Area(๊ตฌ์กฐ์ฒด)์ผ ๋•Œ์—๋Š” ๋งˆ์ง€๋ง‰์— ENDSELECT๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ตฌ๋ฌธ์€ ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ฝ์–ด์„œ ๊ตฌ์กฐ์— ์‚ฝ์ž…ํ•˜๊ณ , ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ๋ชจ๋‘ ์ฝ์–ด ์˜ฌ ๋•Œ๊นŒ์ง€ LOOP๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

SELECT [DISTINCT] <cols> ... WHERE ...
	...
ENDSELECT.

 

 

์•„๋ž˜ 1๋ฒˆ์€ ๊ตฌ์กฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ SELECT ~ ENDSELECT ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์˜€๊ณ ,

2๋ฒˆ์€ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ํ•œ ๋ฒˆ์— ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ENDSELECT ๊ตฌ๋ฌธ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ํ”„๋กœ๊ทธ๋žจ์€ SELECT ~ ENDSELECT๊ตฌ๋ฌธ์ด ๋ฐ˜๋ณต ์ˆ˜ํ–‰๋  ๋•Œ๋งˆ๋‹ค Application Server์™€ Database ๊ฐ„์˜ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ, ํ•œ ๋ฒˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ์ €์žฅํ•˜๋Š” ์˜ค๋ฅธ์ชฝ ํ”„๋กœ๊ทธ๋žจ๋ณด๋‹ค ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

* 1

DATA: gt_itab TYPE STANDARD
		TABLE OF sflight,
        gs_wa TYPE sflight.
        
SELECT * INTO gs_wa
FROM sflight
WHERE carrid EQ 'AA'.
	WRITE : / gs_wa-carrid,
    	gs_wa-connid.
ENDSELECT.
* 2

DATA: gt_itab TYPE STANDARD
	TABLE OF sflight,
    gs_wa TYPE sflight.
    
SELECT * INTO TABLE gt_itab
FROM sflight
WHERE carrid EQ 'AA'.

LOOP AT gt_itab iNTO gs_wa.
	WRITE: / gs_wa-carrid,
    	gs_wa-connid.
ENDLOOP.

 

AS(Alias)

AS ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ, ์นผ๋Ÿผ ๋ช…์— ๋ณ„๋ช…์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

SELECT <cols> [AS <alias>]...

 

๋™์ ์ธ SELECT ๊ตฌ๋ฌธ

SELECT ๊ตฌ๋ฌธ์˜ ์นผ๋Ÿผ์„ ๋™์ ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋™์  ๊ตฌ๋ฌธ์„ ์ €์žฅํ•˜๋Š” ๊ตฌ์กฐ์ฒด GS_LINE์€ ์ตœ๋Œ€ 72์ž๊นŒ์ง€ ๊ฐ€๋Šฅํ•˜๋ฉฐ CHAR ํƒ€์ž…์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

GS_LINE๊ตฌ์กฐ์ฒด๊ฐ€ null์ด๋ฉด *์™€ ๊ฐ™์€ ๊ตฌ๋ฌธ์ด ๋ฉ๋‹ˆ๋‹ค.

SELECT <lines> (<itab>) ...

 

728x90

INTO ๊ตฌ๋ฌธ

SELEcT ๊ตฌ๋ฌธ์—์„œ ์กฐํšŒํ•œ ๊ฒฐ๊ด๊ฐ’์„ ๋ณ€์ˆ˜(Target area)์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

๊ตฌ์กฐ์ฒด (WA; Work Area)

์—ฌ๋Ÿฌ ์นผ๋Ÿผ์˜ ํ•œ ๋ผ์ธ๋งŒ ์กฐํšŒํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ์—๋Š” Work Area(๋ณ€์ˆ˜, ๊ตฌ์กฐ์ฒด)์— ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

์• ์Šคํ„ฐ๋ฆฌ์Šคํฌ(*) ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ „์ฒด ์นผ๋Ÿผ์˜ ๊ฐ’์„ ์ฝ์–ด์˜ค๋ฉฐ, CORRESPONDING FIELDS OF ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ๋ฒˆ์— Work area์˜ ๋™์ผ ํ•„๋“œ๋ช…์— ๊ฐ’์„ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

* ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ฐœ๋ณ„ ํ•„๋“œ๋ฅผ SELECT ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋น„ํšจ์œจ์ ์ž…๋‹ˆ๋‹ค. ํŠนํžˆ, SAP ์ฃผ์š” ํ…Œ์ด๋ธ”์€ ๋งŽ์€ ํ•„๋“œ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, ์ด ๊ฒฝ์šฐ์—๋Š” SELECT *๊ตฌ๋ฌธ์€ ์„ฑ๋Šฅ์— ์•„์ฃผ ํฐ ์˜ํ–ฅ์„ ๋ฏธ์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์„œ ์ด๊ฒƒ์€ ํ…Œ์ด๋ธ”์˜ ํ•œ ๋ผ์ธ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ DB์—์„œ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ผ์ •ํ•œ ํฌ๊ธฐ๋งŒํผ ์ž˜๋ผ์„œ SELECT ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ SELECT *๊ตฌ๋ฌธ์˜ ์‚ฌ์šฉ์„ ์‚ผ๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค.

SELECT ...INTO [CORRESPONDING FIELDS OF] <wa>.

 

์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”(Internal Table)

์—ฌ๋Ÿฌ ๋ผ์ธ์„ ์กฐํšŒํ•  ๊ฒฝ์šฐ์—๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

APPENDING์€ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€๋กœ INSERTํ•˜๊ณ , INTO๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•œ ๋‹ค์Œ INSERTํ•ฉ๋‹ˆ๋‹ค.

SELECT ...INTO|APPENDING [CORRESPONDING FIELDS OF]
					TABLE <itab>
                    [PACKAGE SIZE <n>] ...

PACKAGE SIZE๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ๋ช‡ ๊ฐœ์˜ ๋ผ์ธ์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ธ๊ฐ€๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

PACKAGE SIZE 5๋ผ ์„ค์ •ํ•˜๋ฉด 5๊ฐœ์˜ ๊ฐ’์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ์–ด์™€ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด ๊ฒฝ์šฐ์—๋Š” ENDSELECT๊ตฌ๋ฌธ์„ ๋ฐ˜๋“œ์‹œ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT INTO CORRESPOND FIELDS OF TABLE ITAB
	PACKAGE SIZE 5
    FROM SPFLI.
ENDSELECT.

 

Single Field

ํ…Œ์ด๋ธ”์˜ ๊ฐœ๋ณ„ ์นผ๋Ÿผ์„ ์กฐํšŒํ•˜๊ฑฐ๋‚˜ Aggregate ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ๋‹ค์Œ ๊ตฌ๋ฌธ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

INTO ๊ตฌ๋ฌธ ๋‹ค์Œ์— ๋‘ ๊ฐœ ์ด์ƒ์˜ Target์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ด„ํ˜ธ์™€ ๋ณ€์ˆ˜๋ช…์„ ๋ถ™์—ฌ์จ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ณต๋ฐฑ์ด ์กด์žฌํ•˜๋ฉด Syntax Error๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

SELECT ... INTO (F1, f2, ...) ...

SELECT ๊ตฌ๋ฌธ์—์„œ 2๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT carrid connid INTO (gv_carrid, gv_connid) FROM SFLIGHT.

 

FROM ๊ตฌ๋ฌธ

FROM ๊ตฌ๋ฌธ์€ ๋ฐ์ดํ„ฐ๋ฅผ SELECTํ•  ๋Œ€์ƒ ํ…Œ์ด๋ธ”(๋˜๋Š” ๋ทฐ)์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

FROM ๊ตฌ๋ฌธ ๋‹ค์Œ์—๋Š” ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ JOIN ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Alias๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ” ๋ช…์— ๋ณ„๋ช…์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ๋™์ ์œผ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

FROM๊ตฌ๋ฌธ์€ Table์„ ์ •์˜ํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์„ ์ปจํŠธ๋กคํ•˜๋Š” ๋ถ€๋ถ„(Option)์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค.

SELECT ... FROM table option ...

 

FROM ๊ตฌ๋ฌธ์˜ Option

๊ตฌ๋ฌธ ์„ค๋ช…
CLIENT SPECIFIED ์ž๋™ client ์„ค์ •์„ ํ•ด์ œํ•œ๋‹ค.
BYPASSING BUFFER SAP Local Buffer์—์„œ ๊ฐ’์„ ์ฝ์ง€ ์•Š๋Š”๋‹ค.
ํ…Œ์ด๋ธ”์ด Buffering์ด ์„ค์ •๋˜์–ด ์žˆ๋”๋ผ๋„ ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์—์„œ SELECT๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.
UP TO n ROWS SELECT์˜ row ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•œ๋‹ค.
์กฐํšŒ ์กฐ๊ฑด์— ๋‚ ์งœ๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์‹ค์ˆ˜๋กœ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ฑ๋Šฅ ์ €ํ•˜๋ฅผ ์˜ˆ๋ฐฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋ฐ˜์‘ํ˜•

์ •์ ์ธ TABLE ์„ ํƒ

ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์„ ์ •์ ์œผ๋กœ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Alias๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด ๊ฒฝ์šฐ์—๋Š” ๋ฐ์ด๋ธ” ๋ช…์„ SELECT ๊ตฌ๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

SELECT ... FROM <dbtab> [AS <alias>] <options>

 

๋™์ ์ธ TABLE ์„ ํƒ

ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ๋™์ ์œผ๋กœ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ํ…Œ์ด๋ธ” ์ด๋ฆ„์€ ๋ฐ˜๋“œ์‹œ ๋Œ€๋ฌธ์ž๋กœ ์ง€์ •ํ•˜์—ฌ์•ผ ํ•˜๋ฉฐ, ABAP Dictionary์— ์กด์žฌํ•˜๋Š” ์ด๋ฆ„์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

SELECT ... FROM (dbtab).

 

 

JOIN ๊ตฌ๋ฌธ

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ” ๊ฐ’์„ ๋™์‹œ์— ์ฝ์–ด ์˜ฌ ๊ฒฝ์šฐ JOIN์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ Primary Key(์ดํ›„ PK๋กœ ์‚ฌ์šฉ)์™€ Foreign Key(์ดํ›„ FK๋กœ ์‚ฌ์šฉ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JOINํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด์ง€๋งŒ ๋•Œ๋กœ๋Š” ๋…ผ๋ฆฌ์ ์ธ ๊ฐ’๋“ค์˜ ์—ฐ๊ด€์œผ๋กœ JOINํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ์—ฐ๊ฒฐ ์กฐ๊ฑด์€ ON ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. JOIN์— ์‚ฌ์šฉ๋˜๋Š” ํ•„๋“œ๊ฐ€ ์ธ๋ฑ์Šค์— ์กด์žฌํ•  ๋•Œ ๋น ๋ฅธ ์„ฑ๋Šฅ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค.

SELECT ...FROM <tab> [INNER] JOIN <dbtab> [AS <alias>]
		ON <cond> options> ...

 

 

์ฃผ์˜ ์‚ฌํ•ญ

ABAP ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ํ…Œ์ด๋ธ” ๊ฐ„์˜ JOIN์„ ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‚˜์„œ LOOP ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ, ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ SELECTํ•˜์—ฌ ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ” ๋‚ด์šฉ์„ MODIFYํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.

SELECT field11 INTO gt_itab FROM table1.
LOOP AT gt_itab.
	SELECT field2 into gt_itab-field2 FROM table2
    	WHERE field1 = gt_itab-field1.
    MODIFY gt_itab.
ENDLOOP.

์ด๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ž…์žฅ์—์„œ ๋ณด๋ฉด ์•„์ฃผ ์ข‹์ง€ ์•Š์€ ์Šต๊ด€์ž…๋‹ˆ๋‹ค.

ํ”„๋กœ๊ทธ๋žจ์„ ๋Š๋ฆฌ๊ฒŒ ๋งŒ๋“œ๋Š” ์ฃผ์š” ์›์ธ ์ค‘์˜ ํ•˜๋‚˜๋Š” ํ•˜๋“œ๋””์Šคํฌ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์— ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๊ฒฝ์šฐ๋Š” LOOP๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜๋ณต์ ์œผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋ฌผ๋ก , ABAP ์–ธ์–ด์—๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ธํ„ฐ๋„ ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•˜๊ณ , OPEN SQL์˜ ์ œํ•œ๋œ ๊ธฐ๋Šฅ ๋•Œ๋ฌธ์— ์–ด์ฉ” ์ˆ˜ ์—†์ด LOOP์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋ฐ–์— ์—†์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ JOIN์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ผ๋ฉด, ๋  ์ˆ˜ ์žˆ์œผ๋ฉด JOIN์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•ฉ๋‹ˆ๋‹ค.

SELECT a~field1 b~field2 INTO gt_itab
FROM table1 as a, table2 as b
	on a~field1 = b~field1.

 

INNER JOIN๊ณผ OUTER JOIN

JOIN์˜ ์ข…๋ฅ˜์—๋Š” ํฌ๊ฒŒ INNER JOIN๊ณผ OUTER JOIN ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ ,OUTER JOIN์€ ๋‹ค์Œ ๊ตฌ๋ฌธ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT ... FROM <tab> LEFT [OUTER] JOIN <dbtab>
		[AS <alias>] ON ,cond>
<options>

 

 

 


์ฐธ๊ณ 

EASY ABAP2.0 ๋„์„œ

 

728x90
๋ฐ˜์‘ํ˜•