RE: SQL to identify bad data in numeric columns
"Birgitta Hauser" <Hauser-bRAHsVz2yv4TVIZRl6KS/[email protected]>
| Newsgroups | gmane.comp.hardware.ibm.midrange |
|---|---|
| Message-ID | <[email protected]> |
The easiest way to get the source code is to use ACS - Schemas. Position on the database object ... right click GENERATE_SQL --> DDL and you'll get the source code. Another option to get the SQL Source code from a database object is to use the GENERATE_SQL Stored Procedure Here is the SQL Code retrieved with Reverse Engineering: CREATE OR REPLACE FUNCTION SYSTOOLS/VALIDATE_DATA_FILE ( LIBRARY_NAME VARCHAR(10) FOR SBCS DATA , FILE_NAME VARCHAR(10) FOR SBCS DATA ) RETURNS TABLE ( VALIDATE_TIME TIMESTAMP , LIBRARY_NAME VARCHAR(10) FOR SBCS DATA , FILE_NAME VARCHAR(10) FOR SBCS DATA , MEMBER_NAME VARCHAR(10) FOR SBCS DATA , RELATIVE_RECORD_NUMBER BIGINT , SQL_WARNING INTEGER , REASON_CODE INTEGER , COLUMN_NAME VARCHAR(128) FOR SBCS DATA , WARNING_TEXT VARCHAR(1000) FOR SBCS DATA ) LANGUAGE SQL SPECIFIC SYSTOOLS/VALID_FIL NOT DETERMINISTIC MODIFIES SQL DATA CALLED ON NULL INPUT NO EXTERNAL ACTION NOT FENCED SYSTEM_TIME SENSITIVE NO SET OPTION ALWBLK = *ALLREAD , ALWCPYDTA = *OPTIMIZE , COMMIT = *NONE , DECRESULT = (31, 31, 00) , DFTRDBCOL = QSYS2 , DLYPRP = *NO , DYNDFTCOL = *NO , DYNUSRPRF = *USER , SRTSEQ = *HEX BEGIN -- -- description: validate_data_file will call validate_data() for all members within a database file -- -- Input parameters: -- library_name - the library name containing the database file -- file_name - the database file name -- -- There are no expected failures, so leave if a failure occurs, with an info message sent to the joblog DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN DECLARE LOCAL_SQLCODE INTEGER ; DECLARE LOCAL_SQLSTATE CHAR ( 5 ) ; DECLARE V_MESSAGE_TEXT VARCHAR ( 2000 ) FOR SBCS DATA ; GET DIAGNOSTICS CONDITION 1 LOCAL_SQLCODE = DB2_RETURNED_SQLCODE , LOCAL_SQLSTATE = RETURNED_SQLSTATE , V_MESSAGE_TEXT = MESSAGE_TEXT ; CALL SYSTOOLS / LPRINTF ( 'VALIDATE_DATA_FILE FAILED WITH SQLCODE=' CONCAT LOCAL_SQLCODE CONCAT ' SQLSTATE=' CONCAT LOCAL_SQLSTATE CONCAT ' MESSAGE= ' CONCAT V_MESSAGE_TEXT ) ; END ; RETURN WITH TBL ( MBR ) AS ( SELECT TABLE_PARTITION FROM QSYS2 / SYSPARTITIONSTAT A WHERE SYSTEM_TABLE_SCHEMA = LIBRARY_NAME AND SYSTEM_TABLE_NAME = FILE_NAME AND TABLE_PARTITION IS NOT NULL ) SELECT A . * FROM TBL , LATERAL ( SELECT * FROM TABLE ( SYSTOOLS . VALIDATE_DATA ( LIBRARY_NAME => LIBRARY_NAME , FILE_NAME => FILE_NAME , MEMBER_NAME => TBL . MBR ) ) ) A ; END ; Mit freundlichen Grüßen / Best regards Birgitta Hauser Modernization Education Consulting on IBM i Database and Software Architect IBM Champion since 2020 "Shoot for the moon, even if you miss, you'll land among the stars." (Les Brown) "If you think education is expensive, try ignorance." (Derek Bok) "What is worse than training your staff and losing them? Not training them and keeping them!" "Train people well enough so they can leave, treat them well enough so they don't want to. " (Richard Branson) "Learning is experience everything else is only information!" (Albert Einstein) -----Original Message----- From: MIDRANGE-L <midrange-l-bounces-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org> On Behalf Of Vern Hamberg via MIDRANGE-L Sent: Friday, 8 May 2026 15:09 To: [email protected] Cc: Vern Hamberg <[email protected]> Subject: Re: SQL to identify bad data in numeric columns Hi Birgitta You mention reverse-engineering to get the SQL source code. Is there something more to do than merely to open the routine body in the Schemas tool? I took a brief look at it this morning and have not identified everything the VALIDATE_FILE table function does. *Regards* *Vern Hamberg* IBM Champion 2025 <cid:[email protected]> CAAC (COMMON Americas Advisory Council) IBM Influencer 2023 On 5/8/2026 12:12 AM, Birgitta Hauser wrote: > Yep! > You have to run the services multiple times until you trap all invalid > numeric values! > ... but the service is in the SYSTOOLS schema, that means it is not in the > normal support, but with reverse engineering you can get the SQL source code > ... so you can modify it and create your own function. > > Another alternative is using TRY_CAST on every numeric column and if it > returns NULL replace it with a valid numeric value (normally 0). > Which, in composition with dynamic SQL is what I have used for my function > for revising invalid numeric values ... and even in before > insert/update/delete triggers that check for invalid numeric values, and > revise them, so SQL tables will not crash, when writing invald data. > > Mit freundlichen Grüßen / Best regards > > Birgitta Hauser > Modernization Education Consulting on IBM i > Database and Software Architect > IBM Champion since 2020 > > "Shoot for the moon, even if you miss, you'll land among the stars." (Les > Brown) > "If you think education is expensive, try ignorance." (Derek Bok) > "What is worse than training your staff and losing them? Not training them > and keeping them!" > "Train people well enough so they can leave, treat them well enough so they > don't want to. " (Richard Branson) > "Learning is experience everything else is only information!" (Albert > Einstein) > > > -----Original Message----- > From: MIDRANGE-L<midrange-l-bounces-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org> On Behalf Of Dan > Bale via MIDRANGE-L > Sent: Thursday, 7 May 2026 23:08 > To: Midrange Systems Technical Discussion<[email protected]> > Cc: Dan Bale<dan.bale-EkiVQpMPPJH8R359/[email protected]> > Subject: SQL to identify bad data in numeric columns > > I found the systools.validate_data_file table function. It "works", but it > returns only one result per row that may have more than one numeric column > with invalid data. I would like the results to show all invalid columns for > a given row, not just one. > > I extracted the source for this, but I'm wondering whether anyone else has > already invented this wheel and is willing to share. > > - Dan Bale > *** CONFIDENTIALITY NOTICE: The information contained in this communication > may be confidential, and is intended only for the use of the recipients > named above. If the reader of this message is not the intended recipient, > you are hereby notified that any dissemination, distribution, or copying of > this communication, or any of its contents, is strictly prohibited. If you > have received this communication in error, please return it to the sender > immediately and delete the original message and any copy of it from your > computer system. If you have any questions concerning this message, please > contact the sender. *** > -- > This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list > To post a message email:[email protected] To subscribe, > unsubscribe, or change list options, > visit:https://lists.midrange.com/mailman/listinfo/midrange-l > or email:MIDRANGE-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org > Before posting, please take a moment to review the archives at > https://archive.midrange.com/midrange-l. > > Please contactsupport-FMtJrHiV//lnDLsaKlm4mFaTQe2KTcn/@public.gmane.org for any subscription related > questions. > -- This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: [email protected] To subscribe, unsubscribe, or change list options, visit: https://lists.midrange.com/mailman/listinfo/midrange-l or email: MIDRANGE-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org Before posting, please take a moment to review the archives at https://archive.midrange.com/midrange-l. Please contact support-FMtJrHiV//lnDLsaKlm4mFaTQe2KTcn/@public.gmane.org for any subscription related questions. -- This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: [email protected] To subscribe, unsubscribe, or change list options, visit: https://lists.midrange.com/mailman/listinfo/midrange-l or email: MIDRANGE-L-request-+hD5IHI5Xscn3HwCXmMcX9BPR1lH4CV8@public.gmane.org Before posting, please take a moment to review the archives at https://archive.midrange.com/midrange-l. Please contact support-FMtJrHiV//lnDLsaKlm4mFaTQe2KTcn/@public.gmane.org for any subscription related questions.