如何在Oracle函数结束时正确获取错误?

时间:2014-07-14 15:33:45

标签: oracle plsql oracle11g

这是我在程序结束时的代码。如果出现问题,它应该显示错误:

IF return_status = 'E' OR return_status = 'U'
  THEN
     MESSAGE := MESSAGE || 'return status: ' || return_status || ' msg: ';
     IF (FND_MSG_PUB.Count_Msg > 0)
     THEN
        FOR i IN 1 .. FND_MSG_PUB.Count_Msg
        LOOP
           FND_MSG_PUB.Get (p_msg_index       => i,
                            p_encoded         => 'F',
                            p_data            => out_message,
                            p_msg_index_OUT   => l_msg_index_OUT);
           MESSAGE := MESSAGE || ' ' || out_message;
        END LOOP;
     END IF;
  END IF;

  RETURN (MESSAGE);

MESSAGE是VARCHAR2(4000)变量;

我多次执行该过程并且很快发生以下错误:[Error] ORA-06502: PL/SQL: numeric or value error: character string buffer too small

这是输出(数字是lead_id,其余是连接错误信息):

192774 
192930 
192966 
193714 
193714 
193714 
193848 
193848 
193848 
193848 
194096 
194096 
194109 
194109 
194593 
194593 
194124 
195055 
194875 
195451 
195451 
195451 
195451 
195451 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database.
191239 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database.
191239 
187351 
192237 
195868 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database.
195939 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database.
196827 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database.
196977 
196977 
196977 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 493.  Please ensure that the entered identifier or combination of identifiers exist in the database.
197024 
197060 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 493.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database.
197158 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 493.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database.
197280 
197280 
197420 
197420 
197420 
197502 
197502 
197502 
197502 return status: E msg:  Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 616.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 493.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 11682.  Please ensure that the entered identifier or combination of identifiers exist in the database. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. Cannot read value for profile option USER_ID. The column(s), PRODUCT_CATEGORY, has an invalid value(s), 583.  Please ensure that the entered identifier or combination of identifiers exist in the database.
197502 
197761 
189846 
189846 
198500 
198500 
198500 
174335 
174335 
174335 
207133

您可以很容易地看到该消息包含上一次调用中的错误。我怎样才能重写它,所以它只包含正确的信息?我只考虑获取最后一条消息所有< FND_MSG_PUB.Count_Msg,但我不确定这是否正确。我是.NET开发人员,这是我们的遗留代码。我也在互联网上找到了这个:http://orclpps.blogspot.com/2007/06/fndmsgpub-for-error-logging-in-oracle.html它也使用了所有的消息。是否可以以某种方式清除FND_MSG_PUB或仅查找当前呼叫产生的错误?

4 个答案:

答案 0 :(得分:2)

您应该查看FND_MSG_PUB中提供的以下程序:

--  Procedure   Initialize
--
--  Usage       Used by API callers and developers to intialize the
--              global message table.
--  Desc        Clears the G_msg_tbl and resets all its global
--              variables. Except for the message level threshold.
--

PROCEDURE Initialize;

--  PROCEDURE   Delete_Msg
--
--  Usage       Used to delete a specific message from the message
--              list, or clear the whole message list.
--
--  Desc        If instructed to delete a specific message, the
--              message is removed from the message table and the
--              table is compressed by moving the messages coming
--              after the deleted messages up one entry in the message
--              table.
--              If there is no entry found the Delete procedure does
--              nothing, and  no exception is raised.
--              If delete is passed no parameters it deletes the whole
--              message table.
--
--  Prameters   p_msg_index     IN NUMBER := NULL Optional
--                  holds the index of the message to be deleted.
--

PROCEDURE Delete_Msg
(   p_msg_index IN    NUMBER    :=      NULL
);

调用Delete_Msg以在处理完消息后或在调用Oracle API之前清除消息。我不能说你是否会找到Initialize的用途,因为这取决于你没有向我们展示的申请的其他因素。示例(不保证编译):

IF return_status = 'E' OR return_status = 'U'
THEN
  IF FND_MSG_PUB.Count_Msg > 0
  THEN
    FOR i IN 1 .. FND_MSG_PUB.Count_Msg
    LOOP
      FND_MSG_PUB.Get (p_msg_index       => i,
                       p_encoded         => 'F',
                       p_data            => out_message,
                       p_msg_index_OUT   => l_msg_index_OUT);
      MESSAGE := MESSAGE || ' ' || out_message;
    END LOOP;

    FND_MSG_PUB.Delete_Msg;
  END IF;
END IF;

答案 1 :(得分:1)

MESSAGE附加现有文字不能这样,通过覆盖/取消?

IF return_status = 'E' OR return_status = 'U'
  THEN

     /*MESSAGE := MESSAGE || 'return status: ' || return_status || ' msg: ';*/


    -- Message is appended with existing text can't it be like this, by overwriting/nullifying ?

     MESSAGE  := 'return status: ' || return_status || ' msg: ';
     IF (FND_MSG_PUB.Count_Msg > 0)
     THEN
        FOR i IN 1 .. FND_MSG_PUB.Count_Msg
        LOOP
           FND_MSG_PUB.Get (p_msg_index       => i,
                            p_encoded         => 'F',
                            p_data            => out_message,
                            p_msg_index_OUT   => l_msg_index_OUT);
           MESSAGE := MESSAGE || ' ' || out_message;
        END LOOP;
     END IF;
  END IF;

  RETURN (MESSAGE);

此外,在PL/SQL中,Varchar2支持 32K

答案 2 :(得分:1)

它与您使用FND_MSG_PUB保存邮件的方式有关。我的猜测是,每次调用程序时,它的包状态都不会被重置;因此,每次运行该过程时,都会获得随时间累积的所有消息,这些消息会不断增加。

也许你可以在每次调用程序时重置FND_MSG_PUB的状态;也许你只能查看编号最高的邮件。这些中的任何一个是否正确和可靠取决于整个过程中包装的使用方式。

答案 3 :(得分:0)

让我们看一个将“项目类别”分配给“项目”的示例。

在这里,您可以看到参数"p_init_msg_list"设置为false,这意味着消息API不会被初始化并连接所有存储的错误。

inv_item_category_pub.create_category_assignment (
    p_api_version         => 1.0
    , p_init_msg_list       => fnd_api.g_false
    , p_commit              => fnd_api.g_false
    , x_return_status       => l_return_status
    , x_errorcode           => l_errorcode
    , x_msg_count           => l_msg_count
    , x_msg_data            => l_msg_data
    , p_category_id         => l_category_id
    , p_category_set_id     => l_category_set_id
    , p_inventory_item_id   => l_inventory_item_id
    , p_organization_id     => l_organization_id
);
            FOR k IN 1..fnd_msg_pub.count_msg LOOP
                fnd_msg_pub.get (
                    p_msg_index       => k
                    , p_encoded         => 'F'
                    , p_data            => l_msg_data
                    , p_msg_index_out   => l_msg_index_out
                );
                dbms_output.put_line ('l_msg_data:= ' || l_msg_data);
                IF l_msg_data IS NOT NULL THEN
                    l_error_message := l_error_message
                                       || '-'
                                       || l_msg_data;
                END IF;
            END LOOP;

如果要检查与交易有关的错误,请将参数"p_init_msg_list"设置为true或在循环中手动初始化消息。

FND_MSG_PUB.initialize;