DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN.

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
PL/SQL.
Embedded SQL (Cont.) Pertemuan 10 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
1 Minggu 6, Pertemuan 11 Programmatic SQL Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
A Guide to SQL, Seventh Edition. Objectives Embed SQL commands in PL/SQL programs Retrieve single rows using embedded SQL Update a table using embedded.
Introduction to PL/SQL
Where Do We Start? How Do We Debug? Sources of Debug Information –CICS Transaction Abends –Batch Abend Codes –System Codes –Message Logs –DB2 SQL Codes.
SEMESTER 1, 2013/2014 DB2 APPLICATION DEVELOPMENT OVERVIEW.
Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Cursor and Exception Handling By Nidhi Bhatnagar.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
A Free sample background from © 2006 By Default! R.SARAVANA KUMAR S.NAVEEN Relational Database Model.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Converting To Anthony Tichonoff Florida Hospital MIS January 2007 With Ease Multi-Row Fetch.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan Introduction to Concurrency Control Different types.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Inserting SQL into HLL programs
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Application Program Design Day3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/TP01/003 Version No: 1.0 Objectives Basic CICS programming –Structure.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
1 Handling Exceptions Part F. 2 Handling Exceptions with PL/SQL What is an exception? Identifier in PL/SQL that is raised during execution What is an.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Chapter 8 Embedded SQL.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
implicit and an explicit cursor
In this module, we will design a program to work with the PROJECT table, learning to insert a new project into the table, to delete one or more projects.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Chapter 4 An Introduction to SQL.
Introduction to SQL Programming Techniques
A Guide to SQL, Seventh Edition
Interacting with the Oracle Server
Handling Exceptions.
DB2.
Introduction to Database
Unit I-2.
Chapter 8 Advanced SQL.
Dynamic SQL The dynamic SQL component of SQL allows programs to construct and submit SQL queries at run time. In contrast, embedded SQL statements must.
Presentation transcript:

DB2

2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN Hands On SQLCA Copy Book and its Use Single Row Manipulation Multiple Row Manipulation Cursors

3 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

4 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

5 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

6 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

7 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

8 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

9 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a SPUFI SQL Processor Using File Input

10 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

11 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

12 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

13 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

14 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

15 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

16 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

17 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

18 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

19 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EMBEDDED SQL

20 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Embedded SQL Embedded SQL refers to the use of standard SQL statements within a high-level language program such as COBOL, PL1, C, PASCAL, etc,. The high level language is called Host language Embedded SQL statements are preprocessed by SQL Pre processor before the application program is compiled

21 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Embedded SQL Embedded SQL syntax is almost same as the SQL syntax used in interactive mode. The output of a query is directed to a pre-defined set of variables instead of the terminal. These variables are defined in the host language and are referred to as the host variables. An additional INTO clause is placed after the SQL SELECT statement.

22 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Types of SQL 1.Static SQL: The application programmer knows in advance the SQL statement completely. 2. Dynamic SQL: The application programmer is unaware of the SQL statement in advance. This requires dynamic compilation and binding.

23 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Host Variables(1 of 2) Host variables are just like any other variable of the high level language They are referred as host variables because they are used for receiving data from the table or inserting data to the table. One must declare host variables for all values that are to be passed between the application program and DB2. The host variables may appear anywhere in the WORKING-STORAGE SECTION. The data types of the DB2 columns and corresponding host variables must be compatible.

24 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Host variables (2 of 2) The host variables can not be group items, the only exception to this rule is the variable corresponding to VARCHAR. The host variables may be grouped together in a host structure. The host variables can be Redefined The host variable cannot be Renamed.

25 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Declaring Host Variables Need to declare in the working-storage section of your program EXEC SQL BEGIN DECLARE SECTION END-EXEC. 01 EMPLOYEE-REC. 03 EMP-NO PIC S9(4) COMP. 03 EMP-NAME PIC X(15). 03 EMP-ADDRESS. 49 EMP-ADDRESS-LEN PIC S9(4) COMP. 49 EMP-ADDRESS-TEXT PIC X(25). EXEC SQL END DECLARE SECTION END-EXEC.

26 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a DCLGEN Generating copybook of host variables

27 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

28 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

29 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

30 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

31 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

32 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

33 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

34 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

35 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

36 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

37 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a

38 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Using DCLGEN copybook EXEC SQL INCLUDE STUD627 END-EXEC

39 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a SQLCA This is the communication area (a copybook containing some variables) through which DB2 passes the feedback of SQL execution to the program In the working-storage section use WORKING-STORAGE SECTION. EXEC SQL INCLUDE SQLCA END-EXEC.

40 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a SQLCA Copy Book (1 Of 3) 01 SQLCA. 03 SQLCAID PIC X(8). 03 SQLCABC PIC S9(9) COMP. 03 SQLCODE PIC S9(9) COMP VALUE SQLERRM. 49 SQLERRML PIC S9(4) COMP. 49 SQLERRMC PIC X(70). 03 SQLERRP PIC X(8). 03 SQLERRD OCCURS 6 PIC S9(9) COMP. 03 SQLWARN. 05 SQLWARN0 PIC X. 05 SQLWARN1 PIC X. 05 SQLWARN2 PIC X. 05 SQLWARN3 PIC X. 05 SQLWARN4 PIC X. 05 SQLWARN5 PIC X. 05 SQLWARN6 PIC X. 05 SQLWARN7 PIC X. 03 SQLSTATE PIC X(5 )

41 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a 03 SQLCAID For Reading dumps. 03 SQLCABC Length of SQLCA (136 bytes). 03 SQLCODE = 0 (Successful) = +ve (Exceptional condition) = -ve(Failure) 03 SQLERRM. 49 SQLERRML Error Message Length. 49 SQLERRMC Error Message Text. 03 SQLERRP Info about internal Error. 03 SQLERRD(1)Internal Error Code. 03 SQLERRD(2)Internal Error Code. 03 SQLERRD(3)Number of Rows affected by INSERT, UPDATE and DELETE. 03 SQLERRD(4)Estimate of resources needed by dynamic SQL statement. 03 SQLERRD(5)Info about dynamic SQL. 03 SQLERRD(6)Internal Error Code. SQLCA Copy Book (2 of 3)

42 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a 05 SQLWARN0 ‘W’ if any other SQLWARNx fields are set to ‘W’. 05 SQLWARN1 ‘W’ indicates truncation of character strings. 05 SQLWARN2 ‘W’ indicates null values were ignored. Ex: AVG, COUNT. 05 SQLWARN3 ‘W’ indicates more columns than host variables. 05 SQLWARN4 ‘W’ indicates no WHERE clause for UPDATE or DELETE. 05 SQLWARN5 ‘W’ indicates SQL/DS statement. 05 SQLWARN6 ‘W’ for adjustment of DATE. 05 SQLWARN7Reserved for Future use. SQLCA Copy Book (3 of 3)

43 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Programming guidelines (1 of 2) Every SQL statement must be coded between columns 12 and 72. Every SQL statement must be delimited between EXEC SQL and END-EXEC. All the tables that are used in a program are to be declared in the WORKING- STORAGE SECTION. This can be done using INCLUDE statement All SQL statements other than INCLUDE and DECLARE TABLE must appear in PROCEDURE DIVISION. Anything on a line within an SQL statement following two hyphens is treated as a comment. Even * can be coded in column 7 for comments.

44 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (1 of 6) EXEC SQL SELECT EMPNO, SALARY INTO :WS-EMPNO, :WS-SALARY FROM EMMPLOYEE WHERE NAME = :WS-NAME END-EXEC SELECT IF SQLCODE = 0 CONTINUE ELSE IF SQLCODE = -811 DISPLAY “MULTIPLE ROWS” ELSE PERFORM C9000-ERROR-PARA. Error Handling

45 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (2 of 6) EXEC SQL INSERT INTO EMPLOYEE (EMPNO, NAME, SALARY) VALUES (:WS-EMPNO, :WS-NAME, :WS-SALARY) END-EXEC INSERT IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”. Error Handling

46 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (3 of 6) EXEC SQL UPDATE EMPLOYEE SET SALARY = :WS-SALARY WHERE EMPNO = END-EXEC UPDATE IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”. Error Handling

47 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (4 of 6) EXEC SQL DELETE FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO END-EXEC DELETE IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”. Error Handling

48 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (5 of 6) EXEC SQL SELECT NAME, SALARY INTO :WS-NAME, :WS-SALARY:WS-SALARY-IND FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO END-EXEC Selecting Nullable columns(Columns which can take NULL values) Indicator variable values and their meanings (1) 0Not Null (2)-1Null (3)-2Exceptional condition

49 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Single Row Manipulation (6 of 6) MOVE-1 TO WS-SALARY-IND EXEC SQL INSERT INTO EMPLOYEE(SALARY) VALUES:WS-SALARY:WS-SALARY-IND END-EXEC Inserting Null values to columns IF SQLCODE = 0 CONTINUE ELSE DISPLAY “UNIDENTIFIED ERROR OCCURRED”. Error Handling

50 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Cursors (1 of 7) Are memory structures used to handle multiple row selections at a time. Conceptually they is a results table used by DB2 to contain the multiple results of a query. They are data structures which hold some/all the results of a query. Are defined in the WORKING- STORAGE SECTION/ PROCEDURE DIVISION.

51 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Cursors (2 of 7) Operations associated with a Cursor 1.DECLARE 2.OPEN 3.FETCH 4.CLOSE

52 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EXEC SQL DECLARE EMPCUR CURSOR FOR SELECT EMPNO,NAME,SALARY FROM EMPLOYEE WHERE EMPNO > :WS-EMPNO END-EXEC Declaring a Cursor Note: This is just the definition, DB2 executes this statement when we open it. Cursors (3 of 7)

53 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EXEC SQL OPEN EMPCUR END-EXEC Opening a Cursor Cursors (4 of 7) EXEC SQL CLOSE EMPCUR END-EXEC Closing a Cursor

54 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EXEC SQL FETCH EMPCUR INTO :WS-EMPNO,:WS-NAME,:WS-SALARY END-EXEC Fetching results from a cursor Cursors (5 of 7) Remarks: 1. We get one row at a time. 2. Only forward READ till SQLCODE = 100.

55 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EXEC SQL DECLARE EMPCUR CURSOR SELECT EMPNO, NAME, SALARY FROM EMPLOYEE WHERE EMPNO > :WS-EMPNO FOR UPDATE OF SALARY END-EXEC Fetching to Update Cursors (6 of 7) Note: This gives a U lock on the records.

56 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a EXEC SQL UPDATE EMPLOYEE SET SALARY = :WS-SALARY WHERE CURRENT OF EMPCUR END-EXEC Updating a row fetched from a cursor Cursors (7 of 7) Note: This updates exactly one row even if we do not give the key value.

57 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Summary SPUFI Hands On Introduction to Embedded SQL DCLGEN Hands On SQLCA Copy Book and its Use Single Row Manipulation Multiple Row Manipulation Cursors

58 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Thank You!