如何在运行查询后获取Oracle显示的消息?例如,如果我写
create table t;
显示此消息
ORA-00906: missing left parenthesis
但是,如果我写
create table t(id int);
显示
table created.
我想要获取这些消息,如
ORA-00906: missing left parenthesis
或
Table Created
问题陈述是: 我希望用户在Web浏览器的文本区域中输入他的查询。我将获取并将该查询传递给Oracle,并希望显示在他在SQL * Plus上而不是在浏览器中编写这些查询时显示给他的相同消息。
简单来说,我想创建一个模拟SQL * Plus的页面。
我在C#中使用ASP.NET 4.5,使用ODP.NET和Oracle 10g。根据我的研究,我已经知道在成功执行的情况下,Oracle会返回一些SQLCA代码,这些代码被SQL * Plus操作并显示为正确的消息。
答案 0 :(得分:3)
如果您使用SQL*Plus
,则可以将错误消息捕获到sperrorlog
表中。这个新功能是在release 11.1
中引入的。您可以将错误记录到此表中以查找以下类型的错误 -
我已经写了一篇小文章,您可以查看它以获取更多信息。 SQL*Plus error logging – New feature release 11.1
您需要做的就是set errorlogging on
您的会话。
这就是sperrorlog
表在描述时的样子 -
SQL> desc sperrorlog;
Name Null? Type
----------------------------------------- -------- ----------------------------
USERNAME VARCHAR2(256)
TIMESTAMP TIMESTAMP(6)
SCRIPT VARCHAR2(1024)
IDENTIFIER VARCHAR2(256)
MESSAGE CLOB
STATEMENT CLOB
让我们看一个例子 -
SQL> set errorlogging on;
SQL> selct * from dual;
SP2-0734: unknown command beginning "selct * fr..." - rest of line ignored.
SQL> select timestamp, username, script, statement, message from sperrorlog;
TIMESTAMP
---------------------------------------------------------------------------
USERNAME
--------------------------------------------------------------------------------
SCRIPT
--------------------------------------------------------------------------------
STATEMENT
--------------------------------------------------------------------------------
MESSAGE
--------------------------------------------------------------------------------
11-SEP-13 01.27.29.000000 AM
SCOTT
TIMESTAMP
---------------------------------------------------------------------------
USERNAME
--------------------------------------------------------------------------------
SCRIPT
--------------------------------------------------------------------------------
STATEMENT
--------------------------------------------------------------------------------
MESSAGE
--------------------------------------------------------------------------------
selct * from dual;
SP2-0734: unknown command beginning "selct * fr..." - rest of line ignored.
rollback
与此功能一起使用时出现问题。我有一篇解决方法的文章SQL*Plus error logging – workaround for ROLLBACK issue
答案 1 :(得分:0)
尝试" InfoMessage"事件:
http://docs.oracle.com/html/B14164_01/OracleInfoMessageEventArgsClass.htm
// C#
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
class InfoMessageSample
{
public static void WarningHandler(object src, OracleInfoMessageEventArgs args)
{
Console.WriteLine("Source object is: " + src.GetType().Name);
Console.WriteLine("InfoMessageArgs.Message is " + args.Message);
Console.WriteLine("InfoMessageArgs.Source is " + args.Source);
}
static void Main()
{
OracleConnection con = new OracleConnection("User Id=scott;" +
"Password=tiger;Data Source=oracle;");
con.Open();
OracleCommand cmd = con.CreateCommand();
//Register to the InfoMessageHandler
cmd.Connection.InfoMessage += new OracleInfoMessageEventHandler(WarningHandler);
cmd.CommandText =
"create or replace procedure SelectWithNoInto( " +
" empname in VARCHAR2) AS " +
"BEGIN " +
" select * from emp where ename = empname; " +
"END SelectWithNoInto;";
// Execute the statement that produces a warning
cmd.ExecuteNonQuery();
// Clean up
cmd.Dispose();
con.Dispose();
}
}