How to pass VARGRAPHIC to Stored procedures in DB2? - db2

How can I call a sp with VARGRAPHIC variable type as input?
I've create this super simple sp that dose nothing and just for test, with following statement:
CREATE PROCEDURE MYPROCEDURE (IN VARNAME vargraphic(5) )
LANGUAGE SQL
P1: BEGIN
END P1
but when I call the sp in IBM Data Studio,it raises this error:
{? = call SCHEMA.MYPROCEDURE (?)}
[SQL0189] Coded Character Set Identifier 37 not valid.
Run of routine failed.
- Roll back completed successfully.
is there any problem in my sp code?
should I define CCSIDs? How and where?

I suspect that you would want to specify the CCSID of your parameter like this
call SCHEMA.MYPROCEDURE( CAST(? AS VARGRAPHIC(5) CCSID 65535) )
picking the correct CCSID number as appropriate
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_74/db2/rbafzcast.htm

Related

Compare variables declared in Snowflake Stored procedure

I got a stored procedure as below
CREATE OR REPLACE PROCEDURE SP12
(argument1 VARCHAR,argument2 VARCHAR
)
if (argument1 <> argument2) { return N1 } else { return N2}
The issue is iam getting error while comparing argument1 <> argument2 (need to compare not equal to).
Is this correct method.
Thanks
is the the complete code for your Stored Procedure or is it just a snippet? If it is the full code then I suggest that you read the documentation again: CREATE PROCEDURE
You need a RETURNS statement in the header
How are you declaring/assigning values to N1 and N2?
If these suggestions don't help then please provide the full SP Code (if this is just a snippet) and also the error message you are getting

DB2 procedure call literal error

I'm trying to call a DB2 procedure using DBeaver.
The syntax I'm using is this:
{ call db.procedure_name ('1234','2345','3456') }
Then I get an error saying:
"Literal replacement prasing failed for procedure call... Failing SQL
text..."
When I call the same procedure like this:
{ call db.procedure_name (?,?,?) }
and insert the parameters manually it executes.
I'm quite new to DB2 and IBM troubleshooting sites aren't much of a help.
Can you guys help me out? Thanks.
Try call db.procedure_name (:1,:2,:3)
or alternatively, create global variables for each parameter
create or replace variable p1 integer default 123;
create or replace variable p2 decimal(5,2) default 123.45;
create or replace variable p3 char(5);
set p3 = '12345';
call db.procedure_name (p1,p2,p3);

Why am I getting a syntax error when calling my stored procedure?

I am trying to call a stored procedure with Time variable as in parameter. But whenever i try to call the procedure i m getting error as:
db2 'call PASS_FAIL_CHECKDATE('2014-01-21','13:42:25','CSS1',Null,'4500096651','10',Null)'
SQL0104N An unexpected token ":42" was found following "CKDATE(2014-01-21,
13". Expected tokens may include: "+". SQLSTATE=42601
My Procedures input parameter are :
PASS_fail_checkdate (in post_date date,in post_time time,in destplant varchar(4), in destloc varchar(4), in transnum varchar(10), in translineitemnum varchar(6), in inboundconsignment varchar(35))
I am not sure if my declaration for time variable is correct in procedure or if i am calling the time variable correctly in the procedure.
Please give me suggestions on the same.
As already offered, in words, try the following example as a revision to what was noted in the OP as tried already but failing; i.e. change to use double-quote vs the apostrophe, specified as the delimiter for the DB2 SQL statement string:
db2 "call PASS_FAIL_CHECKDATE('2014-01-21','13:42:25','CSS1',Null,'4500096651','10',Null)"

I can create a stored procure with invalid user defined function names in it

I just noticed that I could alter my stored procedure code with a misspelled user defined function in it.
I noticed that at 1st time I execute the SP.
Is there any way to get a compile error when an SP include an invalid user-defined function name in it?
At compile time? No.
You can, however, use some of SQL's dependency objects (if using MS SQL) to find problems just after deployment, or as part of your beta testing. Aaron Bertran has a pretty nice article rounding up the options, depending upon the version of SQL Server.
Here is an example using SQL Server 2008 sys object called sql_expression_dependencies
CREATE FUNCTION dbo.scalarTest
(
#input1 INT,
#input2 INT
)
RETURNS INT
AS
BEGIN
-- Declare the return variable here
DECLARE #ResultVar int
-- Add the T-SQL statements to compute the return value here
SELECT #ResultVar = #input1 * #input2
-- Return the result of the function
RETURN #ResultVar
END
GO
--Fn Works!
SELECT dbo.ScalarTest(2,2)
GO
CREATE PROCEDURE dbo.procTest
AS
BEGIN
SELECT TOP 1 dbo.scalarTest(3, 3) as procResult
FROM sys.objects
END
GO
--Sproc Works!
EXEC dbo.procTest
GO
--Remove a dependency needed by our sproc
DROP FUNCTION dbo.scalarTest
GO
--Does anything have a broken dependency? YES
SELECT OBJECT_NAME(referencing_id) AS referencing_entity_name,
referenced_entity_name, *
FROM sys.sql_expression_dependencies
WHERE referenced_id IS NULL --dependency is missing
GO
--Does it work? No
EXEC dbo.procTest
GO

ARRAY declaration inside function in db2 iseries 7.1

I am unable to declare an array inside a function. It shows the below error.
Error: [SQ20441] Array type not valid where specified.
My code is below,
CREATE TYPE REV_QTY AS INTEGER ARRAY[] ;--ALLOW_ARRAY_VALUE_CHANGES
CREATE OR REPLACE FUNCTION GET_REV_QTY (IN_IPID INTEGER,QTY_TO_PLACE
INTEGER,INITIAL_REV_QTY INTEGER)
RETURNS INTEGER
LANGUAGE SQL
DETERMINISTIC
MODIFIES SQL DATA
--CONTAINS SQL
--RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION
BEGIN
DECLARE AVAILABLE_REV_QTY REV_QTY;
if exists(AVAILABLE_REV_QTY[IN_IPID]) then
CASE WHEN QTY_TO_PLACE > AVAILABLE_REV_QTY[IN_IPID] THEN
SET AVAILABLE_REV_QTY[IN_IPID] = 0
WHEN QTY_TO_PLACE < AVAILABLE_REV_QTY[IN_IPID] AND QTY_TO_PLACE >= 0
THEN
SET AVAILABLE_REV_QTY[IN_IPID] = AVAILABLE_REV_QTY[IN_IPID] -
QTY_TO_PLACE
END;
END IF;
RETURN AVAILABLE_REV_QTY[IN_IPID] ;
END |
Please help me on this. I am new to DB2 i7.1 and I am unable to
understand what is going wrong.
The IBM i 7.1 documentation for that sqlcode=-20441 does not list\include the SQL Scalar Function as where an array-type specification can be made [neither as an argument nor declared variable]. See:
Listing of SQL messages
These tables list SQL messages. Use these tables to find message text, cause text, recovery text, and corresponding SQLCODEs and SQLSTATEs.
SQ20441
Message Text: Array type not valid where specified.
Cause Text: An array type was used but is not allowed in the specified context. Array types can only be used:
• As an argument of an SQL or JAVA procedure.
• For an SQL variable declared in an SQL procedure.
• In a CAST specification in an SQL procedure.
Recovery Text: Remove the reference to the array type. Try the request again.
SQLCODE or SQLCODEs: -20441
SQLSTATE or SQLSTATEs: 428H2
As asides, if the references were code where allowed:
• the EXISTS predicate is not allowed syntax as written in the OP; presumably the following predicate is desired:
if cardinality(AVAILABLE_REV_QTY) >= IN_IPID then
• required semicolon after the statements in THEN of CASE are missing
• INITIAL_REV_QTY INTEGER is unreferenced
• DECLARE AVAILABLE_REV_QTY REV_QTY ; has DEFAULT NULL so even the CARDINALITY is of no use unless the array values are first constructed; perhaps passed as an argument
Note: the work of the routine could be composed as a SQL PROCEDURE, and perhaps that FUNCTION also could be rewritten to invoke the stored procedure to perform the array work -- unsure, as whence the data comes for the array is not clear from the OP.