/* Errors from here on will cause the program to clean up */



Similar documents
Chapter 9, More SQL: Assertions, Views, and Programming Techniques

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

Oracle SQL, introduced in the previous chapter, is not a language that can be

Embedded SQL programming

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

12 Embedding SQL in Programming languages

12 Embedding SQL in Programming languages

PL/SQL. Database Procedural Programming PL/SQL and Embedded SQL. Procedures and Functions

Embedding SQL in High Level Language Programs

More SQL: Assertions, Views, and Programming Techniques

Database DB2 Universal Database for iseries Embedded SQL programming

EXEC SQL CONNECT :userid IDENTIFIED BY :passwd;

Programming Database lectures for mathema

Using SQL in RPG Programs: An Introduction

ERserver. DB2 Universal Database for iseries SQL Programming with Host Languages. iseries. Version 5

Firebird. Embedded SQL Guide for RM/Cobol

Gunter Saake Kai-Uwe Sattler Andreas Heuer. 4. Auflage. Datenbanken. Konzepte und Sprachen

Choosing a Data Model for Your Database

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Oracle For Beginners Page : 1

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

Darshan Institute of Engineering & Technology PL_SQL

Performance Implications of Various Cursor Types in Microsoft SQL Server. By: Edward Whalen Performance Tuning Corporation

ERserver. iseries. DB2 Universal Database for iseries SQL Programming with Host Languages

Teradata Database. SQL Reference. Stored Procedures and Embedded SQL

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

SQL and programming languages

14 Triggers / Embedded SQL

13 Classes & Objects with Constructors/Destructors

Using Casio Graphics Calculators

ERserver. Embedded SQL programming. iseries. Version 5 Release 3

Oracle Database: SQL and PL/SQL Fundamentals

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

InterBase 6. Embedded SQL Guide. Borland/INPRISE. 100 Enterprise Way, Scotts Valley, CA

Physical File. Collection or Schema

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

SQL/PSM. Outline. Database Application Development Oracle PL/SQL. Why Stored Procedures? Stored Procedures PL/SQL. Embedded SQL Dynamic SQL

SQLITE C/C++ TUTORIAL

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Mimer SQL. Programmer s Manual. Version 8.2 Copyright 2000 Mimer Information Technology AB

Handling PL/SQL Errors

C Programming Language

SQL is capable in manipulating relational data SQL is not good for many other tasks

Oracle Database: Program with PL/SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

6. Control Structures

ABAP How To on SQL Trace Analysis

Oracle For Beginners Page : 1

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: Program with PL/SQL

Oracle 10g PL/SQL Training

Part 16: Application. Programming I. 16. Application Programming I (Embedded SQL, ODBC, JDBC) References:

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

Querying Microsoft SQL Server

Language Reference Guide

Persistent Stored Modules (Stored Procedures) : PSM

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Handling PL/SQL Errors

C H A P T E R Condition Handling

CS346: Database Programming.

Illustration 1: Diagram of program function and data flow

Guide to the Superbase. ODBC Driver. By Superbase Developers plc

SQL. Agenda. Where you want to go Today for database access. What is SQL

Chapter 5 Programming Statements. Chapter Table of Contents

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

PL / SQL Basics. Chapter 3

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Querying Microsoft SQL Server 2012

Oracle Database: Program with PL/SQL

Course ID#: W 35 Hrs. Course Content

ODBC Sample Application for Tandem NonStop SQL/MX

Database Extensions Visual Walkthrough. PowerSchool Student Information System

Course 10774A: Querying Microsoft SQL Server 2012

HP NonStop SQL Programming Manual for TAL

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Querying Microsoft SQL Server 20461C; 5 days

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Sources: On the Web: Slides will be available on:

Oracle Database: Program with PL/SQL

Introduction to Structured Query Language

Intro to Embedded SQL Programming for ILE RPG Developers

Database SQL messages and codes

CS 241 Data Organization Coding Standards

arrays C Programming Language - Arrays

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

Database Programming with PL/SQL: Learning Objectives

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

In the March article, RPG Web

Introduction to PL/SQL Programming

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

MOC QUERYING MICROSOFT SQL SERVER

An Introduction to PL/SQL. Mehdi Azarmi

IBM Informix. IBM Informix Object Interface for C++ Programmer s Guide. Version 3.50 SC

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

Oracle Database: Program with PL/SQL

Transcription:

/* Include statements. */ #include EXEC SQL INCLUDE SQLCA; /* Function prototypes */ void cleanup(void); /* ------ MAIN ------ */ /* Top level function */ void main(void) { EXEC SQL BEGIN DECLARE SECTION; char host_name[21]; int host_emp_number; EXEC SQL END DECLARE SECTION; EXEC SQL DECLARE empcsr CURSOR FOR SELECT name, emp_number FROM employees WHERE emp_number = 10001; /* An error when opening the frans database will cause the error to be printed and the program to be aborted */ EXEC SQL WHENEVER SQLERROR STOP; EXEC SQL CONNECT frans; /* Errors from here on will cause the program to clean up */ EXEC SQL WHENEVER SQLERROR CALL cleanup; 1

EXEC SQL OPEN empcsr; printf("some values from the /"employees/" table /n"); /* When ever no more rows are fetched, close the cursor */ EXEC SQL WHENEVER NOT FOUND GOTO close_empcsr; /* The last executable SQL statement was OPEN so we know that the value of "sqlcode" cannot be SQLERROR or NOT FOUND */ while(sqlca.sqlcode == 0) { /* Loop is broken by NOT FOUND */ EXEC SQL FETCH empcsr INTO :host_name, :host_emp_number; /* This "printf" does not execute after the previous FETCH returns the NOT FOUND condition */ printf("%s %d/n",host_name,host_emp_number); /* From this point onwards the program ignore all errors. Also turn off the NOT FOUND condition for consistency */ EXEC SQL WHENEVER SQLERROR CONTINUE; EXEC SQL WHENEVER NOT FOUND CONTINUE; close_empcsr: EXEC SQL CLOSE empcsr; EXEC SQL DISCONNECT; 2

/* ------ CLEAN UP ------ */ /* Error handling procedure (print error and disconnect) */ void cleanup(void) { EXEC SQL BEGIN DECLARE SECTION; char errmsg[10]; EXEC SQL END DECLARE SECTION; EXEC SQL INQUIRE_SQL (:errmsg = ERRORTEXT); /* Get error message. */ /* Alternatively to get only the error number EXEC SQL COPY SQLERROR INTO :errmsg WITH 256; */ printf("aborting because of error: /n%s/n",errmsg); EXEC SQL DISCONNECT; exit(-1); 3

Discussion 1. EXEC SQL INCLUDE: The INCLUDE statement provides a means of including external SQL files in the source code. Here it is used to include the SQLCA structure. 2. EXEC SQL BEGIN... END DECLARE SECTION: Host variables must be declared prior to their use. Host variables can be global or local. 3. EXEC SQL DECLARE CURSOR: Names a cursor for use with a specified set of retrieval criteria. Once declared the cursor can be opened, using an OPEN statement, which causes the select statement specified in DECLARE CURSOR to be executed. The run time retrieval actually occurs when a FETCH is subsequently performed. When all processing has been completed the cursor can be closed with the CLOSE statement. 4. EXEC SQL WHENEVER SQLERROR STOP: The WHENEVER statement stipulates that some action occurs when a given condition is satisfied. In this case the action is STOP and the condition SQLERROR. The SQLERROR condition is satisfied if sqlcode in the SQCLA structure is negative (this indicates that an error has occurred.) The STOP action simply terminates program execution after printing an error message. 5. EXEC SQL CONNECT: Connects the program to the named database. The statement must precede any statements using the database. 6. EXEC SQL WHENEVER SQLERROR CALL: Similar to 4. The CALL action is used to call a host language procedure (cleanup in this case). No arguments can be passed to the procedure. 4

7. EXEC SQL OPEN: Opens the cursor (empcsr) for processing. A cursor must be opened before any data manipulation statements can be performed e.g. a FETCH statement. 8. EXEC SQL WHENEVER NOT FOUND GOTO: Similar to 4 and 6. The NOT FOUND condition becomes true when sqlcode in the SQLCA structure is set to a value of 100 thus indicating that (say) a FETCH statement affected no rows. 9. EXEC SQL FETCH: Used by the cursor (empcsr). It first advances the open cursor one row. Next it loads the values indicated in the SELECT clause of the DECLARE CURSOR statement into the host variables listed in its INTO clause. Once loaded into the host variables the values can be further processed. 10. EXEC SQL WHENEVER SQLERROR CONTINUE and EXEC SQL WHENEVER NOT FOUND CONTINUE: Similar to 4, 6 and 8. The CONTINUE action simply means that no action should be taken based on the associated condition, the program proceeds to the next statement. 11. EXEC SQL CLOSE: Close the open cursor (empcsr) 12. EXEC SQL DISCONNECT: Terminate access to the (frans) database. 13. EXEC SQL COPY SQLERROR: Allows access to text of error messages. Thus when an error occurs, i.e. sqlcode is negative, the text of the error message can be put into the name variable errmsg) and printed. 5