메뉴 건너뛰기

SAP 한국 커뮤니티

Open SQL 문법 정리

SAP질 중 2008.08.20 17:13 조회 수 : 10822 추천:6

SQL


-          Select : 데이타 베이스 테이블 정보를 읽음.


l       Into wa – select문에 기술한 컬럼의 값을 wa에 이동


l       Into corresponding fields of wa여러컬럼 및 변수명의 값이 동일한 변수명으로 이동된다.


l       Into(f1.....) – into wa와 동일 선택한 컬럼의 개수가 복수일 경우에 사용.


l       Into table itab선택된 데이타를 itab에 이동


l       Into corrensponding fields of table itab. – into table wa 와 같은 의미이며 값을 이동시킬때 2번과 같은 원칙


l       Appending table itab – into table itab과 동일 이전 데이터에 더 추가된다.


l       Appending corresponding fields of table itab – into corrensponding .... 과 동일하지만 이전의 데이터에 더 추가.


Ex)


Select * from mara where werks = ‘1101’


 If sy-subrc EQ 0.


   Exit.


 Endif.


Endselect.


*mara table에서 werks 1101인 데이터를 가져와라.


*sy-subrc EQ 0 : 데이타를 성공적으로 가져오면 exit 한다.


*첫번째 레코드만 가져오게 됨.


 


Tip: Up to n rows


Select * from mara up to 1 rows “select row 갯수제한


Where werks = 1101


Endselect.


 


Single line : 하나의 라인 값을 읽어오고자 한 경우 single사용


                 Where조건에 unique key값이 추가되어야 한다.


 


Data : itab type standard table of spfli, “ intarnal table [itab]


      Wa like line of itab.


Data : line(72) type c, “ c type으로 72자리 line선언


List like table of line(72).


Line = ‘cityfrom cityto’.


Append line to list.


Select distict (list)


              Into corresponding fields of table itab


              “into intarnal table clear한 후 insert..


              “appending intarnal table에 추가로 insert..


      From spfli.


If sy-subrc EQ 0.


  Loop at itab into wa.


              Write : / wa-cityfrom , wa-cityto. “ display


Endloop.


             Endif.


 


             Data name(10 type c value ‘SCARR’.


             Select * into wa


                           From (name) client specifued 타 클라이언트의 데이타를 가져온다.


                           Where mandt = ‘000’.


                           Write : / wa-carrid ,  wa-carrname.


             Endselect.


 


             Where co1 between 1 and 10. “ 1~10까지 사이.


            


             Where col2 like ‘%ABC%’. 비교


 


             Where col3 in (‘A’,’B’,’C’). 여러조건 비교.


 


             Data : tab_spfli type table of spfli,


                   Tab_sflight type sorted table of sflight


                                        With unique key table line,


                   Wa like line of tab_sflight.


             Select carrid connid


             Into corresponding fields of table tab_spfli


             From spfli


             Where cityform = ‘NEW YORK’.


             Select carrid connid fldate


             Into corresponding fields of table tab_sflight


             From sflight


             For all entries in tab_spfli


             Where carrid = tab_spfli-carrid and


                    Connid = tab_spfli-connid.


             Loop at tab_sflight into wa.


             At new connid.


                           Write : / wa-carrid , wa-connid.


             Endat.


             Write : / wa-fldate.


             Endloop.


            


             Data : carrid type sflight-carrid,


                   Mainmum type p decimals 2,


              Maximum type p decimals 2.


             Select carrid min( price ) max( price )


             Into (carrid, minimum, maximum)


             From sflight


             Group by carrid


Having sum( seatsocc ) > 300.


                           Write : / carrid , minumum , maximum.


             Endselect.


 


             Data : begin of wa,


             Carrid type sflight-carrid,


             Connid type sflight-connid,


             Min type i,


             End of wa.


             Select carrid connid min( seatsocc ) as min


             Into corresponding fields of wa


             From sflight


             Group by carrid connid


             Order by carrid min descending. “descending 내림차순,ascending 오름차순


                           Write : / wa-carrid, wa-connid, wa-min.


             Endselect.


            


             Data : name_tab type table od scarr-carrname.


                   Name like line of name_tab.


             Select carrname


             Into table name_tab


             From scarr


             Where exists (select * from spfli 결과가 존재하면 true


                                        Where carrid = scarr~carrid


                                        And cityfrom = ‘NEW YORK’).


             Loop at name_tab into name.


                           Write : / name.


             Endloop.


            


-          Insert : 데이타베이스 테이블에 line 추가.


l       Insert [wa into | intial line into] itab [index idx]이 형태는 Standard에서만 가능, wa into를 사용하면 wa의 내용이 하나의 record가 된다.


l       Insert [wa into | initial line into] table itab모든 형태의 table에 사용가능


l       Insert lines of itab1 [from idx1] [to idx2] into itab2 [index idx3] – itab1의 내용을 itab2 에 추가, from to가 추가되면 idx1~idx2 까지가 idx3에 추가


l       Insert lines of itab1 [from idx1] [to idx2] into table itab2 - ??


l       Insert dbta -  dbta의 한라인 추가


l       Insert dbta from table itab – itab 전체라인을 dbta에 추가할 때 사용한다.


l       Insert from dbta where logexp – where 이하의 조건을 만족하는 dbta전체 라인 추가할 때


Ex)


Data : itab type hashed table of spfli


      With unique key carrid connid,


      Wa like line of itab.


Wa-carrid = ‘UA’.


Wa-connid = ‘0011’.


Wa-cityfrom = ‘new york’.


Insert wa into table itab.


Insert spfli from table itab accepting duplicate keys. 덤프에러 방지


 


-          Update : 데이타베이스 테이블 데이타수정


Tables sflight.


Data wa type sflight.


Move ‘AA’ to wa-carrid.


Move ‘0064’ to wa-connid.


Update sflight from wa.


Update sflight


Set price = ‘100’


Where carrid = ‘AZ’


And connid = ‘0011’.


If sy-subrc EQ 0.


              Write ‘update 성공’.


Endif.


 


-          Delete : 데이타베이스 테이블 데이타삭제.


l       Delete itab현재 Header에 있는 Record가 삭제.


l       Delete table itab with table key k1 = vv .... – table key로 주어진 조건을 만족하는 데이타만 itab에서 삭제.


l       Delete table itab [form wa] – wa itab의 필드명이 일치되어 데이터가 삭제.


l       Delete itab index idx – idx번째 index itab record에서 삭제된다.


l       Delete itab from idx1 to idx2 – itab idx1번째 Record에서 부터 idx2번째 record까지 삭제


l       Delete itab where logexp – where 이하의 조건을 만족하는 record가 삭제


l       Delete adjacent duplicates from itab – itab에 중복되는 데이터들이 삭제된다.


l       Delete dbta – dbta의 모든 내용을 삭제할 때 사용된다.


l       Delete dbta from table itab - itab과 같은 값을 dbta 에서 삭제할때 사용한다.


l       Delete from dbta where logexp – where 이하의 조건을 만족하는 dbta 전체라인을 삭제


 


Ex)


Tables sflight.


Data : degin of wa,


      Carrid type sflight-carrid.


      Connid type sflight-connid.


End of wa.


             Move ‘AZ’ to wa-carrid.


             Move ‘0010’ to wa-connid.


             Delete sflight from wa.


             Delete sflight.


             Delete from sflight where carrid = ‘UA’.


             If sy-subrc EQ 0.


                           Write ‘삭제 성공하였습니다.’.


             Endif.


 


-          Modify(insert + modify) : 데이타가 테이블에 존재하면 update/존재하지 않을 경우 insert


Tables sflight.


l       Modify itab [from wa] [index idx] [transporting f1...] – index를 가지는 table에서 사용,index idx를 사용하면 idx라인변경, tansporting을 사용하면 f1... field만 수정


l       Modity table itab [from wa] [transporting f1....] – 제약조건이 없다.


l       Modify itab [from wa] transporting f1.... where cond – 조건을 만족하는 record들에 대하여 변경.


Ex)


Data : itab type hashed table of spfl


      With unique key carrid connid,


      Wa like line of itab.


Wa-carrid = ‘UA’.


Wa-connid = ‘0011’.


Wa-cityfrom = ‘new york’.


Modify wa from itab.


Modify sflight from table itab .


 


-          Read : Internal table 내의 record값을 읽을 경우에 사용된다.


l       Read table itab from wa [additions] – table 내의 record 위치를 찾기 위하여 table key 값사용


l       Read table itab with table key k1=v1 ..... [additions]


l       Read table itab with key k1=v1..... [binary search] [additions]정확한 record의 위치를 선택하기 위하여 특정한 key값을 이용 복수개면 첫번째 record만 선택