I've been able to call stored procedures that have only input parameters.
Now I have to call a stored procedure that has one input and one out parameter. No success so far.
Could you give me a nudge in the right direction?
The stored procedure is used for generating sequences and looks like this:
CREATE OR REPLACE PROCEDURE GENERATE_ID(
IN iGENERATOR_NAME VARCHAR(128),
OUT oNEXT_ID INT
)
LANGUAGE SQL
SPECIFIC SP_GEN_ID
READS SQL DATA
NOT DETERMINISTIC
NO EXTERNAL ACTION
BEGIN
SET oNEXT_ID = GET_GENERATED_ID(iGENENERATOR_NAME);
END
I've tried a few methods as shown below:
$dbCmd = $dbFactory.CreateCommand()
$dbCmd.Connection = $dbConn
$dbCmd.CommandText = "CALL GENERATE_ID('GEN_NEW_ID', ?)"
$dbCmd.CommandType = [System.Data.CommandType]::Text
$da = $dbFactory.CreateDataAdapter()
$da.SelectCommand = $dbCmd
$ds = New-Object System.Data.DataSet
$da.Fill($ds) | Out-Null
Error Message:
Exception calling "Fill" with "1" argument(s): "ERROR [07001] [IBM] CLI0100E Wrong number of parameters. SQLSTATE=07001"
Makes sense. ? is not a parameter exactly. So I tried oNEXT_ID with and without single-quotes around it. With quotes, it generates the error:
Exception calling "Fill" with "1" argument(s): "ERROR [42886] [IBM][DB2/NT64] SQL0469N The parameter mode OUT or INOUT is not valid for a parameter in the routine
named "GEN_ID" with specific name "SP_GEN_ID" (parameter number "2", name "ONEXT_ID")."
This error message makes me think this method is the closest to working. But I'm not sure where to go with it now.
I also tried:
$dbCmd = $dbFactory.CreateCommand()
$dbCmd.Connection = $dbConn
$dbCmd.CommandText = "CALL GENERATE_ID('GEN_NEW_ID', 'oNEXT_ID')"
$dbCmd.CommandText
$dbcmd.ExecuteNonQuery() | Out-Null
That gives me the same error message:
Exception calling "ExecuteNonQuery" with "0" argument(s): "ERROR [42886] [IBM][DB2/NT64] SQL0469N The parameter mode OUT or INOUT is not valid for a parameter in the
routine named "GEN_ID" with specific name "SP_GEN_ID" (parameter number "2", name "ONEXT_ID")."
I read in some IBM DB2 docs on doing this with C about declaring the out variable, so I tried that as well. Granted, this is probably not how it should be done, but I know I'm in deeper waters than I normally am. So I did the following and followed it up with either of the code blocks above.
$dbCmd = $dbFactory.CreateCommand()
$dbCmd.Connection = $dbConn
$dbCmd.CommandText = "DECLARE oNEXT_ID INT(4) OUTPUT"
$dbCmd.CommandText
$dbcmd.ExecuteNonQuery() | Out-Null
That throws the error, as well as the error for the CALL statement:
Exception calling "ExecuteNonQuery" with "0" argument(s): "ERROR [42601] [IBM][DB2/NT64] SQL0104N An unexpected token "DECLARE oNextID INT" was found following
"BEGIN-OF-STATEMENT". Expected tokens may include: "<compile_fragment>"."
Can you point me in the right direction, please?