Sample COBOL SQL

Sample COBOL SQL

Standard Access to IMS Data Access from COBOL Using SQL Haley Fung, IBM [email protected] Disclaimer IBM’s statements regarding its plans, directions, and intent are subject to change or withdrawal without notice at IBM’s sole discretion. Information regarding potential future products is intended to outline our general product direction and it should not be relied on in making a purchasing decision. The information mentioned regarding potential future products is not a commitment, promise, or legal obligation to deliver any material, code or functionality. Information about potential future products may not be incorporated into any contract. The development, release, and timing of any future features or functionality described for our products remains at our sole discretion. IMS 13 SQL support for COBOL • Target Market – IMS TM and DB customers who would like to write or modify IMS COBOL applications to access IMS data using SQL • Challenge Addressed – No SQL access to IMS data from IMS COBOL applications – Lack of DL/I skills in AD community • Solution Statement – Enable SQL calls from COBOL applications in addition to the current Java-based solutions • Business Value – Expands IMS database access for application and database developers – Reduce application development cost by leveraging existing SQL skills – Provide a consolidated native SQL engine as the foundation of existing and future client exploitation Prerequisites • Software requirements – IMS 13 + PTF UK98028 – IMS Catalog function is required – COBOL compiler with IMS co-processor function • Enterprise COBOL Developer Trial z/OS V5.1 or • Enterprise COBOL for z/OS V5.1 + APAR PM92523 – Note: COBOL V5.1 requires z/OS 1.13 and above • Hardware requirements – Same as IMS 13 and COBOL V5.1 Solution Highlights • SQL support for COBOL – Offer SQL as a query language for COBOL programs to access IMS database in addition to DLI • SELECT/INSERT/UPDATE/DELETE – EXEC SQLIMS as the interface to execute IMS SQL calls • SQL processor in IMS – Process SQL calls natively by the IMS subsystem – Still perform DLI database call processing underneath – Provide a consolidated way for SQL processing – Uses database metadata in IMS Catalog • Support IMS TM/DB (MPP, IFP, BMP) and DBCTL BMP IMS SQL concepts What is SQL? • SQL stands for Structured Query Language – SQL is a standard and is well defined – Most common database language for relational databases – Support for SQL can be found in programming languages such as Java, .NET, C++, COBOL, PL/I, etc • IMS and SQL – Introduced SQL support since IMS V7 for Java application – IMS V11 provided Open Database Solution allowing Java applications to access IMS databases over TCP/IP networks and off platform – Supports both data access and data manipulation with SQL. It maps hierarchical structures to relational concepts Hierarchical to Relational Mapping Hierarchical Design Relational Design Dealer Table Dealer DealerID DealerName DealerAddress Segment Row 1 - 0 53SJ7 George 555 Bailey Ave. 1 53SJ8 Bob 240 Elm St. 53SJ9 | Mary | 111 Penny Lane 2 53SJ9 Mary 111 Penny Ln. Segment 1 53SJ8 | Bob | 240 Elm St. 53SJ7 | George | 555 Bailey Ave. Row N - ... ... ... ... (Row 1) - Model JPR27 | Dodge | Durango Segment WJ45 | Mercury | Cougar Model Table UU45 | Dodge | Viper ID Make Model Dealer PR27 | Dodge | Durango Row 1 - UU45 Dodge Viper 53SJ7 0 FF13 | Toyota | Camry PR27 Dodge Durango 53SJ7 0 FF13 Toyota Camry 53SJ7 0 JR27 Dodge Durango 53SJ8 1 WJ45 Mercury Cougar 53SJ8 1 Row N - ... ... ... ... ... Other Terminologies • Relational databases versus IMS databases – Table (relational DB) Segment (IMS DB) – Column Field – Row Segment Instance – Schema PCB – Table primary key Segment unique key • Basic definitions – Statement - standardized set of commands used for data access, manipulation. Similar to executing a series of DL/I calls against a DB PCB. – Result Set – statements return a set of results based on the command executed. Similar to the IO Area returned on a DL/I call. IMS Catalog • Trusted, comprehensive view of IMS database metadata (including application metadata) managed by IMS – PSB, DBD and copybooks • SQL support for COBOL directly access IMS Catalog for database metadata – No need to generate metadata for use in applications PSB PSBGEN source PSBLIB ACBLIB IMS IMS Explorer ACBGEN DBD DBDGEN source DBDLIB DBDLIB Catalog IMS 13 SQL support for COBOL Statements – SQL to access data • Data manipulation – SELECT… FROM… to retrieve data – INSERT INTO… VALUES… to insert data – UPDATE… SET… to update data – DELETE FROM… to delete data – WHERE… AND… OR… to perform conditional selection of data Statements – Declaration • Pre-compiler directives – DECLARE CURSOR, STATEMENT… to declare cursor, statement – INCLUDE SQLIMSCA, SQLIMSDA… to generate SQLIMSCA and SQLIMSDA structures – WHENEVER… to handle errors and warnings Statements – Execution • SQL statement execution – For executing a SELECT statement • PREPARE … to parse SQL statement • DESCRIBE (optional)… to get resultset metadata • OPEN … to open cursor • FETCH … to fetch data • CLOSE … to close cursor processing – For executing an INSERT/UPDATE/DELETE statement • PREPARE… to parse SQL statement • EXECUTE… to execute the non-SELECT statement Solution Details – Key application elements • Delimit SQL statement using EXEC SQLIMS ... END-EXEC • Dynamic SQL programming model – Must call PREPARE to process SQL statement • Host variables – Use for both to send and receive data processed by IMS • SQL communication area (SQLIMSCA) – Structure used by IMS to provide status feedback – SQLIMSCODE (error code), SQLIMSSTATE (state), SQLIMSERRM (error message) • SQL description area (SQLIMSDA) – Structure populated by IMS to provide result set metadata for a SELECT statement • Describe name, length, type of each column returned – Structure populated by application to provide input data Solution Details – Dynamic SQL • Enable SQL statement to be constructed at runtime – No need to hard code SQL statement in the application – Segment and field names associated with the SQL calls are not known at compile time – SQL accepts as input in the form of a character string • Use Prepare call to process SQL statement – Parse SQL statement for syntax and semantics validation at runtime. No bind process. – Convert SQL artifacts to DLI – Statement can be prepared once and then execute many times Coding - High-level application flow Declare host variable/structures for passing data between IMS and application Include SQLIMSCA for execution status Code SELECT, INSERT, UPDATE or DELETE statements to access IMS data Check SQLIMSCA for execution status Handle any SQL errors returned by IMS Coding - SELECT • SELECT statements are used to query one or more tables – SELECT column1, column2, … FROM table_name WHERE some_column=some_value ORDER BY column1, column2 • Programming models – PREPARE… OPEN… FETCH (one or more)… CLOSE – Fixed-list SELECT • Returns rows that contain a known number of values of unknown type and size. You can specify a list of host variables to contains the filled values. – Varying-list SELECT • Returns rows that contain an unknown number of values of unknown type and size. You do not know the exact number and types of host variables to store the result values. Coding Fixed-list SELECT 1. Include SQLIMSCA 2. Specify SQL statement in a COBOL variable 3. Declare host variable or structure for result data row 4. Declare a cursor for the statement name 5. Prepare the SELECT statement 6. Open the cursor 7. Fetch a row of data into host variable or structure. 8. Repeat previous step until not more data . When no more data, SQLIMSCODE=100 9. Handle any error 10.Close the cursor Sample COBOL SQL WORKING-STORAGE SECTION. * Declare SQLIMSCA EXEC SQLIMS INCLUDE SQLIMSCA END-EXEC. * Declare COBOL variable for SQL statement and result data 01 SQL-STATEMENT 49 SQL-STATEMENT-LEN PIC S9(4) COMP. 49 SQL-STATEMENT-TEXT PIC X(100). 01 HOSPITAL-RESULT-ROW 05 HOSPLL PIC S9(3) BINARY. 05 HOSPCODE PIC X(12). 05 HOSPNAME PIC X(17). PROCEDURE DIVISION. * Declare Cursor for the Prepared Statement EXEC SQLIMS DECLARE CURSOR cursor-name for prepared-statement-name END-EXEC. * Load SQL statement in the COBOL variable MOVE "SELECT * FROM PCB01.HOSPITAL” TO SELECT-STATEMENT-TXT. Sample COBOL SQL (Cont’d) * Prepared SQL statement string for processing EXEC SQLIMS PREPARE prepared-statement-name FROM :SQL-STATEMENT END-EXEC. * Open Cursor EXEC SQLIMS OPEN cursor-name END-EXEC. * Execute SQL statement * Fetch data from IMS into host variable until no more data is found PERFORM FETCH-PROC UNTIL SQLCODE EQUAL 100. : FETCH-PROC. EXEC SQLIMS FETCH cursor-name INTO :HOSPITAL-RESULT-ROW END-EXEC. : * Close Cursor EXEC SQLIMS CLOSE cursor-name END-EXEC. Coding Varying-list SELECT 1. Include SQLIMSCA 2. Include SQLIMSDA for result metadata 3. Specify SQL statement in a COBOL variable 4. Declare a cursor for the statement name 5. Prepare the SELECT statement 6. Call DESCRIBE to get result metadata 7. Declare and allocate storage for the result data variables 8. Open the cursor 9. Fetch a row of data into SQLIMSDA with result data variables 10. Repeat previous step until not more data . When no more data, SQLIMSCODE=100 11. Handle any error 12. Close the cursor SQL descriptor area (SQLIMSDA) • SQLIMSDA stores information about prepared SQL statements or host variables. • Can be read by IMS or the application – Read by application after a DESCRIBE statement for result set metadata – Read by IMS for the input host variables Coding – INSERT/UPDATE/DELETE

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    36 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us