DB2 --- create a function that returns a result set

In the data verification, often encountered need to return a result set operation, so finishing a DB2 function returns the result set, easy to post Now

1, to prepare test table

/ * Create test table: Set Attribute Field for the result set * / 
the CREATE  TABLE   Test_EXWASTE_EXC 
    ( 
        Test_Time TIMESTAMP , -
         Test_STATION the CHARACTER ( . 6 ), -
         Test_Three the CHARACTER ( 2 ), -
         Test_Four VARCHAR ( . 3 ), -
         Test_Five VARCHAR ( . 1 ), -
         Test_MODIFY VARCHAR ( . 1 ) -
 )

2, create a function that returns a result set

/ * Function returns: refpk to ',' is inserted to the test partition table information, and returns the result set * / 
- / 
Create  the FUNCTION Test_Func_GetInfo (REFPK VARCHAR ( 500 )) - REFPK parameter passed into 
Returns                                                - the return value of type Table 
TABLE (Out_Time TIMESTAMP , 
 Out_Station Character ( . 6 ), 
 Out_Three Character ( 2 ), 
 Out_Four SMALLINT , 
 Out_Five smallint the , 
 Out_Modify Character ( . 1 )) 
the LANGUAGE the SQL
MODIFIES SQL DATA                    --MODIFIES SQL DATA 表示函数中有ddl操作
begin atomic
DECLARE Out_Time TIMESTAMP;
DECLARE Out_Station character(6);
DECLARE Out_Three character(2);
DECLARE Out_Four SMALLINT;
DECLARE Out_Five smallint;
DECLARE Out_Modify character(1);
DECLARE I_INDEX INT;--
DECLARE I_Length INT;
DECLARE I_Locate INT;
DECLARE V_Char character(1);
DECLARE I_Switch INT;
DECLARE I_Return INT;
DECLARE V_Refpk varchar(500);

set I_Return=0;
set I_INDEX=1;
set I_Length=0;
set I_Locate=1;
set I_Switch=0;
set V_Refpk=refpk||',';
set I_Length=length(V_Refpk);

set I_Return=1;
while I_INDEX<=I_Length do              --将','分隔的信息取出来
    set V_Char=substr(V_Refpk,I_INDEX,1);
    if V_Char=',' then
    set I_Switch=I_Switch+1;
           if I_Switch=1 then set Out_Time=timestamp(substr(V_Refpk,I_Locate,I_INDEX-I_Locate));
         elseif I_Switch=2 then set Out_Station=substr(V_Refpk,I_Locate,I_INDEX-I_Locate);
         elseif I_Switch=3 then set Out_Three=substr(V_Refpk,I_Locate,I_INDEX-I_Locate);
         elseif I_Switch=4 then set Out_Four=substr(V_Refpk,I_Locate,I_INDEX-I_Locate);
         elseif I_Switch=5 then set Out_Five=substr(V_Refpk,I_Locate,I_INDEX-I_Locate);
         else set Out_Modify=substr(V_Refpk,I_Locate,I_INDEX-I_Locate);
           end if;
    set I_Locate=I_INDEX+1;
    end if;
    set I_INDEX=I_INDEX+1;
end while;
insert into Test_EXWASTE_EXC values(Out_Time,Out_Station,Out_Three,Out_Four,Out_Five,Out_Modify);
return select * from Test_EXWASTE_EXC ;
end
/

3, calling function

Since the return value of the function table, so we operate the same function as the operation table

SELECT T.OUT_TIME from  Table (Test_Func_GetInfo ( ' 2019-06-10 07: 30: 40,020801,28,154,0,1 ' )) T WHERE T.OUT_TIME = timestamp ' 2019-06-10 07:30:40 ' 
- Note that the function T field is defined in the field, regardless of Test_Exwaste_exc


4, delete the keywords MODIFIES SQL DATA, reported error -374

 

Guess you like

Origin www.cnblogs.com/handhead/p/11314324.html