1 no class thurs. oct. 2 e-day exam 1 thurs. oct. 9 e-mail hw3 oct. 2 to...

67
1 • No class Thurs. Oct. 2 E-day • Exam 1 Thurs. Oct. 9 • E-mail HW3 Oct. 2 to [email protected] – Available Oct 2 only until 2 pm

Upload: erika-shields

Post on 12-Jan-2016

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

1

• No class Thurs. Oct. 2 E-day

• Exam 1 Thurs. Oct. 9

• E-mail HW3 Oct. 2 to [email protected]– Available Oct 2 only until 2 pm

Page 2: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

2

 

CS 457 - Embedded SQL

Page 3: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 4: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 5: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

5

Embedded SQL

Select dnumber From department Where mgrssn = 987654321

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)

Page 6: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 7: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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=123456789;

Page 8: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 9: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 10: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;

Page 11: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

 

Page 12: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 13: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 14: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;

Page 15: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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."

Page 16: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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)

Page 17: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 18: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;

Page 19: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;          

  };

Page 20: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 21: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 22: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 23: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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 info

Page 24: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 25: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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 programHow to set up .NET in our lab to use Pro C

Page 26: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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 

Page 27: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

27

• Test Oct. 9– bring 1 sheet of paper with notes (use both

sides)

Page 28: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 29: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 30: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 31: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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)

Page 32: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

32

Positioned delete exampleEXEC SQL DELETE FROM emp WHERE deptno = :deptno AND job = :job;

EXEC SQL DECLARE c3 CURSOR FOR SELECT empno, comm FROM empFOR 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;

Page 33: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

       

       

Page 34: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

34

Update

Updates to base table:

• search-update EXEC SQL update table_name [alias]

set col = expr {, col = expr}

[Where search_cond];

Page 35: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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)

Page 36: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 37: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 38: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

38

SQLCA

• after each SQL statement executed, a new value is placed in SQLCA

• indicates if successful, EOF, etc.

• error or warning conditions

Page 39: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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)

Page 40: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 41: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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"

Page 42: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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.

Page 43: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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();

 

Page 44: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

44

Example

EXEC SQL fetch c1 in :hv1;

while (sqlca.sqlcode == 0) {

cout << hv1 << endl;

EXEC SQL fetch c1 into :hv1;

}

Page 45: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 46: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

46

Dynamic SQL

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

Page 47: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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 = 987654321");

or: cin >> stmt1;

Page 48: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 49: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

49

Execute immediate

• Can NOT be used for any select statements

• Can use it for update, drop, create index, create table, create view, etc.

Page 50: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

 

Page 51: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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.

Page 52: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 53: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

53

PreparePrepare 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 Sectionstrcpy (str, "Delete From employee where dno=?");

// Oracle uses a : instead of a ? OR cin >> str;

Page 54: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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’

Page 55: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;

Page 56: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 57: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

57

Execute

• For example prompt user for value of salary and execute the statement

               

    cin >> sal_val;

    EXEC SQL Execute stmt1 using :sal_val;

Page 58: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 59: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

59

Prepare and execute

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

Page 60: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 61: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 62: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 63: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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

Page 64: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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}; } 

Page 65: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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;

}

Page 66: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

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; }

Page 67: 1 No class Thurs. Oct. 2 E-day Exam 1 Thurs. Oct. 9 E-mail HW3 Oct. 2 to vrbsky@cs.ua.eduvrbsky@cs.ua.edu –Available Oct 2 only until 2 pm

67

OLE

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