How to call stored procedure taking array using odbc:param_query in Erlang

一世执手 提交于 2019-12-07 04:27:34

问题


I have a stored procedure in db2

create type intArray as integer array[100]@

create or replace procedure sum(in numList intArray, out total integer) 
begin 
    declare i, n integer;  

    set n = CARDINALITY(numList);

    set i = 1; 
    set total = 100; 

    while (i <= n) do 
    set total = total + numList[i]; 
    set i = i + 1; 
    end while;    
end@

I am trying to call through Erlang odbc:param_query.

odbc:param_query(Ref,  "CALL sum (?, ?)", [{sql_integer,[1]}, {sql_integer,out, [1]}]).

The above is giving me proper return as

{executed,1,[{101}]}

But when I pass multiple values as

 odbc:param_query(Ref,  "CALL sum (?, ?)", [{sql_integer,[1,2,3,4]}, {sql_integer,out, [1]}]).

It is throwing an exception

exception exit: {badarg,odbc,param_query,'Params'} in function odbc:decode/1 (odbc.erl, line 894)

Is there any other way to pass a list (Array) to the stored procedure?


回答1:


It looks like there is no OBDC data type (at least with a corresponding Erlang one) for a list of integer (see erlang obdc documentation). I don't know how the the final query should look like (the syntax for int array) but I think that you can achieve what you want by creating your query as a string:

Query = io_lib:format("CALL sum (~p , ~p)",[int_array_syntax([1,2,3,4]),1])

and then use odbc:sql_query(Ref, Query).




回答2:


I believe you need to have an equal amount of arguments in both lists of arguments, meaning add three 1s in your 2nd list or arguments.

odbc:param_query(Ref,  "CALL sum (?, ?)", [{sql_integer, [1,2,3,4]}, {sql_integer, out, [1,1,1,1]}]).


来源:https://stackoverflow.com/questions/18929391/how-to-call-stored-procedure-taking-array-using-odbcparam-query-in-erlang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!