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

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.

Related

Getting invalid SQL syntax upon executing stored procedure via DB2 CLI

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

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

BigQuery stored procedure access through simba jdbc driver

All,
I am trying to execute a stored procedure in google bigquery using a jdbc connection but having no luck with it.
I followed the example here https://cloud.google.com/blog/products/data-analytics/command-and-control-now-easier-in-bigquery-with-scripting-and-stored-procedures.
I am able to create and call the stored procedure from bigquery web ui but when i set up a data source in intellij using the simba driver as explained here https://blog.jetbrains.com/datagrip/2018/07/10/using-bigquery-from-intellij-based-ide/
Then when i run the call to execute the stored procedure
DECLARE target_employee_id INT64 DEFAULT 9;
DECLARE employee_hierarchy ARRAY<INT64>;
dataset.GetEmployeeHierarchy(target_employee_id, employee_hierarchy);
SELECT target_employee_id, employee_hierarchy;
i get an error
[HY000][100032] [Simba][BigQueryJDBCDriver](100032) Error executing query job. Message: Unrecognized name: target_employee_id at [1:8] com.simba.googlebigquery.support.exceptions.GeneralException: [Simba][BigQueryJDBCDriver](100032) Error executing query job. Message: Unrecognized name: target_employee_id at [1:8]
As the stored procedure support is in pre-release beta, I am wondering if the Simba driver supports it as of now?
If not are there any other jdbc drivers that do currently?
So turns out all the 4 statements need to be executed as a single Bloc not as individual statements.
In intellij you can highlight all those statements and hit run to execute it and return the results.
In Java just pass them as a single string in statement.execute.

Unable to run a stored procedure in snowflake database

CREATE OR REPLACE PROCEDURE wh.sp_schema.my_sp(arg1 STRING, arg2 STRING)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
AS
$$
stmt = snowflake.createStatement(
{sqlText: `CREATE OR REPLACE TABLE wh.table_schema.RAW_`+arg2+`_`+arg1+` LIKE wh.temp_schema.RAW_`+arg2+`_TEMPLATE`}
);
rs = stmt.execute();
rs.next();
output = rs.getColumnValue(1);
return output;
$$
;
when i create the above stored procedure - below message is displayed
Function my_sp successfully created.
when i run the stored procedure from the worksheet using
CALL my_sp('2018','abc');
I see the below error:
SQL compilation error: Invalid identifier my_sp
I even tried running using fully qualified name for the stored procedure:
CALL wh.sp_schema.my_sp('2018','abc');
I still see the below error:
SQL compilation error: Invalid identifier my_sp.
Also, i would like to know a command to see if my stored procedure has been created or not.
I've looked in the snowflake UI, under 'wh" warehouse and "sp_schema" schema, but could not see anything(stored procedures) other than tables.
This is a simple case of a grants issue. As SECURITYADMIN, if you apply the necessary grants to the role you're using,
GRANT USAGE ON DATABASE wh TO ROLE <role>;
GRANT USAGE ON SCHEMA wh.sp_schema TO ROLE <role>;
GRANT USAGE ON ALL PROCEDURES IN SCHEMA wh.sp_schema TO ROLE <role>;
you'll get past this ambiquous error and get to the next real error
JavaScript execution error: Uncaught ReferenceError: arg2 is not defined in MY_SP
Which is due to javascript SP parameters being case sensitive, so you need to modify the code as follows (change arg to ARG):
{sqlText: `CREATE OR REPLACE TABLE wh.table_schema.RAW_`+ARG2+`_`+ARG1+` LIKE wh.temp_schema.RAW_`+ARG2+`_TEMPLATE`}
Snowflake does not support Stored Procedures. They are implementingjavascript` based APIs for it, but it's still in draft mode.
Here's the link to their APIs (Note Draft Document is written on right hand side).
and here's the link to a ticket on their support forum where they've confirmed it.
There are two obvious possibilities why this may go wrong:
You've implicitly donated the procedure ownership to a ROLE you don't have
There is now a mismatch between the procedure and call type signatures. Verify that there is a wh.sp_schema.my_sp(STRING, STRING) and that your call is like CALL wh.sp_schema.my_sp('2018'::STRING,'abc'::STRING);
Try to solve the error by following query syntax.
CALL database_name.schema.MY_SP('database_name','schema');

Is this a bug: stored procedure runs fine without parameters, causes error when parameters are added?

I have a stored procedure created in MySQL DB. This database is accessed as linked server through Microsoft SQL Server 2012 using Microsoft SQL Server Management Studio. The provider selected while creating linked server is "Microsoft OLE DB Provider for ODBC Drivers".
When I run following as text from Report builder 3.0 it runs fine and fetch data.
EXEC('CALL storedProcedureName(''string1'', ''string2'', ''string3'')') AT LinkedServerName;
But when I try to replace string1, string2, string3 with parameter name parameter1, parameter2, parameter3 as:
EXEC('CALL storedProcedureName(#parameter1, #parameter2, #parameter3)') AT LinkedServerName;
I get error:
Could not execute statement on remote server 'LinkedServerName'.
(Microsoft SQL Server, Error: 7215)
And when I try:
EXEC('CALL storedProcedureName('#parameter1', '#parameter2', '#parameter3')') AT LinkedServerName;
I get the prompt to enter values for parameter1, parameter2, parameter3. But when I enter the values and click ok, I get error:
Incorrect syntax near '#parameter1'. (Microsoft SQL Server, Error: 102)
Question: Am I missing something in syntax or is this a bug?
The linked server has:
"RPC" and "RPC out" set to True.
And the OLEDB provider has:
Enabled allow inprocess
Enabled dynamic parameter
I believe you have to call it like the below. So params become strings wrapped in single quotes:
EXEC('CALL storedProcedureName('''+#parameter1+''', '''+#parameter2+''', '''+#parameter3+''')') AT LinkedServerName;
I know this is an older question but the accepted answer is open to SQL Injection and I think it's important to offer a more secure method.
You really want to use a parameterized query
EXEC('CALL storedProcedureName(?,?,?)',#parameter1, #parameter2, #parameter3) AT LinkedServerName;
I know this works for oracle and it should work for MySql as well.

Resources