Getting invalid SQL syntax upon executing stored procedure via DB2 CLI - stored-procedures

I am working on a stored procedure to update values in a couple of tables in one go. I am getting CLI0118E Invalid SQL syntax. during the execution. However, I am not able to find the issue with my stored procedure as I am not quite familiar with the whole stored procedure rules. Any help on this would be much appreciated.
CREATE OR REPLACE PROCEDURE MYPROCNAME(IN VAL1 VARCHAR(255),IN VAL2 VARCHAR(255),IN VAL3 VARCHAR(255),IN VAL4 VARCHAR(255))
LANGUAGE SQL
BEGIN ATOMIC
UPDATE SCHEMA1.TABLE1 SET col1=VAL2, col2=VAL3, col3=VAL4 WHERE col1=VAL1;
UPDATE SCHEMA2.TABLE1 SET col1=VAL3, col2=VAL3, col3=VAL4 WHERE col1=VAL1;
FOR v AS
SELECT col1 FROM SCHEMA2.TABLE2 WHERE col2=VAL1
DO
UPDATE SCHEMA2.TABLE3 SET col2=VAL2 WHERE col1=v.col1 AND col3='<<value>>';
UPDATE SCHEMA2.TABLE3 SET col2=VAL3 WHERE col1=v.col1 AND col3='<<value2>>';
END FOR;
END
#
In the FOR statement, I even tried like this FOR v AS cur1 CURSOR FOR
Used this to compile the procedure first -- db2 -td# -vf myprocname.db2
Then, tried executing the procedure using -- db2 call MYPROCNAME 'val1','val2','val3','val4'
Also, tried the execution in diff combinations like db2 call MYPROCNAME ('val1','val2','val3',val4'), db2 call MYPROCNAME("val1","val2","val3","val4"), etc...
If I use () in the db2 call, I get the bash: syntax error near unexpected token (' error.
PS: I am using db2 11.5

As mentioned in the comment to my question above, running the following db2 "call MYPROCNAME ('val1', 'val2', 'val3', 'val4')" fixes the issue

Related

DB2-12 (Z/OS) - Create Alias for Stored Procedure

it's possible to define an alias for a stored procedure (sql-native)?
In the documentation, reference is made only to: SEQUENCE and TABLES
1) CREATE PROCEDURE OWXXCOLL.STORED1()
2) CREATE ALIAS DB2C.STORED1 FOR OWXXCOLL.STORED1;
3) CALL DB2C.STORED1();
EDIT 2021-05-14
The original question arises for the following problem (I was hoping to get away with using aliases)
Intro
I have defined a native SP
The OWXXCOLL schema is the same one I also use for tables/index...
(I noticed that the tables also have different aliases)
CREATE PROCEDURE OWXXCOLL.STORED1(...)
LANGUAGE SQL
ISOLATION LEVEL CS
WLM ENVIRONMENT FOR DEBUG MODE WLMENV1
ALLOW DEBUG MODE
BEGIN
...
END#
I also modified the cobol program (name:PGMSTO1) to call the Stored with the CALL statement (without qualifier)
EXEC SQL
CALL STORED1 (...)
END-EXEC.
The problem
The various table accesses (SELECT) work correctly BUT When I run the PGMSTO1 the call to the Stored ends with sqlcode -440
NO AUTHORIZED PROCEDURE BY THE NAME STORED1 HAVING COMPATIBLE ARGUMENTS WAS FOUND<
The error comes from the fact that it is not using the owner OWXXCOLL but DB2C (DB2C is user who scheduled the jcl/batch)
If I enter the qualifier (OWXXCOLL) the call it's OK.
I don't understand what to check and what configurations are missing.
Thanks
The cobol program (PGMSTO1) that calls the stored procedure has the following BIND parameters:
COLLID NAME OWNER CREATOR QUALIFIER DYNAMICRULES PATHSCHEMAS
OWXXCOLL PGMSTO1 DB2C DB2C FPXX B "DB2C"
"DB2C" path is used to resolve unqualified stored procedure
I will need to modify the bind parameter "PATH".
I will try to ask to add my schema as well (OWXXCOLL)
PATH("OWXXCOLL","DB2C")

On IBM i, how to run a stored procedure via DB2 command of QShell?

How would you run a stored procedure via DB2 command of QShell, as I need a simple way to unit test a change to a stored procedure?
On IBM i
Started qshell with
QSH
Entered these db2 commands in various formats without success
db2 call libraryname.stroredprocedurename('param1value' 'param2value' ?)
db2 call libraryname.stroredprocedurename ('param1value' 'param2value' ?)
db2 call libraryname.stroredprocedurename (param1value param2value ?)
Only reference source I could find
Have tried using CLP to call stored proceduresbut there are no CLP examples
here
You can also use the JDBC client included with jt400.jar. You can run it from QSH by using the following command.
java -cp /qibm/proddata/os400/jt400/lib/jt400.jar com.ibm.as400.access.jdbcClient.Main jdbc:db2:localhost
The client will also handle stored procedure output parameters as shown by the following example.
create procedure add1(in inparm int, out outparm int) language sql begin set outparm = inparm +1; end
call add1(1,?)
Parameter 1 returned 2
I use Squirrel SQL Client http://squirrel-sql.sourceforge.net/ to test all of my SQL.
call libraryname.stroredprocedurename('param1value', 'param2value')
Note the separator is a comma ,, and ? is not a valid parameter marker when called interactively like this.
In addition to the client, you will need a JDBC driver. You can use the JTOpen driver for IBM i found here: http://jt400.sourceforge.net/
From the CL command line, or inside a CL program, you can use the RUNSQL command to execute an SQL statement.
If you're going to be trying multiple ad hoc SQL statements, you might use the STRSQL command. Personally, I tend to use the SQL window provided as part iNavigator.
Finally got the basic syntax
db2 "CALL lib.proc ('parmvalue1')"
Which resulted in:
DB20000I THE SQL COMMAND COMPLETED SUCCESSFULLY.

Error while creating a simple DB2 sql procedure

I am trying to create a db2 procedure on DB2 z/OS 10.1.5. The code I am using is :
CREATE PROCEDURE WSDIWW16.CALCULATE_SALARY()
LANGUAGE SQL
BEGIN ATOMIC
update wsdiww16.emptable
set dailywage = dailywage * 30;
END;
I am getting sqlcode - 104
SQL0104N An unexpected token "<END-OF-STATEMENT>" was found following "".
Expected tokens may include: "DECLARE".
I haven't created an sql procedure earlier. Can somebody please help ?
You've got two semicolons in that CREATE statement. The first one needs to be a different character that's recognized as a statement terminator.
What development environment are you using? In Rational Developer for z, you can right-click the editor and select the Set Statement Terminator option:
1
Similar options should be available in Data Studio, etc.
You should use end line terminator such as '/' and execute your query using below command -
db2 +c -td/ -vf SQL_file_name
HTH
The answer with the graphical menu worked for me. From inside my SQL script I did a right click anywhere in the white space. The menu above then appeared. I then left clicked on "Set Statement Terminator" (as requested). I then changed my terminator character to "#" (no quote marks). After that the semicolon character ";" started working again.
I hope this helps.
I believe you need to end your procedures with a '/'.

db2 stored procedure error when update statement follows rollback

I have a stored procedure where at the end I check for errors and if there are errors I perform a rollback and then update the status on the batch table to 'FAILED'. When I run the stored procedure I regularly get an SQLCODE 818 error saying there is 'a timestamp conflict occurred'.
When I remove the update statement that changes the status on the batch table, I do not get the error.
What is the best practice to perform these actions so I avoid getting the error?
The section of code looks like this:
IF v_error_count > 0 THEN
-- Batch failed
ROLLBACK;
UPDATE batch_table bt
SET bt.batch_status = 'FAILED'
WHERE batch_id = input_batch_id;
END IF;
Thanks for any help.
SQL Code -818 indicates that the internal timestamp DB2 uses to ensure consistency between the running module matches the DBRM version created when the SQL Statements were precompiled.
You might check with your DBA (or someone else at your site), because the specific steps you have to perform may vary. For a general overview, you can see this article on IBM Knowledge Center.

Magento: Create Stored Procedure

I am trying to create a Magento module installer, that will in turn create a stored procedure.
The code for the procedure has been run through: Toad, phpmyadmin, and mysql.exe command line. It worked in all 3. However, it fails when Magento goes to execute it during the installation process.
Error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
Magento Code:
$installer = $this;
$installer->startSetup();
$sql = <<<____SQL
DELIMITER //
CREATE PROCEDURE GetStuff(
IN pSomeId int(11)
)
BEGIN
DECLARE pOtherId INT;
SELECT some_var INTO pOtherId FROM some_table WHERE id = pSomeId;
END;//
DELIMITER ;
____SQL;
$installer->run($sql);
$installer->endSetup();
There's some other code in between, of course, but simplifying did not change the error.
My guess is that it is something to do with Delimeters and how PDO/Magento is treating them. Thank you.
I figured it out.
I did not need to use any delimiters, PDO can take care of them on its own.
But I did need to switch the method from query (used by the installer) to exec.
So, I ended up with:
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->exec($sql);

Resources