SQL in higher level languages
Upcoming SlideShare
Loading in...5

SQL in higher level languages






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

SQL in higher level languages SQL in higher level languages Presentation Transcript

    • SQL in higher level languages
  •   SQL in higher level languages
    • Why do we want to use SQL in a higher level language?
    • 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
  • Why cont’d
    • Compute results based on result from query e.g. generate a report
      • Query database
      • Compute results from query
      • Print results
    • Provide a user interface for SQL if the current one is lacking
      • Prompt user for query
      • Send query to DBMS
      • Receive results
      • Display results to user
  • 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
        • 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)
  • To do this
    • Can use:
      • embedded SQL
    • Precede each statement with EXEC SQL
    • 2. Platform specific classes, interfaces
    • Oracle’s OLE
    • 3. Platform independent classes, interfaces
    • JDBC
  • 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)
  • 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
  • Cursors 
    • How to access multiple rows from a query result?
    •    Use a cursor 
      • A cursor points to 1 row
      • Can move forward, backwards, etc.
  • 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
    • 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
    • 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)
  • Error messages
    • printing error messages - can extract the error message
    • Available through SQLCA
  • 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.
  • 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.
    • OLE methods are available in Oracle to do most of the above
    • Can also use JDBC, independent of platform