If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > same value returned by udf for each row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-17-07, 05:24
baloo99 baloo99 is offline
Registered User
 
Join Date: Feb 2007
Location: Bratislava, Slovakia
Posts: 85
same value returned by udf for each row

Hi all,
i have next problem: my UDF function returns same vale on each new row.
what is wrong? i need different value for each row.
can you help me pls?

see code :
create table t11 (col1 bigint );
insert into t11 values (1) ;
insert into t11 values (2) ;
insert into t11 values (3) ;
commit;
create function rand_x (i_min int, i_max int )
returns float
language sql
not deterministic
begin atomic
return value(i_min, 0) +(value(i_max, 1)- value(i_min, 0))*rand ();
end ;

select rand_x(1, 100) as bad_col , rand() as ok_col from t11;

BAD_COL OK_COL
61.586779381695 0.0278328806421094
61.586779381695 0.329844050416578
61.586779381695 0.0559099093600269

thans much advance
__________________
Beer contains just a small amount of vitamines - that's why it's necessary to drink lot of it.
Reply With Quote
  #2 (permalink)  
Old 12-17-07, 06:48
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
looks strange.

Output from the function should be FLOAT, but BAD_COL seems to be DECIMAL .

I created the function on our testsystem ( DB2 UDB for z/OS V8 NFM )
omitted the textstring "begin atomic" and the function worked fine.


CREATE FUNCTION RAND_X (I_MIN INT, I_MAX INT )
RETURNS FLOAT
LANGUAGE SQL
NOT DETERMINISTIC

RETURN VALUE(I_MIN, 0) +(VALUE(I_MAX, 1)- VALUE(I_MIN, 0))*RAND ();




SELECT RAND_X ( 1,100), RAND()
FROM SYSIBM.SYSTABLES
FETCH FIRST 10 ROWS ONLY ;

gave as a result:

COL1 COL2
---------- ----------
5.417E+01 7.872E-02
2.944E+01 7.680E-01
6.084E+01 4.268E-02
4.956E+01 8.509E-01
4.169E+01 7.381E-01
6.663E+01 5.565E-01
9.740E+00 4.582E-01
7.174E+01 6.858E-01
6.837E+01 6.786E-02
6.867E+01 2.851E-01
Reply With Quote
  #3 (permalink)  
Old 12-17-07, 07:55
baloo99 baloo99 is offline
Registered User
 
Join Date: Feb 2007
Location: Bratislava, Slovakia
Posts: 85
umayer: works, thanks very much
__________________
Beer contains just a small amount of vitamines - that's why it's necessary to drink lot of it.
Reply With Quote
  #4 (permalink)  
Old 02-25-08, 12:51
baloo99 baloo99 is offline
Registered User
 
Join Date: Feb 2007
Location: Bratislava, Slovakia
Posts: 85
next level. when UDF must contains more than one command, is necessary begin atomic/ end statement. this causes repeating value on UDF result. some ideas ?
RTFM? bug? feature?
db version is 8.2.3. LUW
__________________
Beer contains just a small amount of vitamines - that's why it's necessary to drink lot of it.
Reply With Quote
  #5 (permalink)  
Old 02-26-08, 10:34
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Initially, this looks like a defect in DB2. You may want to try it with the latest FixPak to see if this is already fixed. If not, open a PMR with IBM support.

I recommend that you try to change the original function to EXTERNAL ACTION (default is NO EXTERNAL ACTION). With this, DB2 must make sure that the function body is called as often as is mandated by the SQL statement you typed in (and not what the optimizer may make of it).

Furthermore, your claim that one needs compound statements can usually not be held up. I found so far that it is generally possibly to write procedural SQL statements into relational SQL. (Alas, thinking in sets is not so well practiced amongst the many people who work with relational DBMS.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 03-10-08, 10:04
baloo99 baloo99 is offline
Registered User
 
Join Date: Feb 2007
Location: Bratislava, Slovakia
Posts: 85
thanks

stolze:
thanks
sorry for delay.
__________________
Beer contains just a small amount of vitamines - that's why it's necessary to drink lot of it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On