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.
Related
using pgadmin4, postgres 9.6 on windows 10
I'm trying to use parameter to specify table name in a prepared statement as in the code below. However I do get a syntax error as below. Note that I'm able to use the parameters with a where condition et al.
Query
prepare mySelect(text) as
select *
from $1
limit 100;
execute mySelect('some_table');
pgAdmin message
ERROR: syntax error at or near "$1"
LINE 3: from $1
^
SQL state: 42601
Character: 50
It is not possible. The prepare statement is persistent execution plan - and execution plan contains pined source of data - so tables, column names cannot be mutable there.
When you change table, columns, then you change the semantic of query - you will got different execution plan and then this behave is not possible in prepared statements. The main use case of prepared statements is reusing of execution plans - plan once, execute more. But there are some principal limits - only some parameters can be changed.
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.
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.
I am trying to use HSQL for testing. In production, the code runs against a DB2 database. I would like to turn on the DB2 syntax of HSQL and run the production SQL against it (or as much of it as possible).
According to the documentation, in DB2 syntax mode, the nextval for expression is supported.
I've turned on DB2 syntax mode both with the connection property, sql.syntax_db2=true, and with:
set database sql syntax db2 true;
However, I am not able to use nextval for, though the syntax next value for works correctly:
values (next value for test_seq); -- returns the next value
values (nextval for test_seq); -- results in an error
The error is:
Error: user lacks privilege or object not found: NEXTVAL
SQLState: 42501
ErrorCode: -5501
Am I not correctly activating DB2 syntax mode, or am I doing something else wrong? I am logged in as SA, so can it be a privilege issue?
There is a mistake in the documentation. The DUAL table is supported the same way as in Oracle syntax, but NEXTVAL is an Oracle form used as test_seq.NEXTVAL.
The DB2 supports NEXT VALUE FOR test_seq and allows NEXTVAL as an alternative to NEXT VALUE. This will be supported in the next release of HSQLDB.
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.