Home  >  Article  >  Web Front-end  >  How to run oracle database stored procedure in nodejs

How to run oracle database stored procedure in nodejs

下次还敢
下次还敢Original
2024-04-21 05:52:06743browse

In Node.js, use the OracleDB library to execute Oracle database stored procedures: Install the OracleDB library. Create a database connection. Define the stored procedure name and parameters. Execute the stored procedure. Close the database connection.

How to run oracle database stored procedure in nodejs

Node.js executes Oracle database stored procedure

Method

In Node.js, you can use the OracleDB library to execute Oracle database stored procedures.

Steps

  1. Install OracleDB library
<code>npm install oracledb</code>
  1. Create database connection
<code class="javascript">const oracledb = require('oracledb');

async function createConnection() {
  return oracledb.getConnection(connectionParams);
}</code>
  1. Define stored procedure name and parameters
<code class="javascript">const procedureName = 'MY_PROCEDURE';
const parameters = {
  input: oracledb.NUMBER,
  output: oracledb.OUT
};</code>
  1. Execute stored procedure
<code class="javascript">async function executeProcedure(connection) {
  const result = await connection.execute(
    procedureName,
    parameters,
    {
      autoCommit: true
    }
  );

  return result.outBinds;
}</code>
  1. Close database connection
<code class="javascript">async function closeConnection(connection) {
  await connection.close();
}</code>

Sample code

<code class="javascript">const connectionParams = {
  user: 'username',
  password: 'password',
  connectString: 'host:port/databasename'
};

createConnection()
  .then(connection => executeProcedure(connection))
  .then(result => console.log(result))
  .catch(err => console.error(err))
  .finally(() => closeConnection(connection));</code>

The above is the detailed content of How to run oracle database stored procedure in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn