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



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

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

Embedded SQL programming

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

Using SQL in RPG Programs: An Introduction

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

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

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

Embedding SQL in High Level Language Programs

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

Database DB2 Universal Database for iseries Embedded SQL programming

Teradata Database. SQL Reference. Stored Procedures and Embedded SQL

Darshan Institute of Engineering & Technology PL_SQL

Real SQL Programming. Embedded SQL Call-Level Interface Java Database Connectivity

CS346: Database Programming.

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

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

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

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

CSC 443 Data Base Management Systems. Basic SQL

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

14 Triggers / Embedded SQL

Database Access via Programming Languages

How To Create A Table In Sql (Ahem)

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

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

There are five fields or columns, with names and types as shown above.

12 Embedding SQL in Programming languages

12 Embedding SQL in Programming languages

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

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

Intro to Embedded SQL Programming for ILE RPG Developers

Oracle Database: SQL and PL/SQL Fundamentals

Information Systems SQL. Nikolaj Popov

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

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

Real SQL Programming 1

Choosing a Data Model for Your Database

Oracle Database: SQL and PL/SQL Fundamentals NEW

1 SQL Data Types and Schemas

Oracle Database: SQL and PL/SQL Fundamentals

Lecture 6. SQL, Logical DB Design

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

Programming Database lectures for mathema

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

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

UltraLite C and C++ Programming

Firebird. Embedded SQL Guide for RM/Cobol

Week 1 Part 1: An Introduction to Database Systems. Databases and DBMSs. Why Use a DBMS? Why Study Databases??

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

In the March article, RPG Web

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

ABAP How To on SQL Trace Analysis

Week 5: Embedded SQL. Embedded SQL 4. Application Program. Interactive vs. Non-Interactive SQL. Update Statements

Summary on Chapter 4 Basic SQL

Introduction to SQL: Data Retrieving

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

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

Handling PL/SQL Errors

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

Introduction to PL/SQL Programming

Oracle For Beginners Page : 1

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

The Relational Algebra

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

SQL and programming languages

Training Guide. PL/SQL for Beginners. Workbook

SQL Basics for RPG Developers

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

CS 338 Join, Aggregate and Group SQL Queries

KB_SQL Programmer s Reference Guide

IBM Power Systems Software. The ABCs of Coding High Performance SQL Apps DB2 for IBM i. Presented by Jarek Miszczyk IBM Rochester, ISV Enablement

New York University Computer Science Department Courant Institute of Mathematical Sciences

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

PL/SQL (Cont d) Let s start with the mail_order database, shown here:

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

HP NonStop SQL Programming Manual for TAL

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

SQL Nested & Complex Queries. CS 377: Database Systems

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

Database Access from a Programming Language:

SQL-99: Schema Definition, Basic Constraints, and Queries

Using Temporary Tables to Improve Performance for SQL Data Services

Creating Customized Oracle Forms

VIEWS virtual relation data duplication consistency problems

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

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

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

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

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

T-SQL STANDARD ELEMENTS

Handling PL/SQL Errors

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Instant SQL Programming

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

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

SQLITE C/C++ TUTORIAL

Transcription:

Chapter 9, More SQL: Assertions, Views, and Programming Techniques 9.2 Embedded SQL SQL statements can be embedded in a general purpose programming language, such as C, C++, COBOL,... 9.2.1 Retrieving Single Tuples with Embedded SQL exec sql include sqlca; exec sql begin declare section; char first name[namesize]; char last name[namesize]; char ssn[10]; exec sql end declare section; strcpy(ssn, 987987987 ); exec sql select fname, lname into :first name, :last name from employee where ssn = :ssn; if (sqlca.sqlcode == 0) printf( %s, %s, first name, last name); else printf( No matching employee ); The embedded SQL statements is distinguished from the programming language statements by prefixing it with a command, EXEC SQL, so that a preprocessor can separate them from the host language code, and the SQL statements are terminated by a matching END-EXEC, or ;. Host variable/shared variable: Within embedded SQL statements, we can refer to program variables (we call them shared variables), which are prefixed by a : sign. 1

This allows shared variables and database objects, such as attributes and relations, to have the same names. The shared variables used in embedded SQL statements should be declared somewhere else in the program. The declaration should preceded by exec sql begin declare section; and ended by exec sql end declare section;. SQL communication area (sqlca): After each SQL statement is executed, the DBMS provides feedback on whether the statement worked properly. This information is returned, via a collection of variables, to an area called sqlca (memory) that is shared by the host programming language and the SQL DBMS. exec sql include sqlca; should be put somewhere in the program, the SQL compiler will insert the sqlca variables in place of the exec sql include sqlca; statement. A variable in sqlca is called sqlcode which returns the status of each SQL statement execution. 0 means a successful execution; 100 means no more data/not found; < 0 means errors. another variable in sqlca is sqlstate, which is a string of 5 characters. A value of 00000 means no error or exception; other values indicates various errors or exceptions. 9.2.2 Retrieving Multiple Tuples with Embedded SQL Using Cursors exec sql begin declare section; char first name[namesize]; char last name[namesize]; exec sql end declare section; exec sql declare emp dept cursor for select fname, lname 2

from where employee dno=:dnumber; exec sql open emp dept; while (sqlca.sqlcode == 0){ exec sql fetch emp dept into :first name, :last name; if (sqlca.sqlcode == 0) printf( First Name: %s, Last Name: %s, first name, last name); else printf( ERROR MESSAGE ); } exec sql close emp dept; The Cursor structure represents an area in memory allocated for temporarily storing and processing the results of an SQL SELECT statement. The cursor-name (emp dept in above example) is the name assigned to the cursor structure. The select statement defines the query. The declare cursor statement is declarative; the query is not executed at this time. The open statement open the cursor, and the select statement defined in declare cursor statement is executed and the set of tuples is stored in the cursor structure. This open statement will set a pointer (current pointer) pointing to the position before the first row of the query result. The fetch statement fetches one row from the result into the host variables and moves the pointer to the next row in the result of the query. The close statement closes the cursor. Update command in embedded SQL: 3

Update without cursor structure: exec sql update employee set salary = salary * 1.1 where dno=5; Update with cursor structure: exec sql declare emp d5 cursor for select ssn, salary from employee where dno=5 for update of salary; exec sql update employee set salary = salary * 1.1 where current of emp d5; The cursor structure must be opened and positioned (using FETCH) on a row before the UPDATE command can be executed. Each execution of the UPDATE statement updates one row - the row at which the cursor is positioned. The only columns that can be updated are those listed in the FOR UPDATE OF clause of the DECLARE CURSOR statement. The cursor is not moved by the execution of the UPDATE statement. The FETCH statement moves the cursor. 9.2.3 Specifying Queries at Runtime Using Dynamic SQL Dynamic SQL allows a program to form an SQL statement during execution. Parparing a statement: exec sql prepare update salary from update employee 4

set salary = salary * (1 +?/100) where dno =? ; The question mark indicates that when the statement is executed, the value of the shared variable will be used. Executing prepared SQL: Statements other than SELECT statements, and SELECT statements that return only a single row, are executed with the EXECUTE statement. exec sql execute update salary using :rate, :dnumber; Using prepare statement and declare cursor statement: exec sql prepare update salary from update employee set salary = salary * (1 +?/100) where current of emp ; exec sql declare cursor emp for select ssn, salary where dno = :dnumber for update of salary; exec sql open emp; exec sql fetch emp into :ssn, :salary; exec sql execute update salary using :rate; 5