Create Function - PIPELINED DB2 - db2

Does someone know how I can create that query below on DB2 database?
FUNCTION GPRS7001(
pe_sTexto VARCHAR2,
pe_sDelimitador VARCHAR2)
RETURN v_ttTabela PIPELINED;
I don't how I create PIPELINED table on DB2 database.
I tried to execute this:
CREATE OR REPLACE FUNCTION GPRS7001( pe_sTexto VARCHAR(300))
RETURNS TABLE( pe_sTexto VARCHAR(300)
,pe_sDelimitador VARCHAR(300)
)
LANGUAGE SQL
RETURNS -- WTF ;
Db2 version 10.5

If you properly configure your Db2 database for Oracle-emulation, then Db2 may run a pipelined function without changes.
Refer to the documentation on V10.5 Knowledge Center for details of PL/SQL support, PL/SQL pipelined functions, and the PIPE statement.

Related

How to do a sub procedure in sql / pl

I am doing a procedure migration in pl / sql (oracle) to sql / pl (DB2) and I don't know how to pass a subprocedure to DB2
As I am not a system administrator I cannot change DB2 to be pl / sql compatible
EXAMPLE
create or replace PROCEDURE "SP_NOSTRADAMUS_PRODUTO"
AS
V_EXISTE_TAB NUMBER := 0;
PROCEDURE PR_HIRQ_PRODUTO_OR
AS
BEGIN
END
If your Db2-server runs on Linux/Unix/Windows, in ANSI SQL PL, if you have a local procedure nested within the body of another SQL PL procedure (or compound statement), then you use the DECLARE keyword to define the local procedure. This is supported by Db2-LUW in versions v10.1 and higher.
If you have very many Oracle PL SQL stored procedures, please discuss with your solution architect (if you have one) and your DBA about the possibility of configuring your Db2 server to support Oracle compatibility . This may save you some money.
You can see the Db2 syntax for SQL PL here (be sure to select the correct version of your Db2-server product in the pull down list).
There are some restrictions on local procedures, so study the documentation carefully. But your local procedure can access variables and objects defined in surrounding block(s) that are in scope.
Your example might look like this in ANSI SQL PL:
create or replace PROCEDURE "SP_NOSTRADAMUS_PRODUTO"
begin
declare V_EXISTE_TAB integer default 0;
declare PROCEDURE PR_HIRQ_PRODUTO_OR
BEGIN
-- body of pr_hirq_produto_or procedure
END;
-- body of sp_nostradamus_produto procedure
END
#
Db2 does not have subprocedures, not even sure why oracle would. In db2 you just create the 'sub' procedures as regular procedures and then 'CALL' them.
create procedure a( ....
create procedure b ....
create procedure main(...)
begin
call a(...);
call b(...);
end

How to use DO in postgres

I am attempting to get a better understanding of the DO command in postgreSQL 9.1
I have following code block,
DO
$do$
BEGIN
IF 1=1 THEN
SELECT 'foo';
ELSE
SELECT 'bar';
END IF;
END
$do$
However it returns the following error:
[42601] ERROR: query has no destination for result data Hint: If you want to discard the results of a SELECT, use PERFORM instead. Where: PL/pgSQL function "inline_code_block" line 4 at SQL statement
PostgreSQL DO command creates and executes some specific short life function. This function has not any interface, and then it cannot to return any output other then changes data in tables and some debug output.
Your example has more than one issues:
Only PostgreSQL table functions can returns some tabular data. But the mechanism is significantly different than MSSQL. PostgreSQL user's should to use RETURN NEXT or RETURN QUERY commands.
CREATE OR REPLACE FUNCTION foo(a int)
RETURNS TABLE(b int, c int) AS $$
BEGIN
RETURN QUERY SELECT i, i + 1 FROM generate_series(1,a) g(i);
END;
$$ LANGUAGE plpgsql;
SELECT * FROM foo(10);
DO anonymous functions are not table functions - so no output is allowed.
PostgreSQL 9.1 is not supported version, please upgrade
If you have some experience only from MSSQL, then forget it. A concept of stored procedures of PostgreSQL is very similar to Oracle or DB2, and it is significantly different to MSSQL does. T-SQL integrates procedural and SQL constructs to one set. Oracle, PostgreSQL, ... procedural functionality can embedded SQL, but procedural functionality is not integrated to SQL.
Please, read PostgreSQL PLpgSQL documentation for better imagine about this technology.

Temp table for user defined functions

I am trying to use temp table in user defined function for DB2. I am trying to do this in data studio but the code below is not working. How can I make this work?
Thanks.
CREATE FUNCTION BELSIZE.TEST (aSTRING VARCHAR(50))
RETURNS TABLE(
column1 INTEGER
)
F1: BEGIN ATOMIC
DECLARE c1 CURSOR WITH RETURN TO CLIENT FOR stmt;
SET v_dynStmt = 'SELECT 1 column1 from sysibm.sysdummy1';
PREPARE stmt FROM v_dynStmt;
OPEN c1;
RETURN
END
You have syntax errors in your code, more details below.
Apart from syntax errors, your title mentions temp table but your code does not, so your question is poor.
Never write "...is not working", instead write the exact SQLCODE and SQLSTATE and message that you see.
When asking for help with Db2, always write in the question the Db2-version and the operating-system (Z/OS, i-Series, Linux/Unix/Windows) on which the Db2-server runs, because the answer can depend on those facts. Different versions of Db2 for different operating systems have different capabilities and different syntax.
If you want to use cursors for result-sets then use SQL PL stored-procedures, because there are fewer restrictions.
SQL table functions are suitable when you don't need to declare a cursor for result-set.
Db2-LUW prevents you from declaring a cursor in an SQL table function when you use BEGIN ATOMIC.
If you are not using BEGIN ATOMIC, Db2-LUW (current versions, i.e. v11.1) lets you declare a cursor in an SQL UDF but you cannot use that cursor directly to return the result-set, as you can do inside SQL PL stored procedures.
For your example, the syntax below is valid and also useless, so consider using an SQL PL procedure instead:
--#SET TERMINATOR #
CREATE or replace FUNCTION BELSIZE.TEST (aSTRING VARCHAR(50))
RETURNS TABLE( column1 INTEGER)
language sql
specific belsize.test
BEGIN atomic
RETURN select 1 as column1 from sysibm.sysdummy1 ;
END
#
select * from table(belsize.test('a')) as t
#

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.

PostgreSQL execute statement conditionally by server version

I'm currently writing some installer script that fires SQL files against different database types depending on the system's configuration (the webapplication supports multiple database server like MySQL, MSSQL and PostgreSQL).
One of those types is PostgreSQL. I'm not fluent with it and I would like to know if it's possible to make a statement into a define/populate SQL file that makes an SQL query conditional to a specific PostgreSQL server version.
How to make an SQL statement conditionally in plain PGSQL so that it is only executed in version 9? The command is:
ALTER DATABASE dbname SET bytea_output='escape';
The version check is to compare the version with 9.
Postgres does have version() function, however there is no major_vesion(). Assuming that output string always includes version number as number(s).number(s).number(s) you could write your own wrapper as:
CREATE OR REPLACE FUNCTION major_version() RETURNS smallint
AS $BODY$
SELECT substring(version() from $$(\d+)\.\d+\.\d+$$)::smallint;
$BODY$ LANGUAGE SQL;
Example:
=> Select major_version();
major_version
---------------
9
(1 row)
However real issue here is that AFAIK you can't execute your commands conditionally in "pure" SQL and best what you can do is to write some stored function like this:
CREATE OR REPLACE FUNCTION conditionalInvoke() RETURNS void
AS $BODY$
BEGIN
IF major_version() = 9 THEN
ALTER DATABASE postgres SET bytea_output='escape';
END IF;
RETURN;
END;
$BODY$ LANGUAGE plpgsql;
I think that you should rather use some scripting language and generate appropriate SQL with it.
Or you could just use
select setting from pg_settings where name = 'server_version'
Or
select setting from pg_settings where name = 'server_version_num'
If you need major version only
select Substr(setting, 1, 1) from pg_settings where name = 'server_version_num'
or
select Substr(setting, 1, strpos(setting, '.')-1) from pg_settings where name = 'server_version'
if you want it to be compatible with two digit versions.
Maybe you could make things dependent on the output of
select version();
(probably you'll have to trim and substring that a bit)
BTW (some) DDL statements may not be issued from within functions; maybe you'll have to escape to shell-programming and here-documents.