Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a stored procedure with a parameter of type table

I'd like to call a stored procedure, that has a parameter of type TABLE.
How can I do this using OCCI (11g1) in a windows C++ application?

Here's the definition of the stored procedure:

  FUNCTION am_send(
    p_caFnr                   IN       VARCHAR2,
    p_TabBgr                  IN       DSKS_BGR_TAB,
    p_caTextout               OUT      VARCHAR2)
  RETURN NUMBER;

and the used types:

create or replace
TYPE      DSKS_BGR_TAB,
AS TABLE OF DSKS_BGR

create or replace
TYPE      DSKS_BGR
(BgrNr    VARCHAR2(3),
 TrId     VARCHAR2(8))

What I have done so far:

I created a object representation of type DSKS_BGR using the OTT Utility.

My code so far:

Environment* env = Environment::createEnvironment(Environment::OBJECT); 

try
{
    Connection *con = env->createConnection("xxxxx", "xxxxx", "xxxxx");

    Statement* statement = con->createStatement("BEGIN :1 := am_send(:2, :3, :4); END;");

    statement->registerOutParam(1, OCCINUMBER);
    statement->setString(2, "Test");    
    // ?? DSKS_BGR_TAB
    statement->registerOutParam(4, OCCISTRING, 1000);

    statement->execute();

    int result = statement->getNumber(1);
    string textOut = statement->getString(4);

    env->terminateConnection(con);  
}
catch(const SQLException &exc)
{
    cout << exc.getErrorCode() << exc.getMessage();
}

Environment::terminateEnvironment(env);

I have no idea how to set the TABLE parameter.

like image 302
nabulke Avatar asked Apr 03 '14 12:04

nabulke


People also ask

Can a table be a parameter in stored procedure?

Table-Valued Parameters aka TVPs are commonly used to pass a table as a parameter into stored procedures or functions. They are helpful in a way, we can use a table as an input to these routines and we can get rid of dealing more complex steps to achieve this process.

How do you pass a table datatype to a procedure in SQL Server?

Create a user-defined table type that corresponds to the table that you want to populate. Pass the user-defined table to the stored procedure as a parameter. Inside the stored procedure, select the data from the passed parameter and insert it into the table that you want to populate.

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.


1 Answers

You are almost there!

  1. Create object representations of your oracle types using the Object Type Translator utility OTT

  2. Create a vector of pointers to the OTT created type and use the OCCI call setVector() on the statement

  3. Execute!

Here is a small code example:

#include <occi.h>
#include <iostream>
#include "RegisterMappings.h"

using namespace oracle::occi;
using namespace std;

void callproc(Connection *con)
{  
    vector<my_obj_t *> vect;  
    int i;  
    for (i=0; i<10; i++)  
    {    
        my_obj_t *obj = new my_obj_t();    
        obj->setid(i);
        obj->setname("TEST");
        vect.push_back(obj);
    }  
    cout << "\ncallproc - invoking a PL/SQL procedure with parameters"  << endl;  
    Statement *stmt = con->createStatement("BEGIN my_proc(:1); END;");  
    cout << "\nExecuting the block :" << stmt->getSQL() << endl;  
    setVector(stmt, 1, vect, "MY_OBJ_TAB_T");  
    stmt->execute();  
    con->terminateStatement (stmt);  

    cout << "\nocciproc - done" << endl; 

    // delete allocated memory
    for (i=0; i<10; i++)  
    {    
        delete vect[i];
    }  
}
// end of callproc ()
int main()
{
try {
    Environment* env = Environment::createEnvironment(Environment::OBJECT);  
    RegisterMappings(env);  
    Connection* conn = env->createConnection("scott","tiger");  
    callproc(conn);   conn->commit();  
    env->terminateConnection(conn);  
    Environment::terminateEnvironment(env);
    }
    catch(SQLException &ex)
    {  
        cout << ex.getMessage() << endl;
    }
}
like image 95
Ronald McBean Avatar answered Sep 22 '22 01:09

Ronald McBean