Download Programmatic SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Extensible Storage Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Database wikipedia , lookup

DBase wikipedia , lookup

Oracle Database wikipedia , lookup

Ingres (database) wikipedia , lookup

Team Foundation Server wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft Access wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Btrieve wikipedia , lookup

Tandem Computers wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Null (SQL) wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

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.
• 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
– 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)
– No need for any precompilation.
– Provides a cleaner interface and generates more manageable
code.
2 Types of Embedded SQL
• Static SQL
– A complete embedded SQL statement.
– Static SQL statements can be placed into stored
procedures and can contain host variables.
• 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,
viewDate DATE
NOT NULL,
comments VARCHAR2(40));
Static SQL
/*Program to create the Viewing table*/
#include <stdio.h>
#include <stdlib.h>
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 DATE
NOT 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 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 DATE
NOT 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
EXEC SQL WHENEVER <condition> <action>
SQLERROR
SQLWARNING
NOT FOUND
CONTINUE
DO name (args)
DO BREAK
DO CONTINUE
GOTO label
STOP
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-MAY2001’, ‘NOT ENOUGH SPACE’);
EXEC SQL INSERT INTO
2001’, ‘QUITE LIKE IT’);
Viewing
VALUES
(‘CR77’, ‘PA14’, ’12-MAY-
-------------HOW PRECOMPILER READS IT-----------EXEC SQL WHENEVER SQLERROR GOTO error1;
EXEC SQL INSERT INTO Viewing VALUES (‘CR76’, ‘PA14’, ’12-MAY2001’, ‘NOT ENOUGH SPACE’);
if (sqlca.sqlcode < 0) goto error1;
EXEC SQL INSERT INTO
Viewing
2001’, ‘QUITE LIKE IT’);
if (sqlca.sqlcode < 0) goto error1;
VALUES
(‘CR77’, ‘PA14’, ’12-MAY-
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’;
Retrieving data using embedded
SQL
EXEC SQL SELECT fName, lName, 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’);
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
EXEC SQL ALTER
EXEC SQL ANALYZE
EXEC SQL COMMENT
To define and control data
EXEC SQL CONNECT
EXEC SQL CREATE
EXEC SQL DROP
EXEC SQL DELETE
EXEC SQL FETCH
To query and manipulate data
EXEC SQL INSERT
EXEC SQL COMMIT
EXEC SQL ROLLBACK
To process transactions
Statements for Dynamic SQL
EXEC SQL DESCRIBE
EXEC SQL EXECUTE
EXEC SQL PREPARE
To use dynamic SQL
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”. 3rd
ed. 2002.
- http://msdn.microsoft.com/library/default.asp?url=/library/enus/esql
forc/ec_6_epr_01_3m03.asp
- http://www-db.stanford.edu/~ullman/fcdb/oracle/or-proc.html#sql