Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
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.
CSC343 – Introduction to databases – A. Vaisman1 Database Application Development.
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.
1 Minggu 6, Pertemuan 11 Programmatic SQL Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Murali Mani SQL with other Programming Languages.
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Database Management Systems (DBMS)
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
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.
Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Agenda Journalling More Embedded SQL. Journalling.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 HW3 Oct. 2 to –Available Oct 2 only until 2 pm.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Stored Procedures, Transactions, and Error-Handling
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Advanced SQL: Cursors & Stored Procedures
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Database Technology Jing Shen.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
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.
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.
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.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Views, Sequence, and Stored Procedure used in PosgreSQL.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
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.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Introduction to SQL Programming Techniques
Interacting with the Oracle Server
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Embedded DB2.
Lecture 17: Systems Aspects of SQL
Chapter 8 Advanced SQL.
Lecture 05: SQL Systems Aspects
SQL (Structured Query Language)
Presentation transcript:

Programmatic SQL Shaista Khan CS 157B

Topic Embedded SQL statements in high-level programming languages.

Why Embedded SQL? SQL standard lacked “computational completeness”: it contained no flow of control commands such as IF, THEN, ELSE, GOTO, DO, or DO…WHILE. SQL standard lacked “computational completeness”: it contained no flow of control commands such as IF, THEN, ELSE, GOTO, DO, or DO…WHILE. To overcome this and to provide more flexibility, SQL allows statements to be “embedded” in a high-level procedural language. To overcome this and to provide more flexibility, SQL allows statements to be “embedded” in a high-level procedural language.

Two types of Programmatic SQL Embedded SQL statements Embedded SQL statements –SQL statements are embedded into the program source code and mixed with the host language statements. This allows users to write programs that access the database directly. A special precompiler modifies the source code to replace SQL statements with calls to DBMS routines. The source code can then be compiled and linked in the normal way. Application Programming Interface (API) Application Programming Interface (API) –No need for any precompilation. –Provides a cleaner interface and generates more manageable code.

2 Types of Embedded SQL Static SQL Static SQL –A complete embedded SQL statement. –Static SQL statements can be placed into stored procedures and can contain host variables. Dynamic SQL Dynamic SQL –With dynamic SQL statements, knowing the complete structure of an SQL statement before building the application is not necessary. Dynamic SQL statements allow run-time input to provide information about the database objects to query.

Simple SQL Statement CREATE TABLE Viewing (propertyNo VARCHAR2(5) NOT NULL, clientNo VARCHAR2(5) NOT NULL, clientNo VARCHAR2(5) NOT NULL, viewDate DATE NOT NULL, viewDate DATE NOT NULL, comments VARCHAR2(40)); comments VARCHAR2(40));

Static SQL /*Program to create the Viewing table*/ #include EXEC SQL INCLUDE sqlca; main() { EXEC SQL BEGIN DECLARE SECTION; char *username = “Manager/Manager”; char *connectString = “DreamHome”; EXEC SQL END DECLARE SECTION; /* Connect to database*/ EXEC SQL CONNECT :username USING :connectString; If(sqlca.sqlcode < 0) exit(-1); /* Display message for user and create the table */ printf (“Creating VIEWING table\n”); EXEC SQL CREATE TABLE Viewing (propertyNo VARCHAR2(5) NOT NULL, clientNo VARCHAR2(5) NOT NULL, viewDate DATENOT NULL, comments VARCHAR2(40)); if (sqlca.sqlcode >= 0)/* Check success */ printf (“Creation successful\n”); else printf (“Creation unsuccessful\n”); /* Commit the transaction and disconnect from the database */ EXEC SQL COMMIT WORK RELEASE; }

Using the SQLCA Data Structure The SQL Communication Area (SQLCA) is a data structure that traps and reports runtime errors to the Embedded SQL for C applications. The SQL Communication Area (SQLCA) is a data structure that traps and reports runtime errors to the Embedded SQL for C applications. The application checks the error fields and status indicators of the SQLCA data structure to determine the success or failure of an embedded SQL statement. The application checks the error fields and status indicators of the SQLCA data structure to determine the success or failure of an embedded SQL statement.

SQLCA /* Connect to database*/ EXEC SQL CONNECT :username USING :connectString; If(sqlca.sqlcode < 0) exit(-1); /* Display message for user and create the table */ printf (“Creating VIEWING table\n”); EXEC SQL CREATE TABLE Viewing (propertyNo VARCHAR2(5) NOT NULL, clientNo VARCHAR2(5) NOT NULL, viewDate DATENOT NULL, comments VARCHAR2(40)); if (sqlca.sqlcode >= 0)/* Check success */ -SQLCA.sqlcode = 0 successfully executed > 0 statement executed but with an exception < 0 error occurred – statement did not execute

Error Handling The WHENEVER statement The WHENEVER statement EXEC SQL WHENEVER EXEC SQL WHENEVER CONTINUE DO name (args) DO BREAK DO CONTINUE GOTO label STOPSQLERROR SQLWARNING SQLWARNING NOT FOUND NOT FOUND EXEC SQL WHENEVER SQLERROR GOTO error1; EXEC SQL WHENEVER SQLWARNING STOP;

The WHENEVER statement EXEC SQL WHENEVER SQLERROR GOTO error1; EXEC SQL INSERT INTO Viewing VALUES (‘CR76’, ‘PA14’, ’12-MAY- 2001’, ‘NOT ENOUGH SPACE’); EXEC SQL INSERT INTO Viewing VALUES (‘CR77’, ‘PA14’, ’12-MAY- 2001’, ‘QUITE LIKE IT’); HOW PRECOMPILER READS IT EXEC SQL WHENEVER SQLERROR GOTO error1; EXEC SQL INSERT INTO Viewing VALUES (‘CR76’, ‘PA14’, ’12-MAY- 2001’, ‘NOT ENOUGH SPACE’); if (sqlca.sqlcode < 0) goto error1; EXEC SQL INSERT INTO Viewing VALUES (‘CR77’, ‘PA14’, ’12-MAY- 2001’, ‘QUITE LIKE IT’); if (sqlca.sqlcode < 0) goto error1;

Host Language Variables - Program variable declared in the host language. - Can be used in embedded SQL statements - Can be used within the WHERE clause of SELECT statement - Cannot be used to represent database objects, such as table names or column names.

Host Language Variable - Must be declared to SQL as well as being declared in the syntax of the host language. - All host variables must be declared to SQL in this block, which must appear before any of the variables are used in an embedded SQL statement. EXEC SQL BEGIN DECLARE SECTION; float increment; EXEC SQL END DECLARE SECTION;

Host Language Variables To use a host variable in an embedded SQL statement, the variable name is prefixed by a colon. For example, EXEC SQL UPDATE Staff SET salary = salary + :increment WHERE staffNo = ‘SL12’; WHERE staffNo = ‘SL12’;

Retrieving data using embedded SQL EXEC SQL SELECT fName, lName, address EXEC SQL SELECT fName, lName, address INTO :firstName, :lastName, :address, INTO :firstName, :lastName, :address, FROM PrivateOwner WHERE ownerNo = ‘CO21’;

Difference between Static and Dynamic SQL Static SQL does not allow host variables to be used in place of table names or column names. For example, EXEC SQL BEGIN DECLARE SECTION; char Viewing; EXEC SQL END DECLARE SECTION; EXEC SQL INSERT INTO :Viewing VALUES (‘CR76’, ‘PA14’, ‘5-MAY-2001’, ‘Not enough space’); VALUES (‘CR76’, ‘PA14’, ‘5-MAY-2001’, ‘Not enough space’);

List of embedded SQL statements Declarative Statement EXEC SQL ARRAYLEN To use host arrays EXEC SQL BEGIN DECLARE SECTION EXEC SQL END DECLARE SECTION To declare host variables EXEC SQL DECLARE To name SQL objects EXEC SQL INCLUDE To copy in files EXEC SQL TYPE To equivalence datatypes EXEC SQL VAR To equivalence variables EXEC SQL WHENEVER To handle runtime errors

Executable Statements EXEC SQL ALLOCATE To define and control data EXEC SQL ALTER EXEC SQL ANALYZE EXEC SQL COMMENT EXEC SQL CONNECT EXEC SQL CREATE EXEC SQL DROP EXEC SQL DELETE To query and manipulate data EXEC SQL FETCH EXEC SQL INSERT EXEC SQL COMMIT To process transactions EXEC SQL ROLLBACK

Statements for Dynamic SQL EXEC SQL DESCRIBE To use dynamic SQL EXEC SQL EXECUTE EXEC SQL PREPARE

Running SQL Commands Any SQL command can be run from within an embedded SQL application. Below are some examples of how to do that. Creating a table: - EXEC SQL CREATE TABLE foo (number integer, ascii char(16)); - EXEC SQL CREATE UNIQUE INDEX num1 ON foo(number); - EXEC SQL COMMIT; Inserting rows: - EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad');

Running SQL Commands Deleting rows: - EXEC SQL DELETE FROM foo WHERE number = 9999; Single-row Select: - EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = ‘doodad’;

Running SQL Commands Selecting using Cursors: - EXEC SQL DECLARE foo_bar CURSOR FOR SELECT number, ascii FROM foo ORDER BY ascii; - EXEC SQL OPEN foo bar; - EXEC SQL FETCH foo_bar INTO :FooBar, DooDad; … - EXEC SQL CLOSE foo_bar; Updates: - EXEC SQL UPDATE foo SET ascii = ‘foobar’ WHERE number = 9999;

Bibliography - Connolly, Thomas, Carolyn Begg. “Database Systems: A practical approach to design, implementation, and management”. 3 rd ed forc/ec_6_epr_01_3m03.asp forc/ec_6_epr_01_3m03.asp forc/ec_6_epr_01_3m03.asp -