1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| create or replace function f_createomid return varchar2 as v_omid varchar2(12); v_maxomid t_main_order.omid%type; begin v_omid:='O'||to_char(sysdate,'yyyymm'); select max(omid) into v_maxomid from t_main_order where to_char(odate,'yyyymm')=to_char(sysdate,'yyyymm'); if v_maxomid is null then v_omid:=v_omid||'00001'; else v_omid:=v_omid||trim(to_char(to_number(substr(v_maxomid,8,5))+1,'00000')); end if; return v_omid; exception when others then return null; end; / show error;
select f_createomid from dual;
添加订单明细数据:
create or replace procedure p_insertorderitems(i_omid in t_order_items.omid%type, i_gid in t_order_items.gid%type, i_onum in number default 1,o_result out number) as v_count number:=0; begin select count(omid) into v_count from t_main_order where trim(omid)=trim(i_omid); if v_count<1 then o_result:=-2; return; end if; select count(gid) into v_count from t_goods where trim(gid)=trim(i_gid); if v_count<1 then o_result:=-2; return; end if; insert into t_order_items values(i_omid,i_gid,f_returngoodprice(i_gid),i_onum,null); o_result:=1; commit; exception when others then o_result:=sqlcode; rollback; end; / show error;
declare v_result number; begin p_insertorderitems('O20170800001','g0006',10,o_result=>v_result); if v_result=1 then dbms_output.put_line('添加成功'); elsif v_result=-2 then dbms_output.put_line('数据不存在'); else dbms_output.put_line(v_result); end if; end;
create or replace function f_returngoodprice(good_id varchar2) return number as v_goodprice t_goods.gprice%type; begin select nvl(gprice,0)*nvl(gdiscount,1) into v_goodprice from t_goods where upper(trim(gid))=upper(good_id); return v_goodprice; exception when others then return 0; end f_returngoodprice;
|