dbTalk Databases Forums  

DB2 UDF Problem

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss DB2 UDF Problem in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gilroy Gonsalves
 
Posts: n/a

Default DB2 UDF Problem - 06-15-2010 , 11:59 AM






Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix VARCHAR(100) )
RETURNS integer
SPECIFIC TRUNCATE_TABLES
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION


------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
CASE (P_TABLE_NAME)
WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
WHEN 'TB04' THEN DELETE FROM TB04 WHERE col1=p_suffix
END
{code}


When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type
2) Do i need to modify any thing in the above mentioned UDF.

Thanks Gilroy

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: DB2 UDF Problem - 06-16-2010 , 12:27 AM






On 2010-06-15 17:59, Gilroy Gonsalves wrote:
Quote:
Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix VARCHAR(100) )
RETURNS integer
SPECIFIC TRUNCATE_TABLES
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION


------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
CASE (P_TABLE_NAME)
WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
WHEN 'TB04' THEN DELETE FROM TB04 WHERE col1=p_suffix
END
{code}


When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type
The main purpose of a function is to return something, but it may under
restricted circumstances have side effects. Not the other way around.

Quote:
2) Do i need to modify any thing in the above mentioned UDF.

What you describe does not really belong in a sql function, but you can
squeeze it in. First, MODIFIES SQL DATA requires that the function
returns a table not a scalar (and since you modifies sql data, you have
to specify that). Something like:

create function myfun (
p_table_name VARCHAR(100),
p_suffix VARCHAR(100)
)
returns table( x int )
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC

if p_table_name = 'TB01' then
delete from tb01 where col1 = p_suffix;
elseif p_table_name = 'TB02' then
delete from tb02 where col1 = int(p_suffix);
end if;

return values (1);
END
@

You call a table function like:

select * from table(myfun('TB01', '3'));


I don't think it is a good idea to use functions this way, but if it is
absolutely necessary you may succeed with something like the above.


/Lennart


> Thanks Gilroy

Reply With Quote
  #3  
Old   
Gilroy Gonsalves
 
Posts: n/a

Default Re: DB2 UDF Problem - 06-16-2010 , 06:27 AM



On Jun 16, 5:27*am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-06-15 17:59, Gilroy Gonsalves wrote:



Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix *VARCHAR(100) *)
* *RETURNS integer
* *SPECIFIC TRUNCATE_TABLES
* *LANGUAGE SQL
* *MODIFIES SQL DATA
* *NO EXTERNAL ACTION

------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
* *CASE (P_TABLE_NAME)
* *WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
* *WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
* *WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
* *WHEN 'TB04' THEN DELETE FROM TB04 WHERE *col1=p_suffix
* *END
{code}

When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type

The main purpose of a function is to return something, but it may under
restricted circumstances have side effects. Not the other way around.

2) Do i need to modify any thing in the above mentioned UDF.

What you describe does not really belong in a sql function, but you can
squeeze it in. First, MODIFIES SQL DATA requires that the function
returns a table not a scalar (and since you modifies sql data, you have
to specify that). Something like:

create function myfun (
* * p_table_name VARCHAR(100),
* * p_suffix *VARCHAR(100)
)
returns table( x int )
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC

* * if p_table_name = 'TB01' then
* * * * delete from tb01 where col1 = p_suffix;
* * elseif p_table_name = 'TB02' then
* * * * delete from tb02 where col1 = int(p_suffix);
* * end if;

* * return values (1);
END
@

You call a table function like:

select * from table(myfun('TB01', '3'));

I don't think it is a good idea to use functions this way, but if it is
absolutely necessary you may succeed with something like the above.

/Lennart

Thanks Gilroy


Hi Lennart,

Much appreciated for your help. I followed the method of using SQL
Procedure. But since we are migrating our environment from Oracle to
DB2, some of the data stage Jobs uses Oracle function which requires
the table to be truncated as SELECT query. As per current project
timeline, we are simply migrating it to DB2 maintaining the same
Oracle function/Procedure in DB2 without changing data stage jobs.

Do you have any suggestion, of achieving the same case with different
alternative. Again, DELETE is something which i am unhappy off. 50
millions rows each day populate this table and delete this 50 million
each day as part of Nightly batch. So the LOGGING which the DELETE
stmt will do is what concerns me a lot.

The tables is currently MDC, PARTITION and distributed, and i believe
DETACH...followed by ADD partition.......then DROP DETATACHED
partition is something which is ticking my head. Truncate is part of
NIGHTLY BATCH only.

Any suggestions.

Thanks
Gilroy

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: DB2 UDF Problem - 06-16-2010 , 02:34 PM



On 2010-06-16 12:27, Gilroy Gonsalves wrote:
Quote:
On Jun 16, 5:27 am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com
wrote:
On 2010-06-15 17:59, Gilroy Gonsalves wrote:



Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix VARCHAR(100) )
RETURNS integer
SPECIFIC TRUNCATE_TABLES
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION

------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
CASE (P_TABLE_NAME)
WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
WHEN 'TB04' THEN DELETE FROM TB04 WHERE col1=p_suffix
END
{code}

When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type

The main purpose of a function is to return something, but it may under
restricted circumstances have side effects. Not the other way around.

2) Do i need to modify any thing in the above mentioned UDF.

What you describe does not really belong in a sql function, but you can
squeeze it in. First, MODIFIES SQL DATA requires that the function
returns a table not a scalar (and since you modifies sql data, you have
to specify that). Something like:

create function myfun (
p_table_name VARCHAR(100),
p_suffix VARCHAR(100)
)
returns table( x int )
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC

if p_table_name = 'TB01' then
delete from tb01 where col1 = p_suffix;
elseif p_table_name = 'TB02' then
delete from tb02 where col1 = int(p_suffix);
end if;

return values (1);
END
@

You call a table function like:

select * from table(myfun('TB01', '3'));

I don't think it is a good idea to use functions this way, but if it is
absolutely necessary you may succeed with something like the above.

/Lennart

Thanks Gilroy



Hi Lennart,

Much appreciated for your help. I followed the method of using SQL
Procedure. But since we are migrating our environment from Oracle to
DB2, some of the data stage Jobs uses Oracle function which requires
the table to be truncated as SELECT query. As per current project
timeline, we are simply migrating it to DB2 maintaining the same
Oracle function/Procedure in DB2 without changing data stage jobs.

Do you have any suggestion, of achieving the same case with different
alternative. Again, DELETE is something which i am unhappy off. 50
millions rows each day populate this table and delete this 50 million
each day as part of Nightly batch. So the LOGGING which the DELETE
stmt will do is what concerns me a lot.

The tables is currently MDC, PARTITION and distributed, and i believe
DETACH...followed by ADD partition.......then DROP DETATACHED
partition is something which is ticking my head. Truncate is part of
NIGHTLY BATCH only.


If you have range partitioning and can detach partitions I believe that
this will outperform the alternatives (cave eat, I haven't used r.p.
other than to play with, so there might be something lurking around).

The obvious question is how to do that from a function? Perhaps it is
possible to write an external function in say c, that can use some db2
api and solve the problem that way. If that is not possible I would look
into the possibility to do a call to a procedure from the function.

Sorry for not being of more help

/Lennart

Reply With Quote
  #5  
Old   
Gilroy Gonsalves
 
Posts: n/a

Default Re: DB2 UDF Problem - 06-17-2010 , 04:14 AM



On Jun 16, 7:34*pm, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-06-16 12:27, Gilroy Gonsalves wrote:



On Jun 16, 5:27 am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com
wrote:
On 2010-06-15 17:59, Gilroy Gonsalves wrote:

Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix *VARCHAR(100) *)
* *RETURNS integer
* *SPECIFIC TRUNCATE_TABLES
* *LANGUAGE SQL
* *MODIFIES SQL DATA
* *NO EXTERNAL ACTION

------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
* *CASE (P_TABLE_NAME)
* *WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
* *WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
* *WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
* *WHEN 'TB04' THEN DELETE FROM TB04 WHERE *col1=p_suffix
* *END
{code}

When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type

The main purpose of a function is to return something, but it may under
restricted circumstances have side effects. Not the other way around.

2) Do i need to modify any thing in the above mentioned UDF.

What you describe does not really belong in a sql function, but you can
squeeze it in. First, MODIFIES SQL DATA requires that the function
returns a table not a scalar (and since you modifies sql data, you have
to specify that). Something like:

create function myfun (
* * p_table_name VARCHAR(100),
* * p_suffix *VARCHAR(100)
)
returns table( x int )
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC

* * if p_table_name = 'TB01' then
* * * * delete from tb01 where col1 = p_suffix;
* * elseif p_table_name = 'TB02' then
* * * * delete from tb02 where col1 = int(p_suffix);
* * end if;

* * return values (1);
END
@

You call a table function like:

select * from table(myfun('TB01', '3'));

I don't think it is a good idea to use functions this way, but if it is
absolutely necessary you may succeed with something like the above.

/Lennart

Thanks Gilroy

Hi Lennart,

Much appreciated for your help. I followed the method of using SQL
Procedure. But since we are migrating our environment from Oracle to
DB2, some of the data stage Jobs uses Oracle function which requires
the table to be truncated as SELECT query. As per current project
timeline, we are simply migrating it to DB2 maintaining the same
Oracle function/Procedure in DB2 without changing data stage jobs.

Do you have any suggestion, of achieving the same case with different
alternative. Again, DELETE is something which i am unhappy off. 50
millions rows each day populate this table and delete this 50 million
each day as part of Nightly batch. So the LOGGING which the DELETE
stmt will do is what concerns me a lot.

The tables is currently MDC, PARTITION and distributed, and i believe
DETACH...followed by ADD partition.......then DROP DETATACHED
partition is something which is ticking my head. Truncate is part of
NIGHTLY BATCH only.

If you have range partitioning and can detach partitions I believe that
this will outperform the alternatives (cave eat, I haven't used r.p.
other than to play with, so there might be something lurking around).

The obvious question is how to do that from a function? Perhaps it is
possible to write an external function in say c, that can use some db2
api and solve the problem that way. If that is not possible I would look
into the possibility to do a call to a procedure from the function.

Sorry for not being of more help

/Lennart


Calling SQL procedure from UDF is restricted in DPF env. This is a
serious limitation that DB2 enforced.

What i will do if i am told to do is
1) Schedule TASK for Nightly batch before the data stage job is
executed
2) Detach the partition so the parition become independent partition
3) Add new partition
4) Will maintain window of 7 day to drop the detached partition
5) Data stage job execute
6) Run REORG and RUNSTATS on the table

I believe this is one thing which i am thinking off....


Thanks
Gilroy

Reply With Quote
  #6  
Old   
Gagan Kumar
 
Posts: n/a

Default Re: On 2010-06-15 17:59, Gilroy Gonsalves wrote:The main purpose of afunction is - 11-17-2010 , 03:56 PM



Simple scenario: Pass two parameters ID & TYPE based on TYPE select ID from 1 of the 5 Tables Table_A............Table_E -----> Return ID

I have tried it doing both ways using If ....elseif but keep getting th errors.

CREATE FUNCTION TEST.GET_CHILD_ID(
IN_ID BIGINT,
IN_TYPE BIGINT
)
RETURNS BIGINT
LANGUAGE SQL
DETERMINISTIC
READS SQL DATA
RETURNS NULL ON NULL INPUT
BEGIN ATOMIC
DECLARE CHILD_ID BIGINT;
CASE
WHEN IN_TYPE=1
THEN SELECT TABLE_A.ID INTO CHILD_ID FROM TEST.TABLE_A AS TABLE_A WHERE TABLE_A.TABLE_A_ID = IN_ID;
WHEN IN_TYPE=4
THEN SELECT TABLE_B.ID INTO CHILD_ID FROM TEST.TABLE_B AS TABLE_B WHERE TABLE_B.TABLE_B_ID = IN_ID;
WHEN IN_TYPE=2
THEN SELECT TABLE_C.ID INTO CHILD_ID FROM TEST.TABLE_C AS TABLE_C WHERE TABLE_C.TABLE_C = IN_ID;
WHEN IN_TYPE=0
THEN SELECT TABLE_D.ID FROM INTO CHILD_ID TEST.TABLE_D AS TABLE_D WHERE TABLE_D.TABLE_D_ID = IN_ID;
WHEN IN_TYPE=9
THEN SELECT TABLE_E.ID INTO CHILD_ID FROM TEST.TABLE_E AS TABLE_E WHERE TABLE_E.TABLE_E_ID = IN_ID;
END CASE;
RETURN CHILD_ID;
END;



Quote:
On Tuesday, June 15, 2010 11:59 AM Gilroy Gonsalves wrote:

Hi all,

I am trying to implement UDF which will truncate 4 tables as part of
UDF. The reason to choose DB2 UDF as apposed to DB2 Procedure is one
of our Data Stage job uses this function as part of SELECT query.

For example, if the datastage job wants to truncate any tables, it
uses following query
{code}SELECT TRUNCATE_TABLE_PARTITION('table-name','search-string')
from sysibm.sysdummy1;{code}

UDF Function that is created is

{code}
CREATE FUNCTION TRUNCATE_TABLE_PARTITION(p_table_name VARCHAR(100),
p_suffix VARCHAR(100) )
RETURNS integer
SPECIFIC TRUNCATE_TABLES
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION


------------------------------------------------------------------------
-- SQL UDF (Scalar)
------------------------------------------------------------------------
RETURN
CASE (P_TABLE_NAME)
WHEN 'TB01' THEN DELETE FROM TB01 WHERE col1=p_suffix
WHEN 'TB02' THEN DELETE FROM TB02 WHERE col1=INTEGER(p_suffix)
WHEN 'TB03' THEN DELETE FROM TB03 WHERE col1=INTEGER(p_suffix)
WHEN 'TB04' THEN DELETE FROM TB04 WHERE col1=p_suffix
END
{code}


When i try to implement this user defined function, i get SQLCODE=-104
and SQLSTATE=42601 error.

My question is
1) If my UDF is simply truncating the tables what must it *RETURN*
type
2) Do i need to modify any thing in the above mentioned UDF.

Thanks Gilroy

Quote:
On Wednesday, June 16, 2010 12:27 AM Lennart Jonsson wrote:

On 2010-06-15 17:59, Gilroy Gonsalves wrote:

The main purpose of a function is to return something, but it may under
restricted circumstances have side effects. Not the other way around.


What you describe does not really belong in a sql function, but you can
squeeze it in. First, MODIFIES SQL DATA requires that the function
returns a table not a scalar (and since you modifies sql data, you have
to specify that). Something like:

create function myfun (
p_table_name VARCHAR(100),
p_suffix VARCHAR(100)
)
returns table( x int )
LANGUAGE SQL
MODIFIES SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
BEGIN ATOMIC

if p_table_name = 'TB01' then
delete from tb01 where col1 = p_suffix;
elseif p_table_name = 'TB02' then
delete from tb02 where col1 = int(p_suffix);
end if;

return values (1);
END
@

You call a table function like:

select * from table(myfun('TB01', '3'));


I do not think it is a good idea to use functions this way, but if it is
absolutely necessary you may succeed with something like the above.


/Lennart

Quote:
On Wednesday, June 16, 2010 6:27 AM Gilroy Gonsalves wrote:

wrote:
-
-
4



Hi Lennart,

Much appreciated for your help. I followed the method of using SQL
Procedure. But since we are migrating our environment from Oracle to
DB2, some of the data stage Jobs uses Oracle function which requires
the table to be truncated as SELECT query. As per current project

Quote:
On Wednesday, June 16, 2010 2:34 PM Lennart Jonsson wrote:

On 2010-06-16 12:27, Gilroy Gonsalves wrote:

Quote:
On Thursday, June 17, 2010 4:14 AM Gilroy Gonsalves wrote:

wrote:
----
----
ix)
ix)
-104
r
n
e
s

Quote:
Submitted via EggHeadCafe
Merging SharePoint List Data into Word Documents
http://www.eggheadcafe.com/tutorials...documents.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.