Download - SQL in higher level languages
![Page 1: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/1.jpg)
1
SQL in higher level languages
![Page 2: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/2.jpg)
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
![Page 3: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/3.jpg)
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
![Page 4: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/4.jpg)
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
![Page 5: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/5.jpg)
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)
![Page 6: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/6.jpg)
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
![Page 7: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/7.jpg)
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)
![Page 8: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/8.jpg)
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
![Page 9: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/9.jpg)
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.
![Page 10: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/10.jpg)
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
![Page 11: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/11.jpg)
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
![Page 12: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/12.jpg)
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)
![Page 13: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/13.jpg)
13
Error messages
• printing error messages - can extract the error message
• Available through SQLCA
![Page 14: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/14.jpg)
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.
![Page 15: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/15.jpg)
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.
![Page 16: SQL in higher level languages](https://reader035.vdocument.in/reader035/viewer/2022081204/5463c5d0af79590c328b6c8d/html5/thumbnails/16.jpg)
16
OLE, JDBC
• OLE methods are available in Oracle to do most of the above
• Can also use JDBC, independent of platform