You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决ORA-01722无效数字错误?执行存储过程报错求助

Fixing ORA-01722 Invalid Number Error in Your Stored Procedure

Let's break down why you're hitting this error and fix it right away.

The Root Cause

ORA-01722 pops up when Oracle tries to convert a string to a number and can't—which is exactly what's happening here. Let's compare your INSERT column order to the values you're passing:

Your target columns are ordered as:
"NAME", "CREATEDATE", "TAG","STATUS", "MONEY","PHOTOID","OWNID","EXPLAIN"

But in your VALUES clause, you have:
p_name,CURRENT_DATE,p_tag,'T',p_money,p_mail,(SELECT ID FROM USERS WHERE MAIL=p_mail),p_explain

See the mismatch? The 6th position should map to PHOTOID (a numeric column, since it's defined as QUESTION.PHOTOID%TYPE), but you're passing p_mail—a string value like 'cerate' from your test call. That string-to-number conversion fails, triggering the ORA-01722 error. You meant to use p_img here instead.

Corrected Stored Procedure Code

Here's the fixed version with values aligned correctly to their columns:

create or replace PROCEDURE INSERTQUESTION(
    p_name IN QUESTION.NAME%TYPE,
    p_tag IN QUESTION.TAG%TYPE,
    p_money IN QUESTION.MONEY%TYPE,
    p_explain IN QUESTION.EXPLAIN%TYPE,
    p_img IN QUESTION.PHOTOID%TYPE,
    p_mail IN USERS.MAIL%TYPE
) IS
BEGIN
    INSERT INTO QUESTION ("NAME", "CREATEDATE", "TAG","STATUS", "MONEY","PHOTOID","OWNID","EXPLAIN")
    VALUES (
        p_name,
        CURRENT_DATE,
        p_tag,
        'T',
        p_money,
        p_img, -- Replaced p_mail with the correct parameter for PHOTOID
        (SELECT ID FROM USERS WHERE MAIL=p_mail),
        p_explain
    );
    COMMIT;
END;

Bonus Tip to Prevent This in the Future

To avoid column-order mixups like this, use the explicit column-value assignment syntax for INSERT. It's a bit more typing, but it's far safer—especially if your table schema changes later:

INSERT INTO QUESTION (
    "NAME",
    "CREATEDATE",
    "TAG",
    "STATUS",
    "MONEY",
    "PHOTOID",
    "OWNID",
    "EXPLAIN"
) VALUES (
    NAME => p_name,
    CREATEDATE => CURRENT_DATE,
    TAG => p_tag,
    STATUS => 'T',
    MONEY => p_money,
    PHOTOID => p_img,
    OWNID => (SELECT ID FROM USERS WHERE MAIL=p_mail),
    "EXPLAIN" => p_explain
);

This way, even if you reorder columns in the INSERT clause, your values will always map to the right columns.

内容的提问来源于stack exchange,提问作者user9796004

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:08:19