switching to sql file definitions from ddssqliquery.com/datastore/iquery_ddstosqlddl.pdf ·...

27
Switching to SQL File Definitions from DDS SQL iQuery for IBM i © 2017 Cozzi Productions, Inc. with Bob Cozzi [email protected] Twitter: @bobcozzi iQ SQLiQuery.com Transforming Db2 into Excel, JSON & more with SQL

Upload: others

Post on 03-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

SwitchingtoSQLFileDefinitionsfromDDS

SQLiQueryforIBMi

©2017CozziProductions,Inc.

with

[email protected]:@bobcozzi

iQSQLiQuery.comTransforming Db2 into

Excel, JSON & more with SQL

Page 2: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

DownloadthisPresentation

iQSQLiQuery.com

Transforming Db2 intoExcel and JSON using SQL

Page 3: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

• 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

Page 4: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 5: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

ClassicIBMi DDSFileDefinition

RecordFormatNameColumn(Field)Names

ColumnHeadingsandText

PrimaryKeyFields

Page 6: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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)

Page 7: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

ChangePhysicalFile(CHGPF)Command

Page 8: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

SQLfori DDLTable(File)Definition

iQSQLiQuery.com

Transforming Db2 intoExcel and JSON using SQL

Page 9: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

BasicDifferencesBetweenDDSvsDDL

• DDSisKeywordoriented• Onelineorstatementperfielddefinition• Allfieldattributesareaddedviadifferentkeywordstothesamestatement

• SQLDDLisStatement,Keyword,Clause,Valueoriented• Columndefinitionsareonestatement• Additionalcolumnattributeareaddedthroughadditionalstatements

iQSQLiQuery.com

Transforming Db2 intoExcel and JSON using SQL

Page 10: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 11: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 12: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

GeneratingSQLDDLfromExistingFiles

• TheQSQGNDDLAPIisagoodchoice• TheSQLiQueryRTVSQLSRCcommandiseasier

• TheGENERATE_SQL()procedureispervasivenottoocomplex• Oneveryone'ssystem• Hasacomplexparameterlist,butitislearnable

iQSQLiQuery.com

Transforming Db2 intoExcel and JSON using SQL

Page 13: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

SQLiQueryRTVSQLSRCCommand

Page 14: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 15: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

RunningtheGENERATE_SQLProcedure

• MustberunfromanSQLprocessor• EmbeddedinRPG• RUNSQL… IBM'shijacked"RunanSQLStatement"Command• RUNSQLSTM… IBM'sRunSQL/PLScript• RUNiQRY… SQLiQueryRunSQLStatementsorSQLandSiLQScripts• STRSQL… InteractiveSQL(theoldGreenScreenSQLenvironment)• RUNSQLScriptswhichispartofAccessClientSolutions(ACS)

• PreviouslytheClientAccessproduct

• Youhavelotsofchoices

Page 16: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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..

Page 17: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

STATEMENT_FORMATTING_OPTION=>'0'

• FromtheIBMWebsite:

formatting-option TheformattingoptionusedinthegeneratedSQLstatements.Thevalidvaluesare:• 0- Noadditionalformattingcharactersareaddedtothe

generatedSQLstatements.• 1- Additionalend-of-linecharactersandtabcharactersare

addedtothegeneratedSQLstatements.

Ifformatting-option isnotspecified,1willbeused.

• Alwaysspecify'0'oryouwillnotbehappy.

Page 18: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

GeneratingDDLfromExistingFile

• TheCUSTMASTFile(showearlier)Exists• NeedtomigratethefiletoDDLstatements

• CREATEorREPLACETABLE• LABELONTABLE• LABELONCOLUMN

• "IS"forColumnHeadings• LABLEONCOLUMN

• "TEXTIS"forColumnTextDescriptions• CREATEINDEXmayalsobegeneratedforkeyfields

• MaywanttousePRIMARYKEYinstead

• TogeneratetheSQLDDLcalltheGENERATE_SQL()Procedure

Page 19: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 20: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

GeneratedSQLfromRTVSQLSRCorGENERATE_SQL

FileName

RecordFormatName

Field("column")Names

CREATEorREPLACETABLE• Iftabledoesn'texist

• TableisCreated• Iftableexists

• Tabledefinitionisaltered

• Dataispreserved

Page 21: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

ComparingDDStoSQLDDL

?CREATEINDEXCOZTEST.CUSTMAST_QSQGNDDL_00001ONCOZTEST.CUSTMAST(CUSTNOASC);

Page 22: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

KeyFieldsinSQLDDL– ManualStep

PRIMARY KEY (CUSTNO)

?

KeyFields"PrimaryKey"

Page 23: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

RemainderofDDLTableDefinitionSource

FileText

ColumnHeadings

ColumnTextDescriptions

Page 24: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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)_

Page 25: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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)

Page 26: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

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

Page 27: Switching to SQL File Definitions from DDSsqliquery.com/datastore/iQuery_DDStoSQLDDL.pdf · •Three methods exist to transform DDS to DDL 1. Manually write the DDL 2. Use the QSQGNDDL

ForMoreInformationvisit

www.SQLiQuery.com

Questions?Email:[email protected]

iQSQLiQuery.com

Transforming Db2 intoExcel and JSON using SQL

Twitter:@bobcozzi