SQLIFEXISTSinRPGIVSQL iQuery Scripting
iQSQLiQuery.comTransforming Db2 into
Excel, JSON & more with SQL
with
[email protected]:@bobcozzi
©2017CozziProductions,Inc.
Programming with SQL iQueryModern SQL Scripting
SQLiQuery.com
iQResultsareblah
TXT
{JSON}CSV
,
Your Data with SQL iQueryYour Data
*WriteoneSQLstatement,outputtoanysupportedformat.©2016CozziProductions,Inc.
*WriteOneandDone!
WhatisSQL"IFEXISTS"
• Testsifthesubselect producesanyrows• Ifrowsarereturned,theIFtestistrue• Ifnorowsarereturned,theIFtestisfalse• SQLUDFsand"stored"ProceduressupportIFExiststests• SQLiQuerySupportsIFExiststests• WhataboutRPG?
RPGIVIFExists
5770ST1 V7R3M0 160422 Create SQL ILE RPG Object IFEXISTSDIAGNOSTIC MESSAGES
4 EXEC SQL IF EXISTS (SELECT * FROM DATALIB.ORDHIST 5770ST1 V7R3M0 160422 Create SQL ILE RPG Object IFEXISTS
DIAGNOSTIC MESSAGESMSG ID SEV RECORD TEXT SQL0199 30 4 Position 22 Keyword IF not expected. Valid tokens: ( CL
END GET SET CALL DROP FREE HOLD LOCK OPEN WITH ALTER BEGIN.
HowtoCode"IFEXISTS"inRPGIVwithSQL
• IFEXISTSdoesnotexistinRPG• HoweverRPGdoeshaveIFstatementitself
• RPGsupportstheSQLWHEREEXISTSclause• WecancombinedRPGwithSQLtosimulatethe"IFEXISTS"condition
IFEXISTSinRPG
1.Addan"IfExists"flag(indicatorvariable)
2.SELECT'1'INTOthatvariablefromSYSDUMMY1
3.NesttheEXISTSclausewithintheWHEREclause
4.TesttheExistsvariablefortrue(*ON)condition
/free
/end-free