[ Programing ]/Database

[PL-SQL] IF문 사용법

Mister_Q 2011. 7. 25. 18:11
- IF 문 

IF 조건_1 THEN
   처리문_1
ELSEIF 조건_2 THEN
   처리문_2
ELSE
   처리문_3
END IF;
 


해당 파일은 PL-SQL if 문으로 해당 아이템 인덱스를 삭제하는 프로시져 이다.
목적은 첫번째 인자에 있는 아이템을 삭제 한다.

-- Function: fn_delete_itemlist(integer)
-- DROP FUNCTION fn_delete_itemlist(integer);

CREATE OR REPLACE FUNCTION fn_delete_itemlist(integer)
   RETURNS integer AS
$BODY$
DECLARE
        _eventid     ALIAS for $1;
BEGIN

        DELETE FROM tb_event_list WHERE eventid = _eventid;

        IF NOT FOUND THEN
             RETURN 1;
        END IF;

        RETURN 0;
END;
$BODY$
   LANGUAGE plpgsql VOLATILE
   COST 100;
ALTER FUNCTION fn_delete_itemlist(integer) OWNER TO admin;



설명: 

CREATE OR REPLACE FUNCTION fn_delete_itemlist(integer)
     프로시져 시작


-- 
     주석이다.


RETURNS integer AS
     integer로 데이터를 반환 한다.


_eventid     ALIAS for $1;
     ALIAS for $1는 fn_delete_item_list(integer)의 인자를 가리키며, 인자를 대입한다.


DELETE FROM tb_item_list WHERE itemindex = _itemid;
     대입한 _itemid를 delete 쿼리에 조건으로 사용하여 해당 item index를 삭제 한다.


IF NOT FOUND THEN
     RETURN 1;
END IF;
     만약 NOT FOUND 오류가 났다면 RETURN 1 을 반환한다.