将 REF CURSOR 返回到程序生成的数据
我需要编写一个存储过程,它在表上执行一些插入操作,并根据插入的执行情况编译每行的“状态”列表。每行都将被插入到一个循环中,该循环将迭代游标,该游标为 INSERT 语句提供一些值。我需要返回的是一个结果集,如下所示:
FIELDS_FROM_ROW_BEING_INSERTED.., STATUS VARCHAR2
状态由插入的方式决定。例如,如果 INSERT 导致 DUP_VAL_ON_INDEX 异常,表明存在重复行,我会将 STATUS 设置为“Dupe”。如果一切顺利,我会将其设置为“成功”并继续下一行。
最后,我将得到一个包含 N 行的结果集,其中 N 是执行的插入语句的数量,每行包含一些正在插入的行的标识信息,以及插入的“状态
”我的数据库中没有表来存储我想传回给用户的值,我想知道如何返回信息?临时表?在Oracle中,临时表似乎是“全局”的,不确定我是否想要一个全局表,会话完成后是否有任何临时表被删除?
I need to write a sproc which performs some INSERTs on a table, and compile a list of "statuses" for each row based on how well the INSERT went. Each row will be inserted within a loop, the loop iterates over a cursor that supplies some values for the INSERT statement. What I need to return is a resultset which looks like this:
FIELDS_FROM_ROW_BEING_INSERTED.., STATUS VARCHAR2
The STATUS is determined by how the INSERT went. For instance, if the INSERT caused a DUP_VAL_ON_INDEX exception indicating there was a duplicate row, I'd set the STATUS to "Dupe". If all went well, I'd set it to "SUCCESS" and proceed to the next row.
By the end of it all, I'd have a resultset of N rows, where N is the number of insert statements performed and each row contains some identifying info for the row being inserted, along with the "STATUS" of the insertion
Since there is no table in my DB to store the values I'd like to pass back to the user, I'm wondering how I can return the info back? Temporary table? Seems in Oracle temporary tables are "global", not sure I would want a global table, are there any temporary tables that get dropped after a session is done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 Oracle 10gR2 或更高版本,那么您应该检查 DML 错误日志记录。这基本上实现了您想要实现的目标,即它允许我们通过记录任何错误并继续执行语句来以批处理方式执行所有 DML。
原则是我们使用 PL/SQL 内置包 DBMS_ERRLOG 为需要使用的每个表创建一个 ERROR LOG 表。 了解更多信息。 DML 语法有一个简单的扩展,可以将消息记录到错误日志表中。 请参阅此处的示例。此方法不会创建比您的建议更多的对象,并且具有使用某些标准 Oracle 功能的优点。
当使用批量处理时(即使用 FORALL 语法时),我们可以使用内置的 SQL%BULK_EXCEPTIONS 集合捕获异常。 查看一下。可以将批量异常与 DML 错误日志记录结合起来,但这可能会在 11g 中产生问题。 了解更多。
If you are using Oracle 10gR2 or later then you should check out DML error logging. This basically does what you want to achieve, that is, it allows us to execute all the DML in a batch process by recording any errors and pressing on with the statements.
The principle is that we create an ERROR LOG table for each table we need to work with, using a PL/SQL built-in package DBMS_ERRLOG. Find out more. There is a simple extension to the DML syntax to log messages to the error log table. See an example here. This approach doesn't create any more objects than your proposal, and has the merit of using some standard Oracle functionality.
When working with bulk processing (that is, when using the FORALL syntax) we can trap exceptions using the built-in SQL%BULK_EXCEPTIONS collection. Check it out. It is possible to combine Bulk Exceptions with DML Error Logging but that may create problems in 11g. Find out more.
对于临时表而言,“全局”仅意味着它们是永久性的,而数据是临时的。
我将定义一个与您的光标匹配的记录类型以及状态字段。然后定义该类型的表。
更好的方法是将输入也定义为表类型而不是游标。
"Global" in the case of temporary tables just means they are permanent, it's the data which is temporary.
I would define a record type that matches your cursor, plus the status field. Then define a table of that type.
Even better would be to also define the inputs as a table type instead of a cursor.