How to import table data when calling oralce procedure [online materials]

Source: Internet
Author: User

There is a project at the front-end time and the Oracle stored procedure needs to be called,Its input parameters contain custom table data.

The solution is as follows:

1. You need to install the corresponding OracleODP 

 

PL/SQL package: My pack

Create package mypack
Type assocarrayvarchar2_t is table of varchar (20) index by binary_integer;
Procedure testvarchar2 (
Param1 in assocarrayvarchar2_t,
Param2 in out assocarrayvarchar2_t,
Param3 out assocarrayvarchar2_t );
End mypack;

PL/SQL package body: My pack

Create package body mypack as
procedure testvarchar2 (
param1 in assocarrayvarchar2_t,
param2 out assocarrayvarchar2_t,
param3 out assocarrayvarchar2_t)
is
I integer;
begin
-- copy a few elements from Y to z
param3 (1): = param2 (1 );
param3 (2): = NULL;
param3 (3): = param2 (3);

-- Copy all elements from X to Y
Param2 (1): = param1 (1 );
Param2 (2): = param1 (2 );
Param2 (3): = param1 (3 );

For I in 1 .. 3 Loop
Insert into T1 values (I, param2 (I ));
End loop;

For I in 1 .. 3 Loop
Select col2 into param2 (I) from T2 where col1 = I;
End loop;
End testvarchar2;
End mypack;

ODP. Net example

Public void bindassocarray (){
...

Oraclecommand cmd = new oraclecommand (
"Begin mypack. testvarchar2 (: 1,: 2,: 3); end;", con );

Oracleparameter param1 = cmd. Parameters. Add (...);
Oracleparameter param2 = cmd. Parameters. Add (...);
Oracleparameter param3 = cmd. Parameters. Add (...);

Param1.direction = parameterdirection. input;
Param2.direction = parameterdirection. InputOutput;
Param3.direction = parameterdirection. output;

// Specify that we are binding PL/SQL associative array
Param1.collectiontype = oraclecollectiontype. plsqlassociativearray;
Param2.collectiontype = oraclecollectiontype. plsqlassociativearray;
Param3.collectiontype = oraclecollectiontype. plsqlassociativearray;

// Setup the values for PL/SQL associative array
Param1.value = new string [3] {"first element ",
"Second element ",
"Third element ",
Param2.value = new string [3] {"first element ",
"Second element ",
"Third element ",
Param3.value = NULL;

// Specify the maximum number of elements in the PL/SQL associative array
Param1.size = 3;
Param2.size = 3;
Param3.size = 3;

// Setup the arraybindsize for param1
Param1.arraybindsize = new int [3] {13, 14, 13 };

// Setup the arraybindstatus for param1
Param1.arraybindstatus = new oracleparameterstatus [3] {
Oracleparameterstatus. Success,
Oracleparameterstatus. Success,
Oracleparameterstatus. Success };

// Setup the arraybindsize for param2
Param2.arraybindsize = new int [3] {20, 20, 20 };

// Setup the arraybindsize for param3
Param3.arraybindsize = new int [3] {20, 20, 20 };

// Execute the cmd
Cmd. executenonquery ();

// Print out the parameter's values
...
}

Array binding
The array bind feature enables applications to bind arrays of A type using the oracleparameter class. Using the array bind feature, an application can insert multiple rows into a table in a single database round-trip.

The following code example inserts three rows into the dept table with a single database round-trip. The oraclecommand arraybindcount property defines the number of elements of the array to use when executing the statement.

// C #
...
// Create an array of values that need to be inserted
Int [] myarraydeptno = new int [3] {10, 20, 30 };

// Set the command text on an oraclecommand object
Cmd. commandtext = "insert into dept (deptno) values (: deptno )";
// Set the arraybindcount to indicate the number of values
Cmd. arraybindcount = 3;

// Create a parameter for the Array Operations
Oracleparameter PRM = new oracleparameter ("deptno", oracledbtype. int32 );
PRM. Direction = parameterdirection. input;
PRM. value = myarraydeptno;

// Add the parameter to the parameter collection
Cmd. Parameters. Add (PRM );

// Execute the command
Cmd. executenonquery ();

See also:

"Value" for more information

Oracleparameter array bind Properties
The oracleparameter object provides two properties for granular control when using the array bind feature:

Arraybindsize Property

The arraybindsize property is an array of integers specifying the maximum size for each corresponding value in an array. the arraybindsize property is similar to the size property of an oracleparameter parameter t arraybindsize specifies the size for each value in an array.

Before the execution, the application must populate arraybindsize; after the execution, ODP. Net populates the arraybindsize.

Arraybindsize is used only for parameter types that have variable length such as clob, blob and varchar2. the size is represented in bytes for Binary datatypes and characters for the Unicode string types. the count for string types does not include the terminating character. the size is inferred from the actual size of the value, if it is not explicitly set. for an output parameter, the size of each value is set by ODP. net. the arraybindsize property is ignored for Fixed Length datatypes.

Arraybindstatus Property

The arraybindstatus property is an array of oracleparameterstatus values specifying status of each corresponding value in an array for a parameter. this property is similar to the status property of oracleparameter, parameter t that arraybindstatus specifies the status for each value in an array.

before the execution, the application must populate the arraybindstatus property and after the execution, ODP. net populates it. before the execution, an application using arraybindstatus can specify a null value for the corresponding element in the array for a parameter. after the execution, ODP. net populates the arraybindstatus array, indicating whether the corresponding element in the array has a null value or if data truncation occurred when the value was fetched.

error handling for Array binding
if an error occurs during an array bind execution, it can be difficult to determine which element in the value property caused the error. ODP. NET provides a way to determine the row where the error occurred, making it easier to find the element in the row that caused the error.

When an oracleexception is thrown during an array bind execution, the oracleerrorcollection contains one or more oracleerror objects. each of these oracleerror objects represents an individual error that occurred during the execution and contains a provider-specific property, arraybindindex, which indicates the row number at which the error occurred.

Code snippet

Try {
/An array bind execution errors out}

Catch (oracleexception e ){
Console. writeline ("oracleexception {0} occured", E. Message );
For (INT I = 0; I <E. errors. Count; I ++)
Console. writeline ("array BIND error {0} occured at row number {1}", E. errors [I]. Message,
E. errors [I]. arraybindindex );

} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.