简体   繁体   English

以数组为参数调用存储过程

[英]Calling a stored procedure with an array as parameter

Using Java and Spring Data, I need to call the following stored procedure from an Oracle database: 使用Java和Spring Data,我需要从Oracle数据库中调用以下存储过程:

TYPE R_ID_INSC_TYPE IS RECORD(id_insc  INSC_REGISTRE.ID%TYPE);
TYPE T_ID_INSC IS TABLE OF R_ID_INSC_TYPE INDEX BY BINARY_INTEGER;

PROCEDURE CHARGER_RECHERCHE
(
    P_NO_DEMANDE    IN NUMBER,
    P_SEQ_DEM       IN NUMBER,
    P_ETAPE_RECH    IN VARCHAR2,
    P_ID_INSC_TAB   IN T_ID_INSC
);

What seems to be causing me trouble is the last parameter in this stored procedure, which is meant to be a list of ids. 似乎引起我麻烦的是此存储过程中的最后一个参数,它是id的列表。 Here's how I'm calling it in Java: 这是我在Java中的调用方式:

public void call(final Integer numeroDemande,
                 final Integer numeroSequenceDemande,
                 final EtapeRecherche etapeRecherche,
                 final Integer[] ids) {

    new SimpleJdbcCall(this.jdbcTemplate)
            .withSchemaName(this.schemaName)
            .withProcedureName(this.procedureName)
            .declareParameters(
                    new SqlParameter("P_NO_DEMANDE", Types.INTEGER),
                    new SqlParameter("P_SEQ_DEM", Types.INTEGER),
                    new SqlParameter("P_ETAPE_RECH", Types.VARCHAR),
                    new SqlParameter("P_ID_INSC_TAB", Types.ARRAY))
            .execute(numeroDemande, numeroSequenceDemande, etapeRecherche.getValue(), ids);
}

Which throws the following error: 这将引发以下错误:

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call PLATEFORME.CHARGER_RECHERCHE()}];
nested exception is java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'CHARGER_RECHERCHE'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

The person responsible for the implementation of the stored procedure also gave us the following usage example: 负责执行存储过程的人员还提供了以下用法示例:

declare

P_ID_INSC_TAB  PLATEFORME.T_ID_INSC;
P_NO_DEMANDE   NUMBER:=317225;
P_SEQ_DEM NUMBER:=1;
P_ETAPE_RECH VARCHAR2(2):='AI';

begin


P_ID_INSC_TAB(1).id_insc:=1258773;
P_ID_INSC_TAB(2).id_insc:=1258774;
P_ID_INSC_TAB(3).id_insc:=1258775;

PLATEFORME.CHARGER_RECHERCHE(P_NO_DEMANDE,P_SEQ_DEM,P_ETAPE_RECH,P_ID_INSC_TAB);

end;

The person responsible for the implementation of the stored procedure also offered to change the definition to this one, if it can make it any easier: 负责执行存储过程的人员还提出将定义更改为该定义(如果可以简化定义的话):

TYPE T_ID_INSC IS TABLE OF NUMBER(9) INDEX BY BINARY_INTEGER;

PROCEDURE CHARGER_RECHERCHE
(
    P_NO_DEMANDE    IN NUMBER,
    P_SEQ_DEM       IN NUMBER,
    P_ETAPE_RECH    IN VARCHAR2,
    P_ID_INSC_TAB   IN T_ID_INSC
);

What would I need to do in order to successfully call this stored procedure? 为了成功调用此存储过程,我需要做什么?

Thanks. 谢谢。

T_ID_INSC is an Oracle type. T_ID_INSC是Oracle类型。 ids cannot be a java Integer array. id不能是Java Integer数组。 Try this (conn is the oracle connection): 试试这个(conn是oracle连接):

import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;

ArrayDescriptor arrDesc = ArrayDescriptor.createDescriptor("T_ID_INSC", conn);
Integer[] data = {1258773, 1258774, 1258775};
Array ids = new ARRAY(arrDesc, conn, data);

or else try: 否则尝试:

import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;

Integer[] data = {1258773, 1258774, 1258775};
Array ids = ((OracleConnection)conn).createOracleArray("T_ID_INSC", data);

You can put the Ids is a loop and call the the stored procedure. 您可以将Ids循环并调用存储过程。 like below: 如下所示:

for (int i=0;i<arr.size();i++) 
{
int id=arr[i];
//call stored procedure here, passing id
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从java中的输入参数使用数组调用sybase中的存储过程 - calling a stored procedure in sybase with an array from input parameter in java 使用列表参数调用存储过程 - Calling Stored Procedure With List Parameter 通过CallableStatement调用存储过程时出现“参数不是OUT参数”错误 - “Parameter is not an OUT parameter” error while calling stored procedure via CallableStatement 将字节数组作为参数传递给oracle中的存储过程 - Passing byte array as parameter to stored procedure in oracle 使用jpa 2.0调用具有out参数的存储过程 - calling a stored procedure having out parameter using jpa 2.0 从存储过程中获取 Output 参数而不调用 execute() - Get Output parameter from stored procedure without calling execute() 使用Spring +没有为参数指定值来调用Postgres存储过程 - Calling a Postgres stored Procedure using Spring + No value specified for parameter 调用存储过程时接收消息找不到对应的参数 - Receiving Message Unable to locate the Corresponding Parameter when calling Stored Procedure 使用 OUT 参数调用存储过程 Spring Spring JPA 2.* - Calling stored procedure using OUT parameter with Spring Spring JPA 2.* 在SQL Server 2008中使用表类型输入参数调用存储过程 - Calling stored procedure with table type input parameter in sql server 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM