Compare variables declared in Snowflake Stored procedure - snowflake-task

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

Related

How to pass VARGRAPHIC to Stored procedures in 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

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);

How to store the result of a select query into a variable(IBM DB2)?

I am trying to save the result of a query into a variable. I am using IBM DB2, but I can only store the result if I am declaring the variable inside a procedure.
My code is:
DECLARE #myvar INTEGER;
SET #myvar = (SELECT MAX(ID) FROM S0SCSQMS.S0SRPTCNAME);
and I receive the following errors:
For the first line:"SQL0104N An unexpected token "INTEGER" was found following "DECLARE #myvar ". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601"
The error code does not tell me much. I looked for it on the IBM documentation.
Looking forward for an answer.
Thank you.
try this (work on iseries db2 v7r1)
CREATE OR REPLACE VARIABLE myvar INTEGER ;
SET myvar = (SELECT max( id_xp_dossier) FROM cilgprod.xp_dossier);
DROP VARIABLE myvar;

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