CREATE OR REPLACE FUNCTION FN_CANCEL_WHITE(BarCode IN VARCHAR2,
CREATEDATE IN DATE)
RETURN VARCHAR2 IS
ABEXISTS VARCHAR2(2);
BEGIN
SELECT CASE
WHEN COUNT(LOT_2) = 0 THEN
'N'
ELSE
'Y'
END
INTO ABEXISTS
FROM (SELECT H.LOT_2
FROM ZZ_CANCEL_OPERATION_SUMMARY H
WHERE H.LOT_2=BarCode
AND H.CREATE_DATE > CREATEDATE
AND H.APPLICATION_NAME = 'PartCutRetreatWorkData'
ORDER BY CREATE_DATE ASC)
WHERE ROWNUM = 1;
RETURN ABEXISTS;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
END;
调用的SQL
SQL code
SELECT fn_cancel_white(Z.LOT2,Z.CREATE_DATE) FLAG FROM ZZ_WHITEPMSTORAGE_SUMMARY Z
WHERE NOT EXISTS (SELECT *
FROM ZZ_CANCEL_OPERATION_SUMMARY H
WHERE H.LOT_2= Z.LOT2
AND H.CREATE_DATE > Z.CREATE_DATE
AND H.APPLICATION_NAME = 'PartCutRetreatWorkData')
如果你是批量任务,要判断ZZ_WHITEPMSTORAGE_SUMMARY Z中多数记录的状态,建议你用语句的方式,能减少sql引起于plsql引擎之间切换,而且func中外部语句执行计划不能连接优化. 用not 效率比较低,可以换成关联(你可以用left jion) SELECT decode(nvl(H.LOT_2,0),0,'N','Y') flag 示例 FROM ZZ_WHITEPMSTORAGE_SUMMARY Z, ZZ_CANCEL_OPERATION_SUMMARY H WHERE Z.LOT2 =H.LOT_2(+) AND Z.CREATE_DATE < H.CREATE_DATE(+) AND H.APPLICATION_NAME(+) = 'PartCutRetreatWorkData'