Oracle 函数 “判断数据表中不存在的数据,才允许通过”
1 create or replace function mca_detail_material_val(p_material_code VARCHAR2, --实参 2 p_material_name VARCHAR2, 3 p_mca_no VARCHAR2 4 ) 5 RETURN varchar2 AS 6 7 --物料编码/名称的验证,数据表(MATERIAL)中 不存在 该物料编码/名称,允许通过,填其他值都不允许通过 8 9 v_count1 NUMBER; -- 形参 10 v_count2 NUMBER; 11 v_count3 NUMBER; 12 v_count4 NUMBER; 13 14 CURSOR get_wl_material_code1 IS 15 SELECT count(m.material_code) -- count()函数:查询出来的记录条数(记录数) 16 FROM MATERIAL m 17 WHERE m.material_code = p_material_code; 18 19 CURSOR get_wl_material_code2 IS 20 SELECT count(material_code) 21 FROM mca_detail m 22 WHERE m.material_code = p_material_code AND m.mca_no <> p_mca_no; 23 -- m.mca_no <> p_mca_no:在编辑时,排除此单号(不做比较)---物料编码 24 CURSOR get_wl_material_name1 IS 25 SELECT count(m.material_name) 26 FROM MATERIAL m 27 WHERE m.material_name = p_material_name; 28 29 CURSOR get_wl_material_name2 IS 30 SELECT count(m.material_name) 31 FROM mca_detail m 32 WHERE m.material_name = p_material_name AND m.mca_no <> p_mca_no; 33 BEGIN -- m.mca_no <> p_mca_no:在编辑时,排除此单号(不做比较)---物料名称 34 OPEN get_wl_material_code1; 35 FETCH get_wl_material_code1 INTO v_count1; 36 CLOSE get_wl_material_code1; 37 38 OPEN get_wl_material_code2; 39 FETCH get_wl_material_code2 INTO v_count3; 40 CLOSE get_wl_material_code2; 41 42 OPEN get_wl_material_name1; 43 FETCH get_wl_material_name1 INTO v_count2; 44 CLOSE get_wl_material_name1; 45 46 OPEN get_wl_material_name2; 47 FETCH get_wl_material_name2 INTO v_count4; 48 CLOSE get_wl_material_name2; 49 50 IF v_count1+v_count3 <> 0 THEN 51 RETURN '4823272'; -- 错误信息码(信息码表) 该信息码对应的内容:物料编码已存在,不允许出现相同值,请确认该编码的真确性! 52 ELSIF v_count2+v_count4 <> 0 THEN 53 RETURN '4823273'; -- 错误信息码(信息码表) 该信息码对应的内容:物料名称已存在,不允许出现相同值,请确认该编码的真确性! 54 END IF; 55 56 RETURN '0'; 57 58 end mca_detail_material_val;
注:如需测试该函数,请复制到Oracle数据库中,右击函数名“mca_detail_material_val”,选择“test” 进行测试,测试时记得把相应的表名及字段换成自己建立的
原创作者:DSHORE 出处:http://www.cnblogs.com/dshore123/ 欢迎转载,转载务必说明出处。(如果本文对你有用,可以点击一下右下角的 推荐,谢谢!) |