CREATE OR REPLACE PROCEDURE INSERT_INOUTITEM_PROC ( p_item_key VARCHAR2 ,p_inout_flag VARCHAR2 ,p_inout_date VARCHAR2 ,p_ware_cd VARCHAR2 ,p_inout_num VARCHAR2 ) IS BEGIN IF EXISTS (SELECT 'X' FROM STOCK WHERE ITEMKEY = p_item_key AND WARECD = p_ware_cd) THEN insert into INOUTITEM values(TO_CHAR(SYSDATE, 'YYMMDD')||LPAD(TO_CHAR(inout_key.NEXTVAL),8,'0') ,p_item_key, p_inout_flag, p_inout_date, p_ware_cd, p_inout_num); IF p_inout_flag = 1 THEN --1 입고 update stock set stocknum = (select stocknum from stock where itemkey = p_item_key and warecd = P_ware_cd)+p_inout_num where itemkey = p_item_key and warecd = p_ware_Cd; ELSIF p_inout_flag = 2 THEN --2 출고 update stock set stocknum = (select stocknum from stock where itemkey = p_item_key and warecd = P_ware_cd)-p_inout_num where itemkey = p_item_key and warecd = p_ware_cd; END IF; ELSIF NOT EXISTS (SELECT 'X' FROM STOCK WHERE ITEMKEY = p_item_key AND WARECD = p_ware_cd) THEN insert into INOUTITEM values(TO_CHAR(SYSDATE, 'YYMMDD')||LPAD(TO_CHAR(inout_key.NEXTVAL),8,'0') ,p_item_key, p_inout_flag, p_inout_date, p_ware_cd, p_inout_num); insert into stock values (p_item_key, p_ware_cd, p_inout_num); END IF; EXCEPTION WHEN OTHERS THEN sys.dbms_output.put_line(SQLERRM); raise; END INSERT_INOUTITEM_PROC;
MSSQLSERVER에서는 if문에 바로 exists가 먹혔었는데 오라클에서는PLS-00240 애러가 발생해 구글링해보니 오라클의경우 커서로 처리해야한다는데 커서가 뭔지 어떻게 처리해야할지 모르겠습니다
커서 개념과 사용법좀 알려주세요 ㅠㅠ
CREATE OR REPLACE PROCEDURE INSERT_INOUTITEM_PROC ( p_item_key VARCHAR2 ,p_inout_flag VARCHAR2 ,p_inout_date VARCHAR2 ,p_ware_cd VARCHAR2 ,p_inout_num number ) IS v_count number; BEGIN SELECT COUNT(*) INTO v_count FROM STOCK WHERE ITEMKEY = p_item_key AND WARECD = p_ware_cd; IF v_count > 0 THEN insert into INOUTITEM values(TO_CHAR(SYSDATE, 'YYMMDD')||LPAD(TO_CHAR(inout_key.NEXTVAL),8,'0') ,p_item_key, p_inout_flag, p_inout_date, p_ware_cd, p_inout_num); IF p_inout_flag = 1 THEN --1 입고 update stock set stocknum = (select stocknum from stock where itemkey = p_item_key and warecd = P_ware_cd)+p_inout_num where itemkey = p_item_key and warecd = p_ware_Cd; ELSIF p_inout_flag = 2 THEN --2 출고 update stock set stocknum = (select stocknum from stock where itemkey = p_item_key and warecd = P_ware_cd)-p_inout_num where itemkey = p_item_key and warecd = p_ware_cd; END IF; ELSE insert into INOUTITEM values(TO_CHAR(SYSDATE, 'YYMMDD')||LPAD(TO_CHAR(inout_key.NEXTVAL),8,'0') ,p_item_key, p_inout_flag, p_inout_date, p_ware_cd, p_inout_num); insert into stock values (p_item_key, p_ware_cd, p_inout_num); END IF; EXCEPTION WHEN OTHERS THEN sys.dbms_output.put_line(SQLERRM); raise; END INSERT_INOUTITEM_PROC;
자문자답입니다 구루비 검색하다 위와 같이 처리하는 방법을 찾았네요 ^^;