rust-oracle icon indicating copy to clipboard operation
rust-oracle copied to clipboard

Question - Calling Oracle Stored Procedures Example

Open Arunosaur opened this issue 2 years ago • 3 comments

How to call Oracle stored procedures returning sys_refcursor as out parameter from oracle0.5.7?

Arunosaur avatar Mar 17 '23 17:03 Arunosaur

See https://www.jiubao.org/rust-oracle/oracle/sql_type/struct.RefCursor.html.

let sql = "begin StoredProcedureName(:cursor); end;";
let mut stmt = conn.statement(sql).build()?;
stmt.execute(&[&None::<RefCursor>])?; // bind :cursor as RefCursor and execute the statement.

let mut cursor: RefCursor = stmt.bind_value(1)?; // get :cursor as RefCursor.

kubo avatar Mar 17 '23 22:03 kubo

How to specify an argument for this procedure? I have a procedure cursor = get_containers(identity), identity is a string (varchar).

As far as I know we should describe return value like this:

:outval = StoredProcedureName(:somearg);

How to get outval?

lebe-dev avatar Aug 05 '23 09:08 lebe-dev

Solved. May be it will help someone:

Procedure returns rows with id and path.

let mut containers: Vec<CloudContainer> = vec![];
let sql = "BEGIN :out := CLOUD_STORAGE_META.get_s3_containers(:identity); END;";
let mut stmt = cnn.statement(sql).build()?;

stmt.execute(&[&OracleType::RefCursor, &identity]).unwrap();

let mut ref_cursor: RefCursor = stmt.bind_value(1)?;

let rows = ref_cursor.query_as::<(u32, String)>()?;

for row_result in rows {
  let (storage_id, container_name) = row_result.expect("unable to decode result");

  let container = CloudContainer {
      storage_id,
      container_name,
  };

  containers.push(container);
}

lebe-dev avatar Aug 05 '23 12:08 lebe-dev