Database Forum / DB2 Topics / May 2006
Help! inserting records and functions
|
|
Thread rating:  |
Jaraba - 28 Apr 2006 17:07 GMT I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am particulary having problems inserting records using a function.
I am attaching the code for your review. Please, help.
=================================--Function 1--====================================
CREATE FUNCTION lx2p111.DelimiterIndex ( string varchar(8000), Delimiter char(1) ) RETURNS TABLE ( ordinal INTEGER, index INTEGER )
-- string = any string delimited by a character -- Delimiter = the delimimeter character i.e. ',', -, * -- Description This functions returns the begining and end position for every array. -- supports RangeElements function
LANGUAGE SQL DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN WITH t(ordinal, index) AS ( VALUES ( 0, 0 ) UNION ALL SELECT ordinal+1, COALESCE(NULLIF( -- find the next delimiter ',' LOCATE(',', string, index+1), 0), LENGTH(string)+1) FROM t WHERE ordinal < 10000 AND LOCATE(Delimiter, string, index+1) <> 0 )
SELECT ordinal, index FROM t UNION ALL -- add indicator for the end of the string SELECT MAX(ordinal)+1, LENGTH(string)+1 FROM t
=================================--Function 2--====================================
CREATE FUNCTION LX2P111.RangeElements ( string varchar (8000), delimiter char (1) ) RETURNS TABLE ( elements varchar(255) )
------------------------------------------------------------------------
-- string = any string delimited by a character -- Delimiter = the delimimeter character i.e. ',', -, * -- Description This functions returns every array element in the list (i.e. ranges)
LANGUAGE SQL DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL RETURN WITH t(ordinal, index) AS ( SELECT ordinal, index FROM TABLE ( LX2P111.DelimiterIndex(string, Delimiter) ) AS x ) SELECT SUBSTR(string, t1.index+1, t2.index - t1.index - 1) FROM t AS t1 JOIN t AS t2 ON ( t2.ordinal = t1.ordinal+1 )
=================================--Stored Proc--====================================
CREATE PROCEDURE LX2P111.LDBS_REQUISITION_DETAIL_TEST ( IN ac_req_number_range
VARCHAR(8000), IN adt_begin DATE ) DYNAMIC RESULT SETS 1 LANGUAGE SQL
-- ac_req_number_range = Requisition ranges (parse string, for dsicrete values
min=max) -- adt_begin = The beginning date range to present (single
value) -- Description This procedure will parse the ai_req_number_range and insert -- all rows into temp table. It first checks to see if there
P1: BEGIN --Create temp table to hold for the parsed array element values
DECLARE GLOBAL TEMPORARY TABLE SESSION.TempList ( RecordId varchar(255) ) ;
-- Insert record into tempList using function
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, ',')) ;
--Insert elelements of array into separate fields
DECLARE GLOBAL TEMPORARY TABLE SESSION.TempList2 ( ParamName Varchar(25), MinValue varchar(15), Maxvalue varchar(15) ) ;
insert into session.templist2 select 'ReqNo' , left(RecordId, (locate('-', RecordID, 1)-1)) , substr (RecordId, (locate('-', RecordId, 1)+1),(length
(RecordId)- (locate('-', RecordId, 1)))) from SESSION.TEMPLIST;
--Declare cursor BEGIN DECLARE MyCursor CURSOR WITH RETURN FOR select RQH_REQ_LOCATION, RQH_REQ_NUMBER, RQH_CREATED, RQH_REQUESTER, RQH_DELIVER_TO from lx2p111.requisition_detail rd, session.templist2 t2 where rd.RQH_REQ_NUMBER between cast(t2.MinValue as int)
and cast(t2.MaxValue as Int) and rd.RLN_CREATED >= coalesce(adt_begin, '01/01/1900') ;
-- Cursor left open for client application OPEN MyCursor; END; END P1
Dave Hughes - 29 Apr 2006 11:39 GMT > I am working in a project that I need to parse an arrayt an select > records based upon the values parsed. I used the functions developed > by Knut Stolze in his article 'Parsing Strings'. I am particulary > having problems inserting records using a function. > > I am attaching the code for your review. Please, help. What error message are getting? For that matter, are you getting an error message or are these just not working as you expected?
> =================================--Function > 1--==================================== [quoted text clipped - 20 lines] > -- find the next delimiter ',' > LOCATE(',', string, index+1), 0), Well, looks like these are basically the same as the functions Knut wrote about in his article, although you've added a parameter to make the delimiter character configurable. Unfortunately, in the line above, you've still got a constant ',' that needs to be replaced with DELIMITER.
> LENGTH(string)+1) > FROM t [quoted text clipped - 7 lines] > SELECT MAX(ordinal)+1, LENGTH(string)+1 > FROM t [snip]
Dave.
Jaraba - 29 Apr 2006 13:19 GMT I am getting an error in the following piece of code, everything else works well specially the ones yjay Knut wrote.
Thanks
Jaraba - 29 Apr 2006 13:20 GMT -- Insert record into tempList using function
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, ',')) ;
Sorry, forgot to post the code in the previous entry.
Dave Hughes - 29 Apr 2006 14:13 GMT > -- Insert record into tempList using function > [quoted text clipped - 4 lines] > > Sorry, forgot to post the code in the previous entry. ... and the error?
Dave.
Jaraba - 29 Apr 2006 14:49 GMT I am getting an error building the stored procedure. It is expecting different parameters. maybe the construction of the insert statement with the function is not properly done
Knut Stolze - 29 Apr 2006 22:03 GMT > I am getting an error building the stored procedure. It is expecting > different parameters. maybe the construction of the insert statement > with the function is not properly done You should run the INSERT statement alone on the command line to test its proper functioning. Also, please post the exact and complete error message here. This helps tremendously to understand your problem.
My best guess is that you have not provided a correlation name for the resulting table. This might work better:
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, ',')) AS t
Note the "AS t" at the end of the FROM clause. This correlation name (called correlation-clause in the manual http://tinyurl.com/7av8b) is mandatory.
 Signature Knut Stolze DB2 Information Integration Development IBM Germany
Dave Hughes - 29 Apr 2006 22:07 GMT > I am getting an error building the stored procedure. It is expecting > different parameters. maybe the construction of the insert statement > with the function is not properly done Could you please post the *error* *message* you are getting? Honestly... Like trying to get blood out of a stone.
In the absence of any further information, I'll take a wild stab in the dark (insert Blackadder II quote here ;-)
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, ','));
The above code will fail, but not with an error about parameters. It'll fail because you're missing a mandatory correlation name from the end:
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, ',')) AS T;
However, that'll fail too because the string literal ',' will be interpreted as a VARCHAR when your function is expecting a CHAR in that position. Either change the function to expect a VARCHAR as the second parameter, or use:
insert into session.templist select * from table (LX2P111.RangeElements (ac_req_number_range, CHAR(','))) AS T;
Not that the second parameter is much use... As mentioned in a prior post you've got a bug in your altered version of Knut's functions. Change:
LOCATE(',', string, index+1), 0),
To:
LOCATE(DELIMITER, string, index+1), 0),
Finally, you may run into some problems by having declared the certain parameters as VARCHAR(8000). I seem to recall a note in the LOCATE function's documentation that the maximum length it can handle is VARCHAR(4000) (when passed a VARCHAR that is), like a number of other functions in the SYSFUN schema.
Finally, and without putting too blunt a point on it (re: Blackadder II quote earlier ;-), here's a checklist for future use:
* Post the code that's causing the error. Try and reduce the code to a minimal use case that demonstrates the problem without requiring extra objects like tables to be defined. If extra objects are necessary, post the code defining them. * Format the code in your post so that it doesn't wrap across lines (especially in the case of comments) allowing people to simply copy and paste your code if they want to test it. * Post the *entire* error message including any apparently cryptic codes. Even if the error message isn't English, the code at the start of a DB2 error can be used to find the English definition in the Info Center. * Include the version, fixpak level, and platform of DB2 that you're using (e.g. DB2 UDB v8.1 FP11 for LUW), or better still include the output of db2level on your system.
Dave.
Jaraba - 30 Apr 2006 15:13 GMT DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884, SQLERRMC: RANGEELEMENTS;FUNCTION Message: No authorized routine named "RANGEELEMENTS" of type "FUNCTION" having compatible arguments was found
Is the message that I am getting.
Jaraba - 30 Apr 2006 19:04 GMT DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884, SQLERRMC: RANGEELEMENTS;FUNCTION Message: No authorized routine named "RANGEELEMENTS" of type "FUNCTION" having compatible arguments was found.
I keep getting this error message.
Serge Rielau - 30 Apr 2006 20:13 GMT > DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884, SQLERRMC: > RANGEELEMENTS;FUNCTION > Message: No authorized routine named "RANGEELEMENTS" of type > "FUNCTION" having compatible arguments was found. > > I keep getting this error message. Could it be the second argument is a CHAR(1)? LX2P111.RangeElements (ac_req_number_range, ',') ',' is a VARCHAR(1) and cannot be promoted to CHAR(1). Try LX2P111.RangeElements (ac_req_number_range, CHAR(','))
 Signature Serge Rielau DB2 Solutions Development IBM Toronto Lab
Jaraba - 01 May 2006 01:54 GMT Thanks. I think that was most of the proble. I adjusted data types and change few things. Now I am getting a privilege error message as follows:
DB2 SQL error: SQLCODE: -551, SQLSTATE: 42501, SQLERRMC: CD2KXG;SELECT;LX2P111.REQUISITION_DETAIL "CD2KXG" does not have the privilege to perform operation "SELECT" on object "LX2P111.REQUISITION_DETAIL".
Even thoug i can select records using the id in the command window
Jaraba - 01 May 2006 02:37 GMT Everything basically works, but when I join the temp table to the REQUISITION_DETAIL view, that is when I get the above error
Serge Rielau - 01 May 2006 05:05 GMT > Everything basically works, but when I join the temp table to the > REQUISITION_DETAIL view, that is when I get the above error Now we are lacking the query :-) If you post all the information together chances increase.
Cheers Serge
 Signature Serge Rielau DB2 Solutions Development IBM Toronto Lab
Jaraba - 01 May 2006 17:54 GMT The query is included in the nody of the stored proc:
BEGIN DECLARE MyCursor CURSOR WITH RETURN FOR select RQH_REQ_LOCATION, RQH_REQ_NUMBER, RQH_CREATED, RQH_REQUESTER, RQH_DELIVER_TO from lx2p111.requisition_detail rd, session.templist2 t2 where rd.RQH_REQ_NUMBER between cast(t2.MinValue as int)
Knut Stolze - 02 May 2006 10:47 GMT > The query is included in the nody of the stored proc: > [quoted text clipped - 9 lines] > where rd.RQH_REQ_NUMBER between > cast(t2.MinValue as int) Well, do what the user response of the SQL0551 error message tells you: grant the necessary privileges on the lx2p111.requisition_detail table to the user who wants to create the procedure. There are some cases where routines don't accept group privileges. I don't remember the details, but this might be such a case.
 Signature Knut Stolze DB2 Information Integration Development IBM Germany
|
|
|