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



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

SQL and programming languages

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Embedded SQL programming

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

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

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

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

14 Triggers / Embedded SQL

More SQL: Assertions, Views, and Programming Techniques

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

Embedding SQL in High Level Language Programs

Appendix K Introduction to Microsoft Visual C++ 6.0

Programming Database lectures for mathema

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

12 Embedding SQL in Programming languages

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

Toad for Oracle 8.6 SQL Tuning

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

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

12 Embedding SQL in Programming languages

Using ORACLE in the CSLab

McGraw-Hill The McGraw-Hill Companies, Inc.,

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

Persistent Stored Modules (Stored Procedures) : PSM

Intro to Embedded SQL Programming for ILE RPG Developers

Developing SQL and PL/SQL with JDeveloper

Compiler Construction

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

Database Access via Programming Languages

Migrate Topaz databases from One Server to Another

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

A basic create statement for a simple student table would look like the following.

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

SQL Basics for RPG Developers

JDBC (Java / SQL Programming) CS 377: Database Systems

Comp151. Definitions & Declarations

A Brief Introduction to MySQL

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

1 File Processing Systems

Data Structures using OOP C++ Lecture 1

Oracle Database 10g Express

1 SQL Data Types and Schemas

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Introduction to the Oracle DBMS

ABAP How To on SQL Trace Analysis

Darshan Institute of Engineering & Technology PL_SQL

Simple File Input & Output

PIC 10A. Lecture 7: Graphics II and intro to the if statement

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

Real SQL Programming 1

The JAVA Way: JDBC and SQLJ

CS346: Database Programming.

Micro Focus Database Connectors

Progress Embedded SQL-92 Guide and Reference

Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

An Introduction to PL/SQL. Mehdi Azarmi

Using SQL in RPG Programs: An Introduction

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Chapter One Introduction to Programming

Database DB2 Universal Database for iseries Embedded SQL programming

Information Systems SQL. Nikolaj Popov

Physical File. Collection or Schema

Running your first Linux Program

5.1 Database Schema Schema Generation in SQL

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

Using SQL Server Management Studio

Pro*C/C++ Getting Started 10g Release 2 (10.2) for Microsoft Windows (32-Bit) B

1. The First Visual C++ Program

Pastel Evolution BIC. Getting Started Guide

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

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

Maintaining Stored Procedures in Database Application

SalesPad for Dynamics GP Credit Card Processing Setup and Credit Card Payments

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

Creating a Simple Visual C++ Program

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

FileMaker 14. ODBC and JDBC Guide

Company Setup 401k Tab

COMPUTER SCIENCE 1999 (Delhi Board)

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

A Tutorial on Stored procedures and user defined functions

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

WebSphere Business Monitor V6.2 KPI history and prediction lab

MyOra 3.0. User Guide. SQL Tool for Oracle. Jayam Systems, LLC

Module File Cache for NonStop SQL/MX

Using the Query Analyzer

Database Studio is the new tool to administrate SAP MaxDB database instances as of version 7.5.

ASP.NET Programming with C# and SQL Server

Transcription:

Embedded SQL Unit 5.1 Unit 5.1 - Embedde SQL - V2.0 1

Interactive SQL So far in the module we have considered only the SQL queries which you can type in at the SQL prompt. We refer to this as interactive SQL. SQL is a non-procedural language SQL specifies WHAT is required, not HOW this requirement is to be met. INTERACTIVE SQL is good for: defining database structure generating low-volume, ad hoc queries prototyping INTERACTIVE SQL is not good for the more sophisticated applications for which a programming language with links to SQL might be better. Unit 5.1 - Embedde SQL - V2.0 2

Embedded SQL SQL can be embedded within procedural programming languages. These language (sometimes referred to as 3GLs) include C/C++, Cobol, Fortran, and Ada. Thus the embedded SQL provides the 3GL with a way to manipulate a database, supporting: highly customized applications background applications running without user intervention database manipulation which exceeds the abilities of simple SQL applications linking to Oracle packages, e.g. forms and reports applications which need customized window interfaces Unit 5.1 - Embedde SQL - V2.0 3

SQL Precompiler A precompiler is used to translate SQL statements embedded in a host language into DBMS library calls which can be implemented in the host language. Editor Precompiler Compiler Linker host program + embedded SQL host program + translated SQL object (binary) program DBMS and other libraries executable program Unit 5.1 - Embedde SQL - V2.0 4

Sharing Variables Variables to be shared between the embedded SQL code and the 3GL have to be specified in the program. EXEC SQL begin declare section; varchar userid[10],password[10],cname[15]; int cno; EXEC SQL end declare section; We also should declare a link to the DBMS so that database status information can be accessed. EXEC SQL include sqlca; This allows access to a structure sqlca, of which the most common element sqlca.sqlcode has the value 0 (operation OK), >0 (no data found), and <0 (an error). Unit 5.1 - Embedde SQL - V2.0 5

Connecting to the DBMS Before operations can be performed on the database, a valid connection has to be established. EXEC SQL connect :userid identified by :password; In all SQL statements, variables with the : prefix refer to shared host variables, as opposed to database variables (e.g. row or column identifiers). This assumes that userid and password have been properly declared and initialised. When the program is finished using the DBMS, it should disconnect using: EXEC SQL commit release; Unit 5.1 - Embedde SQL - V2.0 6

Queries producing a single row A single piece of data (or row) can be queried from the database so that the result is accessible from the host program. EXEC SQL SELECT custname INTO :cname FROM customers WHERE cno = :cno; Thus the custname with the unique identifier :cno is stored in :cname. However, a selection query may generate many rows, and a way is needed for the host program to access results one row at a time. Unit 5.1 - Embedde SQL - V2.0 7

SELECT with a single result Host Program (eg C++) EXEC SQL BEGIN DECLARE SECTION; varchar cname[15]; int cno; EXEC SQL END DECLARE SECTION; DBMS OTHER I/O // get customer number from keyboard cout << "customer number please ->"; cin >> cno; EXEC SQL SELECT custname INTO :cname FROM customers WHERE cnum = :cno; // output customer name cout << "the customer's name is " << cname.arr; SELECT `INTO' Database Unit 5.1 - Embedde SQL - V2.0 8

Cursors - SELECT many rows A cursor provides a pointer to a single row in the result of a selection query (which may return may rows) Cursors are declared by statements similar to view definitions One row at a time is accessed through the cursor, which is moved to the next row before each data transfer The columns of that one row are fetched into the program variables which can then be manipulated in the normal way by the host program. A cursor can also be used to update values in tables. Unit 5.1 - Embedde SQL - V2.0 9

Fetching values EXEC SQL fetch <cursor-name> into <target-list> this moves the cursor to the next row of the select query result and transfers the values from the result row into the program variables specified in target-list. a special value is returned when an attempt is made to fetch a non-existent row after the last row available to the cursor, (sqlca.sqlcode > 0) Unit 5.1 - Embedde SQL - V2.0 10

Declaring and Opening a Cursor EXEC SQL declare <cursor name> cursor for <select statement>> [for update [<of column-list>]] the last element of the definition is required only if the cursor is to be used for updates or deletes on the parts of the table involved in the cursor select statement. the column-list part is omitted if the rows are to be deleted. EXEC SQL open <cursor name> the view described in the cursor declaration is created the cursor is positioned before the first row of the select query result. Unit 5.1 - Embedde SQL - V2.0 11

Program Example Host Program (eg C++) DBMS EXEC SQL BEGIN DECLARE SECTION; varchar cname[15]; int cno; Database OTHER I/O EXEC SQL END DECLARE SECTION; // declare a cursor :- EXEC SQL DECLARE cust_cr CURSOR FOR SELECT cnum,custname FROM customers; // read first row :- EXEC SQL FETCH cust_cr INTO :cno, :cname while (sqlca.sqlcode == 0) { // while there are rows process one row cname.arr[cname.len] = '\0'; cout <<"\ncustomer number is " << cno << "customer number is " << cname.arr; // get next row EXEC SQL FETCH cust_cr INTO :cno, :cnam } EXEC SQL CLOSE cust_cr; Row 1 Row 2 Row 3 etc OPEN Unit 5.1 - Embedde SQL - V2.0 12

Summary Cursors provide a means of integrating traditional 3GL procedural languages and databases by enabling row-at-atime access. Languages such as Visual Basic and Visual C++ also have build-in statements that enable this type of processing with a dedicated database, like that provided by MS-Access. Java has a well-defined interface to enable easy access to databases through a Database Connectivity library - JDBC. Unfortunately, there are many standards. Unit 5.1 - Embedde SQL - V2.0 13