anthony s. rudd implementing practical db2 applications978-1-4471-1035-4/1.pdf · db2 relational...

9
Anthony S. Rudd Implementing Practical DB2 Applications With 69 Figures , Springer

Upload: phamngoc

Post on 04-Jun-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

Anthony S. Rudd

Implementing Practical DB2 Applications With 69 Figures

, Springer

Page 2: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

Antho ny S. Rudd. MS(Hons) OateveG Paumgartnerstrasse 6-14 0 -90426 Nuremberg. GERMANY

ISBN-I3: 978-3-540-19953-3 Dar: 10.1007/978-1-4471-1035-4

e-ISBN-I3: 978-1-4471-1035-4

British Library Cataloguing in Publication Data Rudd, Anthony S.

Implementing practical OB2 applications. - 2nd ed. 1.0B2 (Computer program) 2.0atabase management - Computer progralT\$ I.Title 005.1'56'5

ISBN-I3: 918-3-540-19953-3

Library of Congrus Cataloging-in-Publ ieat ion Data A catalog record for this book is available from the Library of Congress

Apart from any fair dealing fo r the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored o r transmiued, in any fo rm or by any means, with Ihe prior permiuion in writing of the publishers, o r in the cue of reprographic reproduction in accordance wilh the te rlT\$ of licences iuued by the Copyright licensing Agency. Enquiries concerning reproduction outside those te r lT\$ should be sent to the pub/ is he rs.

e Springer-Verlag london l imited 1996

The use ofregistHed names, Irademarb etc. in th is publication does not imply, even in the abunce of a specific statement, that such names I re exempt from Ihe releyant laws and regulations Ind therefo re free for general use.

The publisher makes no representation, txpreu or implied, with regard 10 the accu racy ofth t information contained in this book and Clnnot accept any legal responsibility or liability fo r any erron or omiuions that may be made.

Typeuning: camna ready by author

3413830-543210 Printed on acid·free paper

Page 3: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

Preface

The tit le of this book "IMPLEMENTING DB2 APPLICATIONS' reveals the purpose I had in writing it: a concise , complete, source of information necessary for the development and implication of applications using IBM'S DB2 relational database package in the MVS environmenl. When I started wriling DB2 applications I was quite literally overwhelmed by the physical amount of literature available. And usually all of this literature was necessary as reference for the writing of an application , because the required infonnatian was distributed amongst the manuals. With this book I have tried to separate out that information necessary to put an application together. This book is hopefully more than a mere reference; it contains tips and notes I and my colleagues found out the hard way . I have tried to present this practical information in such a form that it is easy to find and use. I hope this will enable readers to spend more productive time developing applications, rather than trying to find out why something works in a particular way.

Good though the IBM manuals are , they suffer from two drawbacks: they have now attained a s ize (and weight) that they are unwieldy to use; reference manuals offer limited scope to explain background information or critique, where it might be required.

And who should read this book? Both beginners and expens. Beginners are lead through the steps required to put DB2 applications together; complete worked examples, large enough to be practical but devoid of superfluous

Page 4: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

vi PREFACE

detail, enable the DB2 novice to see what is necessary to implement a simple application. Experts have a compact reference for those features necessary to write a DB2 application. In particular, the mechanics of preparing and testing a DB2 application program.

What this book is not? This is not a book about SQL; there have been enough books published already on this subject. This book does not go into the details of writing SQL statements, although it is sufficiently detailed to serve as a basic reference, rather it describes how the various components fit together. Similarly, it does not treat the subjects of distributed applications and optimisation. Also, it does not discuss the internal processing and optimisation aspects of DB2 systems. These are all specialised topics, a discussion of which would conflict with the aim of having a compact book.

At this point I would like to thank Elke Berger for her help and suggestions for improvements.

Page 5: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

Table of Contents

Preface . .... . .. . . ....................... v

I. Introduction 1.1 DATABASE2 (OB2) environment ................................. I I .2 DBl data objects ......... ................ .... . ... 2 I. 3 Access to DB2 tables.. ... .. . ............................. 3 1.4 UseofSQL..... . ...... .......................... ......... 3 1.5 Use of SQL from applications ............... . ........... 4

2. Applicalion Design 2.1 Application design...... . ....... , ., ....................... . .. .. 6 2.2 Application considerations ............................................. 7

2 .2. 1 Dialogue or batch?. . ......................................... 7 2.2 .2 Command procedure ................................................... 8 2 .2 .3 Static or dynamic SQL? ................................................. 8 2.2.4 Changes to the database ................................................ to 2.2 .5 Authorisation considerations .................... . ..................... 10

2.3 Which product to use ................................................... 11

3. QMF Facilities 3. 1 Introduction ........ .. .................... . .... ... .. .. ........ . .......... . t 2 3.2 QMF objects .............................. .. ................. 12

3.2.1 QMFquery ... ............................ . ................... 13 3.2.2 QMF data ................................. .. ....................... 13 3.2 .3 QMF form ...................... ................. .. ......... 13 3.2.4 QMF report .................. ............ .. ............ 13 3.2.5 QMF procedure ........... .............. ...... .. ................... 14

Page 6: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

viii TABLE OF CONTENTS

3.2.6 3.2.7

3.3 3.4

3.4.1

3.5

3.6

3.7

3.4.2 3.4.3

3.5.1 3.5.2

3.6.1 3.6.2 3.6.3 3.6.4

3.7.1 3.7.2

QMF profile .............................................................. 14 QMF chart ................................................................ 14 QMF invocation .......................................................... 14 QMF command facilities ................................................ 15 Transfer of QMF items to/from host system ......................... 15 Administration of QMF objects ........................................ 15 Invoke host system component ....................................... 15 Invocation of QMF ...................................................... 16 QMF files ................................................................. 17 Sample CLIST for invocation of QMF ................................. 18 QMF callable interface .................................................. 18 The DSQCOMM communications area ................................ 18 Callable interface invocation syntax ................................. 20 START command .......................................................... 21 Examples ................................................................. 22 QMF command interface ............................................... 30 QMF status ................................................................ 30 Passing parameters to a QMF command ............................. 31

4. QMF Commands 4.1 4.2

4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.15 4.2.16 4.2.17 4.2.18 4.2.19 4.2.20 4.2.21 4.2.22 4.2.23 4.2.24

4.3 4.4 4.5 4.6

Introduction .............................................................. 33 Commands ............................................................... 33 DISPLAY - Display an object.. .......................................... 34 END - End current operation ........................................... 35 ERASE - Remove object from database ................................ 35 EX IT - Terminate QMF session ......................................... 36 EXPORT - Transfer object to external dataset ......................... 36 HELP - Display help information ...................................... 38 IMPORT - Impon object into the QMF environment.. ................ 38 INTERACT - Provide interactive suppon ............................... 39 ISPF - Invoke ISPF/PDF panel ......................................... .40 LIST - Display list of QMF objects .................................... 40 MESSAGE - Display message ............................................ .41 PRINT - Print content of object ....................................... .42 QMF - Execute explicit QMF command ............................... .45 RESET - Clear object. .................................................... 45 RESET QUERY - Clear query object .................................... .46 RESET GLOBAL - Clear global variable ................................. 46 RUN - Execute object .................................................... 46 SAVE - Save contents of work area ................................... .47 SAVE DATA - Save contents of data area .............................. .48 SAVE PROF I LE - Save profile ........................................... .49 SET (PROFILE) - Set profile ........................................... .49 SET GLOBAL - Set global variable ...................................... 51 STATE - Return QMF status ............................................. 51 TSO - Invoke TSO command ............................................ 51 Example .................................................................. 52 External QMF data ...................................................... 54 QMF external data format .............................................. 57 IXF data ................................................................... 58

Page 7: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

4.7 4.8 4.9

TABLE OF CONTENTS ix

QMF external data .................... ... . QMF data format .. QMF repon data .

..... .... . . ....... 62 ............. 64

... . ................ 66

5. Program Environment 5.1 Introduction. ..... .. ..................... . .. .. .............. 71 5.2 lCL procedures . . . ..................... . ................... 73

5.2.1 PU I preprocessor ............ ......................................... 75 5.2 .2 OB2 precompiler ......................... . ............. 76 5 .2 .3 Compile ........... . .................................. 79 5.2.4 Pre·linkedit .............. .. . . ....... .............................. 79 5 .2.5 Linkedit ........... ...... . . . . .. ...................... .79 5.2.6 Bind ..................... . .................................. 79

5 .3 Example ................... ......... . . . ...................... . . ..... 80 5.4 OB21 (OB2 Interactive) ..... ... . . ............. . . .. . 80

6. DB2 program Invocation 6.1 Introduction ........................ . ........... 81 6.2 Commands penaining to OB2 ........ . . ... . . ................. 82

6.2 .1 DSN - Invoke OBI session ......... .... .......... . . ............ 82 6 .2.2 OSNH - Prepare program in foreground.... . ........ 82

6.3 OB2 subcommands .......................... . .............. 83 6.3 . 1 BIND PACKAGE - Build application package . . ................... 83 6.3 .2 BIND PLAN' Build application plan ................................... 86 6.3.3 OCLGEN -Table declaration generator ....... . ................ 88 6.3 .4 END - Terminate OB2 session . .. ..... ................................... 90 6.3.5 FREE - Free application plan .. .. .. . ........................... . .. . 91 6.3 .6 REBUO - Rebind application plan ........... ........................ 91 6.3.7 RUN - Invoke OB2 program ............. . .......... . 93 6.3.8 spun -SQL Processor Using File Input ................ .. .... ... . ... 94

6.4 DB2! (OATABASE2 Interactive). . .... 94 6.4 . 1 Invoke SPUfl ..... ..................... ................................. 95 6.4.2 Invoke OCLGEN .... ... .... . . ............ . ... .. .. .... 97 6.4.3 Perform OB2 Program Preparation ... .. . . . . .. . .... 97 6.4.4 Perform OB2 Precompile ........... ........ ... . ............. 99 6.4.5 Perform OB2 Bind function......... ........... . ........... 100 6.4.6 Run an SQL program ................................................. 101 6.4.7 Set OB21 global default parameters ............. ... ................. 103

6.5 Invoke program independent of OBI (CAF interface) ............ 104 6 .5 .1 CLOSE function ......................................................... 105 6.5.2 CONNECT function ............................. . .............. 106 6.5.3 DISCONNECT function ................................................... 107 6.5.4 OPEN function .. . .......... ........ . . . .. . ....................... . . . ... 108 6.5.5 TRANSLATE function.. . ................................... 109 6.5.6 CAF interface program ............................................... 109

7. Issuing SQL statements from within a program 7. 1 Introduction ...... .. ..... . . ....... . . ..... 116 7.2 SQL status ... 117

7.2.1 DSNTIAR subrout ine ............... . ............. 118

Page 8: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

x TABLE OF CONTENTS

7.3

7.4 7.5

7.3.1 7.3.2 7.3.3 7.3.4

7.5.1 7.5.2 7.5.3 7.5.4

Program host variables .. ..................................... ..... .. 119 PU I hosl variables ..................................................... 119 COBOL host variables ................................................. 120 Assembler hOSI variables ... ........................................ . 122 CI370 host variables.... .............. . .... ................... 124 Indicator variables ......................... . ......................... 125 Use of SQL from programs. . ......................... 125 Static SQL .................. .............. . ... .......... . ... ... 125 Static SQL select with a fixed number of columns .............. . 127 Dynamic SQL .......................................................... 130 Dynamic SQL wilh a variable number of columns .......... ..... 132

8. Embedded SQL Statements 8.1 8.2

8.2.1 8.2.2 8.2.3 8.2.4 8.2.5 8.2.6 8.2.7 8.2.8 8.2.9 8.2.10 8.2. 11

8.2.12 8.2.13 8.2.14 8.2.1 5 8.2.16 8.2.17 8.2.18 8.2.19 8.2.20 8.2.21 8.2.22

Introduction ........................................................... 137 Statements ... .......................................................... 137 BEGIN DECLARE - Start declaration of hosl variables.. . ...... 138 CLOSE - Close cursor ...... ...................................... .... 139 COM/'IIT - Make outstanding database changes permanent ........ 139 DECLARE CURSOR - Define cursor ................... ... . ......... ..... 140 DECLARE STATEMENT - Define SQL statement name ................. 141 DECLARE TABLE - Define table structure ......................... .... 141 DELETE - Delete rows .................................................. 142 DESCRIBE - Obtain information aoom a prepared statement ..... 143 END DECLARE - Terminate declarations .............................. 144 EXECUTE - Execute a prepared statement ..................... .... 144 EXECUTE IMMEDIATE - Prepare and execute an executable SQL statement............ . ...................... . ... .... 145 E)(PLA t N -Obtain processing statistics ............................. 146 FETCH - Position cursor to next row ................................ 150 INCLUDE - Include definitions .................................... .. .. 151 INSERT - Insert rows ................................................... 151 LOCK TABLE - Set lock ............... .............................. ... 152 OPEN - Create results table for select.. .............................. 153 PREPARE - Prepare SQL statement .................................... 154 ROLLBACK - Revoke outstanding database changes ................ 155 SelECT INTO - Produce single-row results table .............. . . ... 155 UPDATE - Update database rows ...................................... 156 WHENEVER - Define exception processing ........................... 157

9. Transaction Processing 9.1 Introduction ........................................................... 159 9.2 SQL transaction concepts ............................................. 159 9.3 Consistency ............................................................ 160 9.4 Concurrency ........................................................... 160

10. Testing 10.1 10.2 10.3

10.3. 1

Introduction ............................................................ 164 DB2 testing tools ...................................................... 165 Testing of programs using DB2 (SQL) facilities ... ........ ....... . 165 Testing using the CAF interface...... . ...... .................. 165

Page 9: Anthony S. Rudd Implementing Practical DB2 Applications978-1-4471-1035-4/1.pdf · DB2 relational database package in the MVS environmenl. When I ... 6.3.2 BIND PLAN' Build application

10.3.2 1O.J.3

10.4 10.5

TABLE OF CONTENTS xi

Testing by creating the DB2 environment ......................... 168 Testing using ISPFfPDF Dialog Test ...... .. ... .. ........... ........ 169 CommitfRollback in the testing environment .................... 170 Summary ............ .. ............ .. .............. .. . . ........ ... ...... 170

11. Worked Example 11 . I Introduction ............................................................ 171 Il.l Problem specification . ........... ......... ............ . .. . . . . ...... 171 11.3 QMF solution...................................... . .. ........... 175 11.4 Program solution .............................................. ........ 179 11.5 Summary .. .. .............. ..... .. . .. . ... ............. ... ............... 183

Appendix A. Syntax Notation ....................................... .. .. . ......... 184 Appendix B. SQL Control Areas .... . ....... . .... ................................ 187 Appendix C. Glossary ....................................... . . .. . ..... . .... .. ....... 196

Index .. ...... . ... .............. . . . ..... .. .......... . ..................... ... ... 203