1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 HW3 Oct. 2 to –Available Oct 2 only until 2 pm.

Slides:



Advertisements
Similar presentations
Oracle PL/SQL IV Exceptions Packages.
Advertisements

Programming Languages and Paradigms
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Embedded SQL John Ortiz. Lecture 15Embedded SQL2 Why Isn’t Interactive SQL Enough?  How to do this using interactive SQL?  Print a well-formatted transcript.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
1 Minggu 6, Pertemuan 11 Programmatic SQL Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Murali Mani SQL with other Programming Languages.
Introduction to Structured Query Language (SQL)
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
SPRING 2004CENG 3521 SQL: Constraints, Triggers, Embedded SQL Chapters: 5, 6.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Introduction to Structured Query Language (SQL)
Remaining Topics in SQL to be covered… NULL values in SQL Outer joins in SQL Constraints and Triggers in SQL Embedded SQL.
DB2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB01/003 Version No:2.0a Session Plan SPUFI Hands On Introduction to Embedded SQL DCLGEN.
Jump-Start Embedded SQL into RPG Presented by: Robert Arce.
Advanced Database CS-426 Week 2 – Logic Query Languages, Object Model.
SQL in higher level languages. 2 Why do we want to use SQL in a higher level language? 1)Read in data from file, manipulate data before insert into relation.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Agenda Journalling More Embedded SQL. Journalling.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
Cursor and Exception Handling By Nidhi Bhatnagar.
Overview of JDBC and Pro*C 1 CSE 5330 – Database Systems.
Converting To Anthony Tichonoff Florida Hospital MIS January 2007 With Ease Multi-Row Fetch.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Advanced SQL: Cursors & Stored Procedures
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
ICS 321 Fall 2009 DBMS Application Programming Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 10/06/20091Lipyeow.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
ICS 321 Fall 2010 SQL in a Server Environment (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 11/1/20101Lipyeow.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
SQL in a Programming Environment CIS 4301 Lecture Notes Lecture /11/2006.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
Pointers *, &, array similarities, functions, sizeof.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
Chapter 8 Embedded SQL.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Loops and Files. 5.1 The Increment and Decrement Operators.
In this module, we will design a program to work with the PROJECT table, learning to insert a new project into the table, to delete one or more projects.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
RETRIEVE A NO. OF ROWS ¦ Declare a cursor ¦ Open the cursor ¦ Fetch rows of data ¦ Stop fetching rows ¦ Close the cursor.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Application Development Chapter 6.
Introduction to SQL Programming Techniques
SQL Environment.
Introduction to Database Systems, CS420
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CPSC-310 Database Systems
Embedded DB2.
Unit I-2.
Chapter 8 Advanced SQL.
Presentation transcript:

1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 HW3 Oct. 2 to –Available Oct 2 only until 2 pm

2 CS Embedded SQL

3 To Embed SQL in C/C++ Why do we want to embed SQL in C/C++? 1) Read in data from file, insert into relation 2) Compute results based on result from query e.g. generate a report 3) Provide a user interface for SQL if the current one is lacking

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 - create SQL queries

5 Embedded SQL Select dnumber From department Where mgrssn = What is needed? –Variables in which to place result (Host variables) –distinguish SQL statements from C statements (EXEC SQL) –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 read in data from a file Want to: Loop until the EOF (Need to test this) read values from file – C/C++ code manipulate values with C/C++ code insert into relation values (SQL) End loop

7 Static SQL Embedded Select statement EXEC SQL Select [distinct] expr {, expr} into host_var {, host_var} From table_name [alias] {, table_name [alias]} [Where search_cond] [Group by col {, col}] [Having search_cond] EXEC SQL select lname, salary into :lname, :sal From employee Where ssn= ;

8 Static SQL For what type of query is static SQL appropriate? – Retrieves a single row – Union permitted in SQL-92, as long as returns 1 row –As we will see later, can retrieve multiple rows if use arrays in Oracle

9 Host variables (host_var) Referenced by SQL and C/C++ prefix with : in SQL statements transmit data between DB manager and application must declare in a Begin Declare Section

10 Begin Declare Section EXEC SQL Begin Declare Section; short age = 26; long dept; double salary; char ch; char name[9]; EXEC SQL End Declare Section;

11 Host variables Host variables are also used in the search_cond as numerical or char strings in an expression Where ssn = :hv1 Where salary > :hv2 CANNOT be col_names, tables or logical conditions in expression which require compiler attention at runtime after the value of the host variable value is set

12 ORACLE host variables See earlier slide (SQL) on Oracle and C/C++ types some conversions are possible Previously, in embedded SQL no arrays allowed except char Oracle 9i allows host variables to be arrays

13 Array interface to Oracle Oracle (Pro C/C++) lets you define array host variables (called host arrays) and arrays of structures and operate on them with a single SQL statement. Using the array SELECT, FETCH, DELETE, INSERT, and UPDATE statements, you can query and manipulate large volumes of data with ease. You can also use host arrays inside a host variable struct The maximum number of array elements in an SQL statement that is accessible in one fetch is 32K

14 Array interface to Oracle char emp_name[50][20]; int emp_number[50]; float salary[50]; EXEC SQL SELECT ENAME, EMPNO, SAL INTO :emp_name, :emp_number, :salary FROM EMP WHERE SAL > 1000;

15 Array interface to Oracle If there are < 50 rows, will work. If there are > 50 rows, it will not work. If you reissue the select statement, it will retrieve the first 50 rows again. –You must use a cursor in this case –Therefore, if you do not know the maximum number of rows a SELECT will return, you can declare and open a cursor, then fetch from it in "batches."

16 Cursors How to retrieve multiple rows for this query with embedded select without using arrays? Select essn, hours From WORKS_ON Where pno = :proj_id; Use a cursor –A cursor points to 1 row –A cursor is used instead of placing all of results in an array (may be too large)

17 Using cursors 3 steps involved: – 1) declare cursor - just a definition of the select – 2) open cursor - executes select, builds result table – 3) fetch results - to navigate through the results

18 EXEC SQL using cursors EXEC SQL declare cursor_name cursor for subselect; –When this statement is issued, select is executed: EXEC SQL open cursor_name; –Must place this statement in a loop to get each row in result: EXEC SQL fetch cursor_name into :hv1 {, :hv2} – Can close and open the cursor again with a different values for host vars: EXEC SQL close cursor_name;

19 Cursors EXEC SQL declare c1 cursor for Select essn, hours From WORKS_ON Where pno = :proj_id; EXEC SQL open c1; EXEC SQL fetch c1 into :essn_id, :hrs; while (sqlca.sqlcode == 0) // checks for EOT { cout << essn_id << “ “ << hrs << endl; EXEC SQL fetch c1 into :essn_id, :hrs; };

20 Cursor Cursor points to prior row, when it is opened, it points to position just before the first row As the cursor is incremented, the values of rows are retrieved into host vars Cursor is scrollable forward and backward

21 How does 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++)

22 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++

23 Pro*C We will use Pro C/C++ to precompile our.pc file which is C/C++ program containing embedded SQL Must precompile.pc file using Pro C/C++, or can access it through.NET Oracle Pro*C infoPro*C

24 What is needed in.NET to use Oracle? Must set up the environment –Add path for oracle executable files C:\Program Files\Oracle\Ora90\bin –Add path for Oracle include files C:\Program Files\Oracle\Ora90\precomp\public –Add path for Oracle library files C:\Program Files\Oracle\Ora90\precomp\lib\msvc – Add orasql9.lib for Linker

25 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; Sample program How How to set up.NET in our lab to use Pro C

26 C program and DBMS How does the loop terminate? With a test of the SQLCA structure field to terminate loop will discuss this later

27 Test Oct. 9 – bring 1 sheet of paper with notes (use both sides)

28 Industrial Colloquium Series speaker Jim Azar, UA graduate in '83 Topic: "What does it take to be successful in the software development industry" Tuesday night at 5pm HO119

29 Embedded SQL Delete EXEC SQL delete from table_name [alias] [Where search_cond]; If no where clause, all rows are deleted If there is a where, a search-delete is performed

30 Positioned Delete using cursor EXEC SQL delete from table_name [alias] Where current of cursor_name; –Deletes row cursor pointing to, then cursor moves to just before next row –Useful if want to print information before it is deleted –If an empty table, not found occurs with next fetch – Need to have the fetch statement at the beginning of a loop, before the delete

31 Delete using cursor –Must indicate will make changes to the table when cursor is declared. The following precedes a delete statement: EXEC SQL declare cursor_name cursor for subselect {Union subselect} [Order by | For update of ]; –Only 1 table can be specified in the cursor subselect for delete (or update)

32 Positioned delete example EXEC SQL DELETE FROM emp WHERE deptno = :deptno AND job = :job; EXEC SQL DECLARE c3 CURSOR FOR SELECT empno, comm FROM emp FOR UPDATE OF emp; // This is optional in Oracle EXEC SQL OPEN c3; EXEC SQL FETCH c3 INTO :emp_number, :commission; cout << emp_number << commission << endl; EXEC SQL DELETE FROM emp WHERE CURRENT OF c3;

33 Delete Deletions are made to the base table The for update of indicates will make changes to base table Standard requires specified if delete (or update) – optional in Oracle Can choose both update of and order by

34 Update Updates to base table: search-update EXEC SQL update table_name [alias] set col = expr {, col = expr} [Where search_cond];

35 Positioned-update using cursor Again must indicate will make changes to table, and can only have 1 table EXEC SQL declare cursor_name cursor for subselect {Union subselect} [Order by | For update of col {, col}]; positioned-update EXEC SQL update table_name [alias] set col = expr {, col = expr] Where current of cursor_name; (cursor must be pointing to a valid row)

36 Insert No need for cursor, can't specify position of new row EXEC SQL insert into table_name [ (col {, col})] values (:hv1 {, :hv2}); Also available: EXEC SQL Create table Drop table

37 SQLCA SQL communication area - a structure used for communication between DBS monitor and C++ program EXEC SQL include SQLCA allocates program space for errors and starts communication by DBS monitor

38 SQLCA after each SQL statement executed, a new value is placed in SQLCA indicates if successful, EOF, etc. error or warning conditions

39 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) use sqlwarn for further info sqlerrd[2] - indicates number of row affected by insert, update or delete (used for referential integrity)

40 Whenever Can test sqlcode directly or use whenever EXEC SQL whenever condition action; condition can be: – sqlerror - tests of sqlcode < 0 – not found - tests when no rows affected - good for EORows in ORACLE –sqlwarning - tests if sqlcode > 0, other than not found

41 Whenever action can be: –do fnCall - do procedure call –stop - cannot be specified for not found –goto label - must be in the scope of subsequent EXEC SQL statements, preprocessor substitutes condition with the goto label –continue - no action taken unless fatal error, such as "fails to connect"

42 Whenever Example: EXEC SQL whenever sqlerror stop; precompiler implements whenever by inserting tests after every subsequent run-time DB system call does not follow the flow of control - just physical location of whenever statement Must be careful not to set up infinite loops If no whenever - default is to continue processing, even if error, etc.

43 Error messages printing error messages in ORACLE - can extract the error message The following can be defined by the user: int report_error () { cout << "error occurred" << endl; sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml]= '\0'; cout << sqlca.sqlerrm.sqlerrmc << endl; return -1; } EXEC SQL whenever sqlerror do report_error();

44 Example EXEC SQL fetch c1 in :hv1; while (sqlca.sqlcode == 0) { cout << hv1 << endl; EXEC SQL fetch c1 into :hv1; }

45 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

46 Dynamic SQL Different types of dynamic SQL statements: – execute immediate – prepare – execute – cursors

47 Dynamic SQL To execute a dynamic SQL statement, must place a copy of an SQL query into a char string variable char stmt1[80] = " "; // in Declare Section strcpy (stmt1, "Delete From employee where ssn = "); or: cin >> stmt1;

48 Execute immediate Execute immediate statement EXEC SQL Execute immediate :stmt; where stmt contains an SQL statement as a char string EXEC SQL Execute immediate :stmt1; Prepares and executes an SQL statement that does not use any host variables

49 Execute immediate Can NOT be used for any select statements Can use it for update, drop, create index, create table, create view, etc.

50 Execute immediate Execute immediate statement is precompiled When it is executed at run time: 1.executable SQL stmt constructed from SQL char string text 2.executable form of SQL statement is processed 3.executable form of SQL statement is destroyed Recompiles a new SQL statement each time execute this statement

51 Prepare and Execute Delete From employee where ? > ? Theoretically, a dynamic SQL statement is not supposed to contain host variables – instead called parameter markers. However, as we shall see, Oracle includes what look like host variables.

52 Prepare and Execute We need to identify the variable that we will obtain later In some systems use ? as a parameter marker,. However, in ORACLE, use : and any name desired for a parameter marker, end result is it looks like a host variable An SQL statement can contain more than one parameter marker

53 Prepare Prepare stmt_name [into :sqlda] from :stmt; Prepare turns a character string form of SQL into an executable form and associates a name other statements can reference from indicates the name of the host variable containing the SQL string –same types of SQL statements as allowed for execute immediate, only statement can contain host variables –the statement cannot be a select into – must use fetch instead Create a char variable to hold the SQL statement. char str[80] = " "; // in Declare Section strcpy (str, "Delete From employee where dno=?"); // Oracle uses a : instead of a ? OR cin >> str;

54 Prepare EXEC SQL Prepare query1 from :str; where str is the host variable that contains the char string, i.e. give the query the name ‘query1’

55 Execute EXEC SQL execute stmt_name using :hv1 {, :hv2}; 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 dnum = 5; EXEC SQL execute query1 using :dnum;

56 Example with Oracle Using ORACLE, Can specify a name for any host variable in statement char st[80] = " "; // in Declare Section strcpy (st, "Delete From employee where"); cin >> field; strappend (st, field); strappend (st, "> :val"); //Resulting query is: Delete From employee where salary > :val

57 Execute For example prompt user for value of salary and execute the statement cin >> sal_val; EXEC SQL Execute stmt1 using :sal_val;

58 Parameter Markers For every parameter marker, must provide a host variable with data type compatibility The host variable is used to replace the parameter marker in the string

59 Prepare and execute Prepare and execute gives better performance than execute immediate if repeatedly execute the same statement

60 Cursors - Dynamic Select So what happens if you want to Select? Processing a cursor dynamically is nearly identical to using static SQL In static SQL, the select statement is given In dynamic SQL, query is associated with a statement name assigned in the prepare statement

61 Cursors – Dynamic Select Even if only one row will be retrieved from the select statement, must define a cursor for dynamic Select dynamic cursor prepared at run time In Oracle, can use arrays to fetch rows into

62 Dynamic select For dynamic select: 1. Declare host variables 2. Prepare statement 3. Declare cursor 4. Open cursor 5. Fetch data 6. Close cursor

63 EXEC SQL prepare stmt_name [into :sqlda] from :sqlstrr; where sqlstr is a host variable that is a char string with a valid SQL statement Use into :sqlda if don't know before, number of column values to be retrieved before compilation DBMS places a value into SQLDA field of SQLDA structure indicating the number of columns in result table generated by select statement

64 EXEC SQL declare cursor_name for stmt_name; EXEC SQL open cursor_name [using :hostvar {, hostvars}]; the hostvars are optional, only if there is a parameter marker while { EXEC SQL fetch cursor_name into :hv1 {, hvn}; }

65 Example strcpy (pstring, "Select name from students where name <> :name); cin >> sname; EXEC SQL prepare s1 from :pstring; EXEC SQL declare c1 cursor for s1; EXEC SQL open c1 using :sname; EXEC SQL fetch c1 into :hv1; while (sqlca.sqlcode == 0) { cout << hv1 << endl; EXEC SQL fetch c1 into :hv1; }

66 Oracle example using arrays strcpy (pstring, "Select ssn from employee where dno <> :dno"); cin >> dnum; EXEC SQL prepare s1 from :pstring; EXEC SQL declare c5 cursor for s1; EXEC SQL open c5 using :dnum; EXEC SQL fetch c5 into :hv1; //hv1 is an array Num_retd = sqlca.sqlerrd[2]; for (i=0; i< Num_retd; i++) { cout << hv1[i] << endl; }

67 OLE OLE methods are available in Oracle to do most of the above – will discuss later