SQL Apis
SQL Apis
and PL/SQL
Juliana Freire
• Language constructs:
– Connecting to a database:
– Declaring variables:
– Statements:
Declaring Variables
Executing Statements
Error/Status Reporting
• Each SQL statement executed returns a status
code + additional information
– SQL communication area (SQLCA)
• Two special “error” variables:
input variable
input variable
output
variables
FETCH Statement
• What happens if updates are applied to table(s)
referenced in a cursor after a result is fetched
from a cursor?
• Important note [Chamberlin, A Complete Guide
to DB2]: The result of a query associated with a
cursor may be
– Completely materialized when the 1st row is fetched,
or
– Materialized one row at a time
• Choice depends on the access plan chose by
the optimizer
Updates Through Cursors
Can update tuples fetched by cursor by declaring that the cursor
is for update"
declare c cursor for
select *
from account
where branch-name = ʻPerryridgeʼ
for update"
To update tuple at the current location of cursor"
update account
set balance = balance + 100
where current of c#
Apart from their method of finding the row to be updated/deleted,
positioned updates behave exactly the same as UPDATE and
DELETE statements that contain a search condition#
Dynamic SQL
• Sometimes, we can’t predict in advance what SQL
statements will be needed
• Dynamic SQL allows construction of SQL statements
on-the-fly
– Need to PREPARE/EXECUTE
– Example:
JDBC
API
Java
application Driver DB2
XSB-Oracle Interface
• Allow calls to Oracle from Prolog
• XSB-specific calls:
db_open(oracle(Name, Pass))
write('% Connected to Oracle as '),
write(Name), writeln(' ...'),nl,
db_sql('drop table DEPT'),
db_create_table('DEPT', 'DEPTNO NUMBER(2),DNAME
VARCHAR2(14)'), db_import(‘DEPT'(‘DEPTNO',‘DNAME'),
deptall),
db_insert(dept_put(A1,A2),(deptall(A1,A2))),
dept_put(1,’computer science’),
dept_put(1,’biology’),…
Stored Procedures/Functions
• Issues with accessing DB from remote
application:
– JDBC incurs data transfer overheads
– DB resources are tied up, e.g., open cursors
• Advantageous to execute some of the
application logic inside the database
– Minimize data transfer
– Utilize full power of database server
• Stored procedure is a program executed within
the process space of the database
– Can be run by multiple users
Oracle’s PL/SQL
• Oracle's procedural extension SQL.
– Combines the data manipulating power of SQL with
the data processing power of procedural languages.
• Stored objects
– Procedures, functions packages
• Some uses:
– External procedures: PL/SQL code invoked from
other languages
• E.g., using special tags, you can embed PL/SQL scripts into
HTML source code
– Triggers: procedures invoked by insert, delete,
update
Procedures In Oracle’s PL/SQL
SQL> create or replace procedure sum_salary
2 is
3 cursor c1 is
4 select * from employee;
5 sal_sum integer;
6 begin
7 sal_sum := 0;
8 for emp_rec in c1 loop
9 sal_sum := sal_sum + emp_rec.salary;
10 end loop;
11 dbms_output.put_line('Salary sum: ' || sal_sum);
12 end;
13 /
Procedure created.
Function created.
END debit_account;
body
PL/SQL: Procedures
Procedure
SQL> create or replace procedure sum_salary is stored in DB
3 cursor c1 is
4 select * from employee;
declaration
5 sal_sum integer;
6 begin
7 sal_sum := 0; execution
8 for emp_rec in c1 loop
9 sal_sum := sal_sum + emp_rec.salary;
10 end loop;
11 dbms_output.put_line('Salary sum: ' || sal_sum);
12 end;
13 /
Procedure created.
Function created.
46
Making a Connection
import java.sql.*;
Class.forName(com.mysql.jdbc.Driver);
Connection myCon =
DriverManager.getConnection(…);
The driver
Loaded by URL of the database
for mySql;
forName your name, and password
others exist
go here.
47
Statements
• JDBC provides two classes:
1. Statement = an object that can accept a string that
is a SQL statement and can execute such a string.
2. PreparedStatement = an object that has an
associated SQL statement ready to execute.
48
Creating Statements
• stat1 is a Statement.
• We can use it to insert a tuple as:
stat1.executeUpdate(
”INSERT INTO Sells ” +
”VALUES(’Brass Rail’,’Bud’,3.00)”
);
Example: Query
• stat2 is a PreparedStatement holding the query
”SELECT beer, price FROM Sells WHERE bar
= ’Joe’’s Bar’ ”.
• executeQuery returns an object of class
ResultSet – we’ll examine it later.
• The query:
ResultSet menu = stat2.executeQuery();
Accessing the ResultSet
• An object of type ResultSet is something like a
cursor.
• Method next() advances the “cursor” to the next
tuple.
– The first time next() is applied, it gets the first tuple.
– If there are no more tuples, next() returns the value
false.
Accessing Components of Tuples