Embedded SQL - unix.eng.ua.edu

advertisement

• No class Thurs. Oct. 2 E-day

• Exam 1 Thurs. Oct. 9

• E-mail HW3 Oct. 2 to vrbsky@cs.ua.edu

– Available Oct 2 only until 2 pm

1

CS 457 - Embedded SQL

2

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

3

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

4

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)

5

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

6

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;

7

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

8

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

9

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;

10

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

11

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

12

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

13

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;

14

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

15

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)

16

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

17

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;

18

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;

};

19

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

20

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

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

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

23

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

24

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

25

C program and DBMS

• How does the loop terminate?

• With a test of the SQLCA structure field to terminate loop will discuss this later

26

• Test Oct. 9

– bring 1 sheet of paper with notes (use both sides)

27

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

28

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

29

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

30

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)

31

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;

32

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

33

Update

Updates to base table:

• search-update

EXEC SQL update table_name [alias] set col = expr {, col = expr}

[Where search_cond];

34

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)

35

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

36

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

37

SQLCA

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

• indicates if successful, EOF, etc.

• error or warning conditions

38

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)

39

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

40

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"

41

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.

42

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

43

Example

}

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

EXEC SQL fetch c1 into :hv1;

44

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

45

Dynamic SQL

• Different types of dynamic SQL statements:

– execute immediate

– prepare

– execute

– cursors

46

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;

47

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

48

Execute immediate

• Can NOT be used for any select statements

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

49

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

50

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.

51

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

52

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;

53

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’

54

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;

55

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

56

Execute

• For example prompt user for value of salary and execute the statement cin >> sal_val;

EXEC SQL Execute stmt1 using :sal_val;

57

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

58

Prepare and execute

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

59

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

60

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

61

Dynamic select

• For dynamic select:

1. Declare host variables

2. Prepare statement

3. Declare cursor

4. Open cursor

5. Fetch data

6. Close cursor

62

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

63

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

64

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;

}

65

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;

}

66

OLE

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

67

Download