如果我将输入设为06/2007
,那么它可以正常工作。
给予2007/06
显示无效输入 - 很好
NULL
'没有'意味着它提供了强制性输入。但它给出了无效的输入,这是错误的。我该如何处理这两个条件?
PROCEDURE validateDate(v_InputDate IN VARCHAR2) is
ErrorMSG VARCHAR2(1000);
v_tmpDate varchar2(20);
g_dateInvalid EXCEPTION;
g_dateError EXCEPTION;
g_ErrorCode NUMBER;
BEGIN
dbms_output.put_line('Input date is' || v_InputDate);
BEGIN
IF v_InputDate IS NULL THEN
RAISE g_dateError;
ELSIF v_InputDate IS NOT NULL THEN
v_tmpDate := to_date(v_InputDate,
'MM/YYYY');
dbms_output.put_line('Converted date is' || v_tmpDate);
END IF;
EXCEPTION
WHEN OTHERS THEN
RAISE g_dateInvalid;
END;
EXCEPTION
WHEN g_dateError THEN
g_ErrorCode := -20002;
ErrorMSG := 'Mandatory input cannot be null. Enter a valid date in MMYYYY fromat';
raise_application_error(g_ErrorCode,
ErrorMSG);
WHEN g_dateInvalid THEN
g_ErrorCode := -20001;
ErrorMSG := 'Invalid DATE should be in MMYYYY format. Please verify. ';
raise_application_error(g_ErrorCode,
ErrorMSG);
END validateDate;
答案 0 :(得分:0)
我认为你现在这样做的方式比实际需要的要复杂一些。没有必要嵌套另一个块。我测试了以下内容,它对我来说很好用:
CREATE OR REPLACE PROCEDURE validateDate(
v_InputDate IN VARCHAR2
)
IS
v_tmpDate varchar2(20);
g_dateError EXCEPTION;
BEGIN
dbms_output.put_line('Input date is' || v_InputDate);
IF v_InputDate IS NULL THEN
RAISE g_dateError;
ELSE
v_tmpDate := to_date(v_InputDate, 'MM/YYYY');
dbms_output.put_line('Converted date is' || v_tmpDate);
END IF;
EXCEPTION
WHEN g_dateError THEN
raise_application_error(-20002, 'Mandatory input cannot be null. Enter a valid date in MMYYYY fromat');
WHEN OTHERS THEN
raise_application_error(-20001, 'Invalid DATE should be in MMYYYY format. Please verify. ');
END validateDate;
/
使用null测试并得到错误-20002,使用'text'测试并得到-20001。