sql in higher level languages
DESCRIPTION
TRANSCRIPT
1
SQL in higher level languages
2
SQL in higher level languages
Why do we want to use SQL in a higher level language?
1) Read in data from file, insert into relation
Loop until the EOF read values from file – higher level
code manipulate values with higher level
code insert into relation values (SQL)
End loop
3
Why cont’d
2) Compute results based on result from query e.g. generate a report
Query databaseCompute results from queryPrint results
3) Provide a user interface for SQL if the current
one is lackingPrompt user for querySend query to DBMSReceive resultsDisplay results to user
4
Must have:
to do 1) must read in values into C variables then use those values to insert using SQL
still need SQL statement to insert, select tuples
to do 2) must be able to manipulate results from SQL query, but mismatch between C and SQL
sets versus one record at a time
to do 3) need to accept queries from user - create SQL queries
5
Given the query: Select dnumber
From department Where mgrssn = 987654321
What is needed?– Variables in which to place result (Host variables)– Processing of result table (cursors)– Data structure for communicating with DBS in case of
errors (SQLCA)– What if we want to process any query typed in?
(Dynamic SQL)
6
To do this
Can use: 1. embedded SQL
Precede each statement with EXEC SQL
2. Platform specific classes, interfaces
Oracle’s OLE
3. Platform independent classes, interfaces
JDBC
7
Host variables :
• Referenced by SQL and higher level language• Prefixed with : in SQL statements• transmit data between DB manager and application• Allows value of host variable to change if SQL executed >
1
select ssn from employee where salary < :min
insert into project values (:projectName, :projectNumber, :projectLocation, :departmentNumber)
8
Parameter markers ?
Can pass query as character string (dynamic SQL) “update employee
set salary = salary*1.1 where dno = ? and bdate = ?”
• We need to identify the variable that we will obtain later
• Depending on system, use ? or : as a parameter marker
• An SQL statement can contain more than one parameter marker
9
Cursors
• How to access multiple rows from a query result?
• Use a cursor – A cursor points to 1 row– Can move forward, backwards, etc.
10
Using cursors
• 3 steps involved:– 1) declare cursor - just a definition of the
select– 2) open cursor - executes select, builds
result table• Declare/open can be as one, e.g. OLE
– 3) fetch results - to navigate through the results
11
SQLCA
• SQL communication area - a structure• used for communication between DBS
monitor and C++ program• allocates program space for errors and
starts communication by DBS monitor• after each SQL statement executed, a new
value is placed in SQLCA• indicates if successful, EOF, etc.• error or warning conditions
12
SQLCA
• sqlca.sqlcode - testing code part of structure– sqlcode = 0 successful sql call– < 0 error– > 0 warning - call successful but some
condition existed
e.g. EOF is 100 (DB2, Ingres, but not ORACLE)
• e.g. in Embedded SQL, sqlerrd[2] - indicates number of row affected by insert, update or delete (used for referential integrity)
13
Error messages
• printing error messages - can extract the error message
• Available through SQLCA
14
Updating
• Delete– Delete tuples from base table– Positioned delete, use cursor
• Updates– Makes changes to base table– Positioned delete, use cursor
• Insert– No need for cursor, can't specify position of new row
• DDL– Can create tables, etc.
15
Dynamic SQL
• Dynamic SQL– Useful when:– Format of SQL statement is not known - can
generate during execution– Statement known but objects referenced
don't exist at compile time
• Used by OLE, JDBC, etc.
16
OLE, JDBC
• OLE methods are available in Oracle to do most of the above
• Can also use JDBC, independent of platform