DB2 z/OS: Create function failed with SQLCODE = -104 - db2

Below create function statement works fine on DB2 LUW, but failed on DB2 V9.1 for z/OS.
CREATE FUNCTION PLAT_FN_GASN (
OBJID CHARACTER(18) )
RETURNS CHARACTER(18)
LANGUAGE SQL
READS SQL
DATA DETERMINISTIC
NO EXTERNAL ACTION
RETURN SELECT SRCOBJID
FROM PLAT_V_RELATIONSHIPS AS R2
WHERE RELTYPENAME = 'ApplyTaskRefOutputSpec' AND MODELNAME = 'MiningTask'
AND TGTOBJID = (SELECT SRCOBJID
FROM PLAT_V_RELATIONSHIPS AS R1
WHERE RELTYPENAME = 'ApplyOutputHasContentItems' AND MODELNAME = 'MiningTask'
AND TGTOBJID = OBJID)
Error message is:
ILLEGAL SYMBOL "SRCOBJID". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: SOURCE PARAMETER STOP INHERIT RETURNS CALLED ALLOW CONTAINS. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.53.71
I can't figure out what's wrong with the SQL statement. Can someone give me a clue?
Thanks.

I've found the reason is DB2 for z/OS V9 doesn't support scalar select in RETURN for an SQL UDF. The syntax is correct although. DB2 for z/OS V10 should support this.
Thanks.

Related

DB2 FUNCTION TEST ERROR

HOW to resolve this problem ? :
CREATE OR REPLACE FUNCTION "BADIL_PROD"."FCT_UPDATE_POINTAGE" (id_projet in NUMBER)
return NUMBER
is
Results NUMBER;
CURSOR cur_update IS
SELECT distinct t.ID_TACHE,t.id_collaborateur,t.id_ref_rubrique_activite
FROM TACHES t
where t.ID_PROJET =id_projet ;
begin
dbms_output.enable(200000);
FOR cur in cur_update
LOOP
BEGIN
dbms_output.put_line(cur.ID_TACHE || '--' || cur.ID_COLLABORATEUR || '--' || cur.ID_REF_RUBRIQUE_ACTIVITE);
UPDATE POINTAGES p SET p.ID_TACHE_INCIDENT = cur.ID_TACHE
where p.id_projet_tma=id_projet
and p.ID_COLLABORATEUR = cur.ID_COLLABORATEUR
AND p.ID_REF_RUBRIQUE_ACTIVITE = cur.ID_REF_RUBRIQUE_ACTIVITE;
END;
END LOOP;
results:=0;
return(results);
end fct_update_pointage;
So when I want to test this function I get this error in data studio :
(specific name "FCT_UPDATE_POINTAGE") attempted to modify data but was
not defined as MODIFIES SQL DATA.. SQLCODE=-577,
This worked for me with DB2 V11 on Linux:
db2set DB2_COMPATIBILITY_VECTOR=830
db2stop
db2start
db2 create database mydb
db2 connect to mydb
db2 "create table taches(....)"
db2 "create table pointages(...)"
db2 -tvf FCT_UPDATE_POINTAGE.sql
(where FCT_UPDATE_POINTAGE.sql contains your function definition )
However, note the documented restriction in the documentation "Compiled SQL functions (including PL/SQL functions) that MODIFY SQL DATA can only be used as the only element on the right side of an assignment statement that is within a compound SQL (compiled) statement."
DB2 Version 9.7 allowed the "modifies sql data" clause when defining PL/SQL user defined functions, but IBM removed that ability at DB2 Version 10.1 and higher. Maybe you need to convert to an sproc.

Error using regular expressions in DB2 - wrong syntax?

i tried to run this query in DB2 ( which includes regex ). I am getting the following error. Can someone help?
Here is the query:
SELECT COUNT(*) FROM TABLE WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]')
Support for BOOLEAN data type is new in Db2 11.1.1.1 (i.e. the first Mod Pack + Fix pack for Db2 11.1). If you are only on Db2 11.1.0.0, then you will need to explicitly test the result of your regex function.
SELECT COUNT(*) FROM TABLE
WHERE REGEXP_LIKE(TRIM(FIELD), '[^[:digit:]]') = 1;

How to create user defined function in DB2 Z/OS 8.1 version?

This is my code: I trying to create UDF function for z/os from Data Studio . I don't need to use External function or others. I need to execute this SQL Function .
CREATE FUNCTION FUNCTION5()
RETURNS FLOAT
language sql
DETERMINISTIC
READS SQL DATA
NO EXTERNAL ACTION
BEGIN
DECLARE RANVAL FLOAT ;
SELECT RAND() INTO RANVAL FROM SYSIBM.SYSDUMMY1;
RETURN RANVAL ;
END
But, while executing the above code, i'm getting this error. Can yuou please help me to figure it out please.
I want to develop nearly 20 Scalar UDF on DB2 Z/OS . kindly help me, please.
Deploy [MeDB]MeDB.FUNCTION5
Running
MeDB.FUNCTION5 - Deploy started.
Create user-defined function returns SQLCODE: -199, SQLSTATE: 42601.
MeDB.FUNCTION5: 0: DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=DECLARE;ON AFTER <INTEGER>, DRIVER=4.18.60
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=DECLARE;ON AFTER <INTEGER>, DRIVER=4.18.60
MeDB.FUNCTION5 - Deploy failed.
MeDB.FUNCTION5 - Roll back completed successfully.
I believe the function you're trying to create is not supported in DB2 7. The usage of the BEGIN, END, and DECLARE keywords indicate you're trying to use SQLPL, which isn't supported until DB2 10 as compiled scalar functions. I don't have any documentation for 7, but I think you can only create inline scalar functions, which is limited to one expression in a RETURN statement as described in the DB2 10 reference for CREATE FUNCTION (inline scalar):
The CREATE FUNCTION (inlined SQL scalar) statement defines an SQL scalar function at the current server and specifies an SQL procedural language RETURN statement for the body of the function. The function returns a single value each time it is invoked.
SQL-routine-body
Specifies a single RETURN statement.

Create UDF in DB2 on z/os V11 - mutually exclusive clauses

I want to play a little bit with UDFs but I am stuck just at the start: I even can't get the examples from the manuals to work.
The SYSIBM.Version variable for my DB2 system is DSN11015.
Executing the sample code:
CREATE FUNCTION FINDSTRING (BLOB(500K), VARCHAR(200))
RETURNS INTEGER
CAST FROM FLOAT
SPECIFIC FINDSTRINBLOB
EXTERNAL NAME FNDBLOB
LANGUAGE C
PARAMETER STYLE SQL
NO SQL
DETERMINISTIC
NO EXTERNAL ACTION
FENCED
STOP AFTER 3 FAILURES;
Results in a
DSNT408I **SQLCODE = -628**, ERROR: THE CLAUSES ARE MUTUALLY EXCLUSIVE
DSNT418I SQLSTATE = 42613 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNHSMS1 SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 9 0 0 -1 73 2905 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'00000009' X'00000000' X'00000000'
X'FFFFFFFF' X'00000049' X'00000B59' SQL DIAGNOSTIC
INFORMATION
BPA0012E: DB2 SQL/DDL ERROR HAS OCCURRED - ROLLBACK ISSUED.
I have no ideas what clauses are mutually exclusive here...
According to the syntax diagram in the manual (http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2z11.doc.sqlref/src/tpc/db2z_sql_createfunctionexternalscalar.htm) the sequence of options is significant. You may want to try putting DETERMINISTIC and FENCED (in that sequence) before NO SQL.

Table Valued User Defined Functions in DB2 Z/OS

Does anyone know if DB2 v9.1 z/OS supports Table Valued User Defined Functions?
This is what I am trying to create but I keep getting the error message below.
CREATE FUNCTION func_test(v_vchCol CHAR(10))
RETURNS TABLE(col_a char(10), row_cnt integer)
LANGUAGE SQL
SPECIFIC FUNCINFO
NOT DETERMINISTIC
READS SQL DATA
return
select col_1, count(*)
from SCHEMA_NAME.TEST1
where col_1 = v_vchCol
group by col_1;
Error Message:
ERROR [56038] [IBM][DB2] SQL0969N There is no message text
corresponding to SQL error "-4700" in the message file on this
workstation. The error was returned from module "DSNHSMS1" with
original tokens "". SQLSTATE=56038
Any help would be much appreciated
Yes, but it appears to require new function mode which apparently isn't enabled yet in the DB2 instance you're connected to.