Your SlideShare is downloading. ×
SQL in Higher Level Languages
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

SQL in Higher Level Languages


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1.  
    • SQL in higher level languages
  • 2.  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 code
    • insert into relation values (SQL)
    • End loop
  • 3. 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
  • 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. Must have:
        • 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. With what?
    • Can use:
      • 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
    • transmit data between DB manager and application
    • Used in static SQL
      • 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 sex = ?”
    • 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
      • 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)
    • 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. Execute Immediate
    • Prepares and executes an SQL statement that does not use any host variables
    • Can NOT be used for any select statements
    • Can use it for update, drop, create index, create table, create view, etc.
  • 16. Execute Immediate
    • Execute immediate statement is precompiled
    • When it is executed at run time:
      • executable SQL stmt constructed from SQL char string text
      • executable form of SQL statement is processed
      • executable form of SQL statement is destroyed
    • Recompiles a new SQL statement each time execute this statement
  • 17. Prepare and Execute
    • executes previously prepared statement that has parameter markers (host variables)
    • can execute a statement more than once
    • this statement only makes sense for updates, delete, etc. because are not using the 'into'
    • a select statement here makes no sense, cannot print results
    • Prepare and execute gives better performance than execute immediate if repeatedly execute the same statement
  • 18. Embedded Static vs. dynamic SQL
    • Static SQL
      • Embed SQL statements directly into host level language
      • EXEC SQL select lname, salary
      • into :lname, :sal
      • From employee
      • Where ssn=123456789;
  • 19. Embedded Static vs. dynamic SQL
    • Dynamic SQL
      • Place SQL query into character string
    • char st[80] = &quot; &quot;; // in Declare Section
    • strcpy (st, &quot;Delete From employee where&quot;);
    • cin >> field;
    • strappend (st, field);
    • strappend (st, &quot;> :val&quot;);
    • //Resulting query is:
    • // “Delete From employee where salary > :val”
    • EXEC SQL Execute immediate st;
    • Sample program
  • 20. Embedded SQL work
    • Since it is a C/C++ program, must be able to compile
    • Hence, must precompile to identify SQL statements
    • SQL statements are replaced with calls to SQLLIB routines (API calls to data manager that are recognized by C/C++)
  • 21. Precompiler for Oracle
    • start with a source file: fn.pc
    • Precompiler generates file: fn.c/fn.cpp
      • (internal representation of SQL statements that were replaced by calls to SQLLIB routines- orasql9.lib)
      • if examine fn.c/fn.cpp can see all the SQLLib calls
    • Then you compile your program using C/C++
  • 22. What is needed in .NET to use Oracle?
    • Must set up the environment
      • Add path for oracle executable files
        • C:Program FilesOracleOra90in
      • Add path for Oracle include files
        • C:Program FilesOracleOra90precomppublic
      • Add path for Oracle library files
        • C:Program FilesOracleOra90precomplibmsvc
      • Add orasql9.lib for Linker
  • 23. Connect
    • must connect to DBMS
    • Include the following in C/C++ program
    • EXEC SQL connect :user_name identified by :user_pwd using :host_string;
    • EXEC SQL disconnect;
    • How to set up .NET in our lab to use Pro C
  • 24. OLE, JDBC
    • OLE methods are available in Oracle to do most of the above
    • Can also use JDBC, independent of platform