If you frequently work with PL/SQL code from your ODP.NET application you may have encountered code that uses the dbms_output package. This is a common debugging technique employed in PL/SQL code. In fact, the Oracle® Database PL/SQL Packages and Types Reference 10g Release 2 (10.2) has this to say about the dbms_output package:
The DBMS_OUTPUT package enables you to send messages from stored procedures, packages, and triggers. The package is especially useful for displaying PL/SQL debugging information.
In practice, calls to dbms_output are embedded in the PL/SQL code and then the resulting text is often displayed in a character-based tool such as SQL*Plus. But what if you are using an ODP.NET application? How is it possible to retrieve values into your client application that have been output by the dbms_output package into a server-side buffer?
Fortunately the dbms_output package provides two procedures that can be used to accomplish this:
- get_line
- get_lines
As their names suggest, get_line is used to retrieve a single line from the buffer while get_lines can be used to retrieve multiple lines from the buffer.
In this tip I show you how to use both of these procedures in a single sample application.
In order to retrieve a single line of text, you use a parameter object that represents a Varchar2 data type and a parameter object that represents a Decimal data type. The Varchar2 parameter holds the actual line of text while the Decimal parameter holds the status code returned from the dbms_output package.
The code to retrieve multiple lines of text also uses two OracleParameter objects: one parameter object is a PL/SQL Associative Array to hold the lines of text and the other is a Decimal parameter that holds the number of lines to fetch before the call to dbms_output or the number of lines that were fetched after the call to dbms_output.
Be sure to review the documentation for dbms_output linked above.
Begin by creating the PL/SQL code as the database user that will connect to the database from the ODP.NET application. Of course, this is just sample code to illustrate this technique. Your real code would perform additional activities and almost certainly would be more complex.
The PL/SQL code for the sample package and package body:
create or replace package dbms_output_test as
procedure emit_single_line;
procedure emit_multiple_lines;
end;
/
create or replace package body dbms_output_test as
procedure emit_single_line is
begin
/* enable dbms_output using defaults */
dbms_output.enable;
/* output a single line of text */
dbms_output.put_line('This is a simple line of text emitted by dbms_output.');
end;
procedure emit_multiple_lines is
begin
/* enable dbms_output using defaults */
dbms_output.enable;
/* output multiple lines of text */
dbms_output.put_line('Sgt. Pepper''s Lonely Hearts Club Band Track List');
dbms_output.put_line('================================================');
dbms_output.put_line('01 - Sgt. Pepper''s Lonely Hearts Club Band');
dbms_output.put_line('02 - With a Little Help From My Friends');
dbms_output.put_line('03 - Lucy in the Sky With Diamonds');
dbms_output.put_line('04 - Getting Better');
dbms_output.put_line('05 - Fixing a Hole');
dbms_output.put_line('06 - She''s Leaving Home');
dbms_output.put_line('07 - Being for the Benefit of Mr. Kite!');
dbms_output.put_line('08 - Within You Without You');
dbms_output.put_line('09 - When I''m Sixty-Four');
dbms_output.put_line('10 - Lovely Rita');
dbms_output.put_line('11 - Good Morning Good Morning');
dbms_output.put_line('12 - Sgt. Pepper''s Lonely Hearts Club Band (Reprise)');
dbms_output.put_line('13 - A Day in the Life');
end;
end;
/
As you can see, this code simply outputs text using the dbms_output package and performs no other processing. The emit_single_line procedure outputs a single line of sample text while the emit_multiple_lines procedure outputs the track listing for Sgt. Pepper's Lonely Hearts Club Band by The Beatles.
The C# sample code is fairly well commented and should be easy to follow:
using System;
using System.Data;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
namespace DbmsOutput
{
class Program
{
// connect to default database using o/s authenticated account
// be sure to adjust for your environment
private const string constr = "user id=/; enlist=false; pooling=false";
static void Main(string[] args)
{
// create and open connection
OracleConnection con = new OracleConnection(constr);
con.Open();
// call method for single line output
GetSingleLine(con);
// call method for multiple line output
GetMultipleLines(con);
// clean up
con.Dispose();
// prevent console from closing automatically when run from within VS
Console.Write("ENTER to continue...");
Console.ReadLine();
}
static void GetSingleLine(OracleConnection con)
{
// call the procedure that emits a single line of text
string stored_procedure = "dbms_output_test.emit_single_line";
// anonymous pl/sql block to get the line of text
string anonymous_block = "begin dbms_output.get_line(:1, :2); end;";
// create command and execute the stored procedure
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = stored_procedure;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
// create parameters for the anonymous pl/sql block
OracleParameter p_line = new OracleParameter("",
OracleDbType.Varchar2,
32000,
"",
ParameterDirection.Output);
OracleParameter p_status = new OracleParameter("",
OracleDbType.Decimal,
ParameterDirection.Output);
// set command text and parameters to get the text output
// and execute the anonymous pl/sql block
cmd.CommandText = anonymous_block;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(p_line);
cmd.Parameters.Add(p_status);
cmd.ExecuteNonQuery();
// write location, return status value, and the text to the console window
Console.WriteLine("In method GetSingleLine...");
Console.WriteLine("Return status: {0}", p_status.Value.ToString());
Console.WriteLine("Return text: {0}", p_line.Value.ToString());
Console.WriteLine();
Console.WriteLine();
// clean up
p_line.Dispose();
p_status.Dispose();
cmd.Dispose();
}
static void GetMultipleLines(OracleConnection con)
{
// write location to console window
Console.WriteLine("In method GetMultipleLines...");
Console.WriteLine();
// call the procedure that emits multiple lines of text
string stored_procedure = "dbms_output_test.emit_multiple_lines";
// anonymous pl/sql block to get multiples lines of text per fetch
string anonymous_block = "begin dbms_output.get_lines(:1, :2); end;";
// used to indicate number of lines to get during each fetch
const int NUM_TO_FETCH = 8;
// used to determine number of rows fetched in anonymous pl/sql block
int numLinesFetched = 0;
// simple loop counter used below
int i = 0;
// create command and execute the stored procedure
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = stored_procedure;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
// create parameter objects for the anonymous pl/sql block
OracleParameter p_lines = new OracleParameter("",
OracleDbType.Varchar2,
NUM_TO_FETCH,
"",
ParameterDirection.Output);
p_lines.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
p_lines.ArrayBindSize = new int[NUM_TO_FETCH];
// set the bind size value for each element
for (i = 0; i < NUM_TO_FETCH; i++)
{
p_lines.ArrayBindSize[i] = 32000;
}
// this is an input output parameter...
// on input it holds the number of lines requested to be fetched from the buffer
// on output it holds the number of lines actually fetched from the buffer
OracleParameter p_numlines = new OracleParameter("",
OracleDbType.Decimal,
"",
ParameterDirection.InputOutput);
// set the number of lines to fetch
p_numlines.Value = NUM_TO_FETCH;
// set up command object and execute anonymous pl/sql block
cmd.CommandText = anonymous_block;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(p_lines);
cmd.Parameters.Add(p_numlines);
cmd.ExecuteNonQuery();
// get the number of lines that were fetched (0 = no more lines in buffer)
numLinesFetched = ((OracleDecimal)p_numlines.Value).ToInt32();
// as long as lines were fetched from the buffer...
while (numLinesFetched > 0)
{
// write the text returned for each element in the pl/sql
// associative array to the console window
for (i = 0; i < numLinesFetched; i++)
{
Console.WriteLine((p_lines.Value as OracleString[])[i]);
}
// re-execute the command to fetch more lines (if any remain)
cmd.ExecuteNonQuery();
// get the number of lines that were fetched (0 = no more lines in buffer)
numLinesFetched = ((OracleDecimal)p_numlines.Value).ToInt32();
}
// just a couple of separator lines
Console.WriteLine();
Console.WriteLine();
// clean up
p_numlines.Dispose();
p_lines.Dispose();
cmd.Dispose();
}
}
}
Executing the sample produces the following output in the console window:
In method GetSingleLine...
Return status: 0
Return text: This is a simple line of text emitted by dbms_output.
In method GetMultipleLines...
Sgt. Pepper's Lonely Hearts Club Band Track List
================================================
01 - Sgt. Pepper's Lonely Hearts Club Band
02 - With a Little Help From My Friends
03 - Lucy in the Sky With Diamonds
04 - Getting Better
05 - Fixing a Hole
06 - She's Leaving Home
07 - Being for the Benefit of Mr. Kite!
08 - Within You Without You
09 - When I'm Sixty-Four
10 - Lovely Rita
11 - Good Morning Good Morning
12 - Sgt. Pepper's Lonely Hearts Club Band (Reprise)
13 - A Day in the Life
ENTER to continue...
Happy coding...
相关推荐
matlab转换java代码utl_passing_sas_macro_variables_to_R_and_retrieving_macro_variables_from_R 将宏变量传递给R并从R中检索宏变量。关键字:sas sql连接合并大数据分析宏oracle teradata mysql sas社区...
See - tfmadmin_create.sql This account will be the repository for all of the Oracle objects used by the utility and will be responsible for retrieving information from the file system for presentation...
### 关于“Retrieving the COM class factory for component with CLSID”问题详解 #### 背景介绍 在使用ASP .NET应用程序操作Excel、Word等Office软件时,有时会在IIS服务器上部署应用过程中遇到错误:...
在IT行业中,"retrieving account information"是一个关键任务,特别是在数据库管理、用户身份验证、客户关系管理和系统审计等场景中。以下是一些与这个主题相关的详细知识点: 1. **数据库查询**: 要获取账户信息...
The provided code snippet and description detail a custom `WebClient` implementation in C#. This class is designed to handle HTTP requests and responses, specifically focusing on GET and POST methods....
- **支持研究工作**:对于像《2008_Study on Searching-retrieving Behaviour in Designers' Ideation Process》这样的研究项目而言,NII 电子图书馆服务能够为其提供必要的文献资料支持,有助于深化对该主题的理解...
c++ wince sdk AI <br>smartphone版本 和windows版本 <br>目录 Table of content I. Introduction....................................................................................................
sql server sqldmo_x86_x64,C#数据库备份还原很好用的.dll,里面有使用方法,引用Interop.SQLDMO.dll后的注意事项。 SQLDMO.dll是个好东西,ASP.NET利用它可以实现在线备份、还原数据库等各种功能。近日有客户...
解决的问题: 现象: 如果SAP服务端是2022年最新的7700版本(实施商的说法,实际是否最新有待证实);通过NuGet或网上找的一下DLL库都太老了,在一切配置都正常的情况下调用repository.CreateFunction("接口名")时报...
// Retrieving output value ``` 5. **关闭连接**:完成操作后,记得关闭数据库连接以释放资源。 ```csharp connection.Close(); ``` 在提供的示例中,"C#使用存储过程录入数据"可能是一个完整的项目,包含C#...
14.5.3. Retrieving Validated Fields and other Reports 14.5.3.1. Querying if the input is valid 14.5.3.2. Getting Invalid, Missing, or Unknown Fields 14.5.3.3. Getting Valid Fields 14.5.4. Using ...
### Oracle Hyperion Essbase - Retrieving Data - 关键知识点解析 #### 一、Essbase Spreadsheet Add-in 的优势 - **无缝集成**: Essbase Spreadsheet Add-in 是一个嵌入式软件程序,能够与客户端电子表格应用...
**1.3.2 Adding the Output Arguments** 在C++中如何定义输出参数,以便将MATLAB计算的结果传递回C++程序。 **1.3.3 Adding the Input Arguments** 介绍如何在C++中定义输入参数,以向MATLAB传递数据。 **1.3.4 ...
Retrieving Session Data H G Managing the Orders Building an Order H Reviewing the Order H Placing the Order H G Wrapping Up G From Here G Chapter 10 Site Administration Working with Web Server...
提问题的朋友贴出了数据源,来自于:http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query,数据结构大致如下: 复制代码 代码如下: <...
"Unlocker"是一款知名的系统工具,专为解决用户在尝试删除文件或文件夹时遇到的“正在使用”问题。在Windows操作系统中,有时我们会遇到无法直接删除某个文件或文件夹的情况,这是因为该文件正被其他程序占用。...
在IT领域,"Retrieving selected items找回选择的项的内容"这一主题主要涉及到数据操作和用户交互,特别是针对用户在应用程序中进行的选择操作。这通常发生在各种类型的应用程序中,如文件管理器、数据库应用或者...
【标题】:“RM-NN算法用于检索陆地表面温度” 【描述】:“使用RM-NN算法从EOS/MODIS数据中检索陆地表面温度和发射率” 【标签】:“EOS/MODIS数据 RM-NN 检索 LST(陆地表面温度)” 本文介绍了一种名为RM-NN...
此外,手册还介绍了一些JDBC的基础概念,例如如何使用JDBC驱动管理器连接MySQL,以及如何通过JDBC编程模型使用Statement、CallableStatement和Retrieving AUTO_INCREMENT Column Values等对象。这些是所有使用JDBC...