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 > Stored Procedure for row delete date criteria based

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-18-09, 15:54
sujith_g sujith_g is offline
Registered User
 
Join Date: Mar 2009
Posts: 16
Stored Procedure for row delete date criteria based

Hello-
I am having a hard time getting a stored procedure to work based on dates. Many time we have need to get data cleanced out based on date ranges and this would really help. Any help appreciated.

Env: Windows DB2 V9.5

Here is the script i was having issues with

CREATE PROCEDURE DELCRT_ROWS
(IN tabschema VARCHAR(128), IN tabname VARCHAR(128), IN predicate VARCHAR(1000), IN date1 DATE, IN date2 DATE, IN commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt = 'DELETE FROM (SELECT 1 FROM "' || tabschema || '"."' || tabname || '" WHERE '
|| predicate || ' BETWEEN ' || date1 || ' AND ' || date2 || ' FETCH FIRST ' || RTRIM(CHAR(commitcount)) || ' ROWS ONLY) AS D';
PREPARE stmt FROM txt;
l: LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE l; END IF;
COMMIT;
END LOOP;
END
@

The error I get is below
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0440N No authorized routine named "||" of type "FUNCTION" having
compatible arguments was found. LINE NUMBER=7. SQLSTATE=42884

SQL0440N No authorized routine named "||" of type "FUNCTION " having compatible arguments was found.
Reply With Quote
  #2 (permalink)  
Old 03-18-09, 16:04
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Different idea: how about using range-partitioned tables and detaching whole partitions? Then you could drop the detached partition.

To answer your question: you try to concatenate a string with a date. That won't work and you should cast the date to a string - as you already do with the commitcount.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 03-18-09, 16:08
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Dates are not strings; you cannot concatenate dates. In addition to this, you are missing some quotation marks.
Reply With Quote
  #4 (permalink)  
Old 03-18-09, 16:37
sujith_g sujith_g is offline
Registered User
 
Join Date: Mar 2009
Posts: 16
Thanks!!!

I am still getting an error after dates are cast to character. Are the dates parsed differently when converted to text; is there a way to see this?

Modified script:

CREATE PROCEDURE DELCRT_ROWS
(IN tabschema VARCHAR(128), IN tabname VARCHAR(128), IN predicate VARCHAR(1000), IN date1 DATE, IN date2 DATE, IN commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt = 'DELETE FROM (SELECT 1 FROM "' || tabschema || '"."' || tabname || '" WHERE '
|| predicate || ' BETWEEN ' || RTRIM(CHAR(date1)) || ' AND ' || RTRIM(CHAR(date2)) || ' FETCH FIRST ' || RTRIM(CHAR(commitcount)) || ' ROWS ONLY) AS D';
PREPARE stmt FROM txt;
l: LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE l; END IF;
COMMIT;
END LOOP;
END
@

Error message:

CALL U261251.DELCRT_ROWS('U27332','AUD_TRL_CNTL_TOT','P ROD_DT','2006-10-01','2006-10-31', 10)
SQL0401N The data types of the operands for the operation "BETWEEN" are not
compatible. SQLSTATE=42818

Reply With Quote
  #5 (permalink)  
Old 03-18-09, 17:05
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by sujith_g

I am still getting an error after dates are cast to character.
Please allow me to quote myself:
Quote:
Originally Posted by n_i
In addition to this, you are missing some quotation marks.
Reply With Quote
  #6 (permalink)  
Old 03-18-09, 17:28
sujith_g sujith_g is offline
Registered User
 
Join Date: Mar 2009
Posts: 16
Put the quotes around the dates.. now getting a different message

Error

CALL U261251.DELCRT_ROWS('U27332','AUD_TRL_CNTL_TOT','P ROD_DT','2006-10-01','2006-10-31', 10)
SQL0180N The syntax of the string representation of a datetime value is
incorrect. SQLSTATE=22007

Script
CREATE PROCEDURE DELCRT_ROWS
(IN tabschema VARCHAR(128), IN tabname VARCHAR(128), IN predicate VARCHAR(1000), IN date1 DATE, IN date2 DATE, IN commitcount INTEGER)
BEGIN
DECLARE SQLCODE INTEGER;
DECLARE txt VARCHAR(10000);
DECLARE stmt STATEMENT;
SET txt = 'DELETE FROM (SELECT 1 FROM "' || tabschema || '"."' || tabname || '" WHERE '
|| predicate || ' BETWEEN ' || ''' || RTRIM(CHAR(date1) || ''' || ' AND ' || '''
|| RTRIM(CHAR(date2) || ''' || ' FETCH FIRST ' || RTRIM(CHAR(commitcount)) || ' ROWS ONLY) AS D';
PREPARE stmt FROM txt;
l: LOOP
EXECUTE stmt;
IF SQLCODE = 100 THEN LEAVE l; END IF;
COMMIT;
END LOOP;
END
@
Reply With Quote
  #7 (permalink)  
Old 03-18-09, 21:19
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You should debug your procedure by returning the statement being generated as OUT parameter. Then you can have a look at the statement and verify that you construct the right thing.

p.s: What I find strange is this:
Code:
predicate BETWEEN date1 AND date2
I guess that your "predicate" should rather be an "expression". Besides, doing what you do is a begging for SQL injections, which is its own class of security problems.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 03-18-09, 23:32
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
' BETWEEN ' || '''' || RTRIM(CHAR(date1)) || '''' || ' AND ' || '''' || RTRIM(CHAR(date2)) || ''''

Last edited by tonkuma; 03-18-09 at 23:42.
Reply With Quote
  #9 (permalink)  
Old 03-19-09, 17:47
sujith_g sujith_g is offline
Registered User
 
Join Date: Mar 2009
Posts: 16
Thumbs up

Great. It worked perfect. Thanks!!!
Reply With Quote
  #10 (permalink)  
Old 03-20-09, 03:18
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I recommend that you learn how to debug such things yourself in the future. You can either return the SQL statement via an out parameter (as I suggested before), or you write it to a log file (http://www.ibm.com/developerworks/da...03stolze.html).
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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