switching to sql file definitions from ddssqliquery.com/datastore/iquery_ddstosqlddl.pdf ·...
TRANSCRIPT
SwitchingtoSQLFileDefinitionsfromDDS
SQLiQueryforIBMi
©2017CozziProductions,Inc.
with
[email protected]:@bobcozzi
iQSQLiQuery.comTransforming Db2 into
Excel, JSON & more with SQL
DownloadthisPresentation
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
• WeborCommandLineSQLprocessor
• PowerfulScriptingTool"SiLQ"SupportsIBMi DB2SQL• RunanySQLcommand
• SendSQLSELECTresultstoyourchoiceofoutputformats• CSV,Sylk,JSON,Excel,XML,PDF,Print,Display,etc.
SQL iQueryPowerful Database Query and SQL Scripting for IBM i
© Cozzi Productions, Inc.www.SQLiQuery.com
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
Resultsareblah
TXT
{JSON}CSV
,
Your Data with SQL iQueryYour Data
SQL iQuerySQL Processing and Scripting for IBM i
©2016CozziProductions,Inc.
*WriteOneandDone!
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
ClassicIBMi DDSFileDefinition
RecordFormatNameColumn(Field)Names
ColumnHeadingsandText
PrimaryKeyFields
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
DDSMethodforCreatingorMaintainingFiles
• CRTPF– CreatePhysicalFile• CRTPFFILE(CUSTMAST)SRCFILE(COZTEST/QDDSSRC)SRCMBR(CUSTMAST)
• CHGPF– ChangePhysicalFile• Add/Removefields• Add/Removekeyfields• Changecolumntext• FileProperties
• Size,attributesetc.• Dataispreservedwherepossible
• CHGPFFILE(CUSTMAST)SRCFILE(COZTEST/QDDSSRC)SRCMBR(CUSTMAST)
ChangePhysicalFile(CHGPF)Command
SQLfori DDLTable(File)Definition
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
BasicDifferencesBetweenDDSvsDDL
• DDSisKeywordoriented• Onelineorstatementperfielddefinition• Allfieldattributesareaddedviadifferentkeywordstothesamestatement
• SQLDDLisStatement,Keyword,Clause,Valueoriented• Columndefinitionsareonestatement• Additionalcolumnattributeareaddedthroughadditionalstatements
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
JustifyingTransformingDDSintoSQLDDL
• IBMhas"frozen"DDS• Nolongerbeingupdatedit,exceptfixes
• SQL'sDataDefinitionLanguageDDListhenewdatabasedefinitionstandard
• Newfeaturesinthedatabase• OnlybeingimplementedinSQLDDL• NOTbeingaddedtolegacyDDS
• e.g.,auto-incrementfields• DefaultValues
• DisplayFileDDShoweverisstillbeingupdated• ButitislargelyunrelatedtoDatabaseDDSexceptforsyntax iQ
SQLiQuery.comTransforming Db2 into
Excel and JSON using SQL
TransformingDDSintoSQLDDL
• SQLusesDDL(DataDefinitionLanguage)todefinethings• MorelinesofcodetoaccomplishsamethingasDDS,butit'sworthit
• CREATETABLE• LABELON… (toaddtextandcolumnheadings)
• ThreemethodsexisttotransformDDStoDDL1. ManuallywritetheDDL2. UsetheQSQGNDDLAPIortheSQLiQueryRTVSQLSRC3. UsetheDb2fori SQLGENERATE_SQL()Procedure
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
GeneratingSQLDDLfromExistingFiles
• TheQSQGNDDLAPIisagoodchoice• TheSQLiQueryRTVSQLSRCcommandiseasier
• TheGENERATE_SQL()procedureispervasivenottoocomplex• Oneveryone'ssystem• Hasacomplexparameterlist,butitislearnable
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
SQLiQueryRTVSQLSRCCommand
SQLforIBMi ServicesProcedure
• RTVSQLSRCusesQSQGNDDLAPItoextractthefile'sdefinition
• Agoodalternative istheIBM-suppliedSQLProcedure• QSYS2.GENERATE_SQL()
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
CALL QSYS2.GENERATE_SQL( File-name ,Library, SQL-Object-type,Source-file, Source-library, Source member, Options...
)
Must be run via an
SQL Processor
RunningtheGENERATE_SQLProcedure
• MustberunfromanSQLprocessor• EmbeddedinRPG• RUNSQL… IBM'shijacked"RunanSQLStatement"Command• RUNSQLSTM… IBM'sRunSQL/PLScript• RUNiQRY… SQLiQueryRunSQLStatementsorSQLandSiLQScripts• STRSQL… InteractiveSQL(theoldGreenScreenSQLenvironment)• RUNSQLScriptswhichispartofAccessClientSolutions(ACS)
• PreviouslytheClientAccessproduct
• Youhavelotsofchoices
GENERATE_SQLExample
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
CALL QSYS2.GENERATE_SQL('CUSTMAST','COZTEST', 'TABLE','QSQLSRC', 'COZTEST', 'CUSTMAST', STATEMENT_FORMATTING_OPTION => '0'
)
FileName
LibrarySQLType
SourceFileSourceLibrary
SourceMember
TheSTATEMENT_FORMAT_OPTIONoptionisessentiallyarequired
parameter.Withoutit,thesourcefilecontainsextraEOLcharactersandruinsthesourcemember..
STATEMENT_FORMATTING_OPTION=>'0'
• FromtheIBMWebsite:
formatting-option TheformattingoptionusedinthegeneratedSQLstatements.Thevalidvaluesare:• 0- Noadditionalformattingcharactersareaddedtothe
generatedSQLstatements.• 1- Additionalend-of-linecharactersandtabcharactersare
addedtothegeneratedSQLstatements.
Ifformatting-option isnotspecified,1willbeused.
• Alwaysspecify'0'oryouwillnotbehappy.
GeneratingDDLfromExistingFile
• TheCUSTMASTFile(showearlier)Exists• NeedtomigratethefiletoDDLstatements
• CREATEorREPLACETABLE• LABELONTABLE• LABELONCOLUMN
• "IS"forColumnHeadings• LABLEONCOLUMN
• "TEXTIS"forColumnTextDescriptions• CREATEINDEXmayalsobegeneratedforkeyfields
• MaywanttousePRIMARYKEYinstead
• TogeneratetheSQLDDLcalltheGENERATE_SQL()Procedure
GENERATE_SQLExample
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
CALL QSYS2.GENERATE_SQL('CUSTMAST','COZTEST''TABLE','QSQLSRC','COZTEST','CUSTMAST',STATEMENT_FORMATTING_OPTION => '0',
REPLACE_OPTION => '1',
CREATE_OR_REPLACE_OPTION=>'1')
ReplaceSourceMemberGenerate
"CreateorReplace"statement
GeneratedSQLfromRTVSQLSRCorGENERATE_SQL
FileName
RecordFormatName
Field("column")Names
CREATEorREPLACETABLE• Iftabledoesn'texist
• TableisCreated• Iftableexists
• Tabledefinitionisaltered
• Dataispreserved
ComparingDDStoSQLDDL
?CREATEINDEXCOZTEST.CUSTMAST_QSQGNDDL_00001ONCOZTEST.CUSTMAST(CUSTNOASC);
KeyFieldsinSQLDDL– ManualStep
PRIMARY KEY (CUSTNO)
?
KeyFields"PrimaryKey"
RemainderofDDLTableDefinitionSource
FileText
ColumnHeadings
ColumnTextDescriptions
UsingRUNiQRYtoCreateorReplaceTABLE
• TheCUSTMASTTableiscreatedinCOZTEST• ThefilewasnotjournaledsoSQLSTATE'01567'isreturned
• Thiscanbeignored
• Note:RUNiQRYdefaultstoCOMMIT(*NONE)
RUNiQRY SRCMBR(CUSTMAST) SRCFILE(COZTEST/QSQLSRC)
Table CUSTMAST in COZTEST created but was not journaled. iQ: Returned SQLState: 01567 SQLCode: 7905 from execSQL RC(1)
Type command, press Enter.===> RUNiQRY SRCMBR(CUSTMAST) SRCFILE(COZTEST/QSQLSRC)_
UsingRUNSQLSTMtoCreateorReplaceTABLE
• TheCUSTMASTTableiscreatedinCOZTEST• Thefilewasnotjournaled
• Thiscanbeignored
RUNSQLSTM SRCMBR(CUSTMAST) SRCFILE(COZTEST/QSQLSRC) COMMIT(*NONE)
Table CUSTMAST in COZTEST created but was not journaled.
Type command, press Enter.===> RUNSQLSTM SRCMBR(CUSTMAST) SRCFILE(COZTEST/QSQLSRC)_COMMIT(*NONE)
SQLiQueryScriptvsRUNSQLSTM
SQLiQuerySiLQ (ScriptingLanguage)• ProvidesdynamiccreationofSQLstatementsat
runtime• SessionVariables
• CanbeusedasubstitutionvaluesintheSQLstatements• CanbeusedastargetofSELECTINTO
• ConditionalLogic• Conditioningwhichstatementsarerun• Conditioningwhatpartsofa(forexample)SELECT
statementareincluded
• Looping• FOREACHLoopuntilEndofFile(usesaSELECT
statement)• FOR(orDO)SimilartoRPG's"FOR"opcode.FOR&I=1
to10by1;• WHILELoop:ClassicDOWHILE
• Built-inopcodesandsymbolicnamesforhardtoreachinformation
• SystemValues,SystemName,etc.
SQL/PL(ProcedureLanguage)• CreateUDF,UDTFandProcedures
• "Compiles"SQLFunctions
• RuncompletedSQLstatements• CanuseACSRUNSQLScriptforsomescripts,butRUNSQLSTMstillthe"goto"commandforSQLFunction/Procedure/TableCreation.
Bottomline,both RUNiQRYandRUNSQSTMareneededforacomplete
SQLDevelopmentEnvironment
ForMoreInformationvisit
www.SQLiQuery.com
Questions?Email:[email protected]
iQSQLiQuery.com
Transforming Db2 intoExcel and JSON using SQL
Twitter:@bobcozzi