• Transalate SQL queries into appropriate programming language code automatically • Handle errors and warnings – using the SQLCA SQL Communication Area – Explained further • Separate pr
Trang 1Programming Using Pro *C
Oracle Day 4
Trang 2Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Objectives
Introduction to Oracle Pre-compilers
Embedded SQL
Pro *C
Trang 3Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 4Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 5Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Advantages of Pre-compilers
• Write applications using High level programming language
• Embed SQL in HLL programs.
• Automatically convert datatypes
– between those supported by Oracle and the Programming Language.
• Transalate SQL queries into appropriate programming language code automatically
• Handle errors and warnings
– using the SQLCA (SQL Communication Area) – Explained further
• Separate pre-compilation of the program modules and then linking them together
Trang 6Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 7Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Embedded SQL Program Dev – The big picture
ORACLE Run time Library(SQLLIB)
Resolve calls SQL statements replaced
by Library calls
Trang 8Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Embedded SQL- Some Basic Concepts
• Embedded SQL statements
– Executable
– Declarative
• Host, Indicator and pointer variables
• Context areas, cursors and active sets
• Transactions
• Errors and Warnings
Trang 9Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Executable Statements
• Result in calls and return codes to Oracle
• They are used to connect to the ORACLE database, define, query, manipulate and control access to the Oracle Database
• Example:
– EXEC SQL insert into branch values(:brcode,:br_location,:br_mgr);
Trang 10Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Declarative Statements
• All statements that allow you to declare variables(host variables) used in SQL statements , Communication areas and Oracle objects
• Following keywords are used for the respective job:
• DECLARE for Oracle objects
• INCLUDE for communication areas
• WHENEVER for error handling
• The Declare section statements are needed when the precompiler
MODE=ANSI, if MODE=ORACLE then you may omit the Declare section
statements
Trang 11Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Example: Using DECLARE
EXEC SQL BEGIN DECLARE SECTION;
Trang 12Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Example: Using INCLUDE
EXEC SQL INCLUDE sqlca;
• SQLCA is the SQL communication area between Oracle and your Pro *C program (Explained in further slides)
• SQLCA is a structure (Structure is given in the appendix slides)
Trang 13Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Host variables
• Host variables allow communication between Oracle and your program
• Host variables must be declared using the host language data types and rules.
– Must be prefixed with a colon in SQL statements
– Data types must be supported by host language
• A host variable must not be
– Used as an array subscript
– be prefixed with colon in host language statements
Trang 14Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 15Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Indicator Variables
• Every host variable can be associated with an indicator variable
• Used for
– assigning null values to input host variables
– Detect null or truncated values in output host variables
• They must be
– declared in the declare section
– Prefixed with a colon(:) in the SQL statement
• They cannot be
– used in the WHERE clause of an SQL statement
Trang 16Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Indicator variables
• Indicator variables can be used to monitor host variables as follows:
• For input host variables, indicator variables with a
value 1 Oracle assigns NULL value to the column
>=0 Oracle assigns value of host variables
• For output host variables, Oracle assigns the indicator variables with a value
1 column value is NULL
0 Oracle assigns column value to host
>0 Oracle assigns truncated value to host
Trang 17Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Example: Using Host and Indicator variables
main(){
printf(“Enter the employee number”);
scanf(“%d”,&emp_no);
// usage of host and indicator variables
EXEC SQL select name,ytdsales into :emp_name , :emp_sales:ind_sales from salesrep where empno = :empno;
Trang 18Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
• WHENEVER statement is used to do automatic checking and error handling
Trang 19Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
WHENEVER - Actions
– Continue with next statement if possible
• DO {<function>|break}
– Control transferred to function, at end of routine - control returns to
statement following the failed SQL statement
– break : Will break from the loop in which the failed SQL statement is present and transfer control to statement following loop
Trang 20Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
EXEC SQL OPEN emp_cursor;
EXEC SQL WHENEVER NOT FOUND DO break;
WHILE(1)
{EXEC SQL FETCH EMP_CURSOR INTO :EMP_NUMBER,:EMP_NAME;
}EXEC SQL CLOSE EMP_CURSOR;
}
Trang 21Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Summary
• The Pro *C pre-compiler
• SQLCA
• Host and Indicator variables
• Using embedded SQL statements
• Handling errors
• Using embedded PL/SQL
Trang 22Appendix A – More about Pro C
Trang 23Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
EXEC SQL END DECLARE SECTION;
• When using in SQL statements you must prefix the pointer variable with a
colon(:)
EXEC SQL SELECT intcol INTO :int_ptr FROM
Trang 24Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
EXEC SQL END DECLARE SECTION;
•Likewise a collection of indicator variables is an indicator array.
Trang 25Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Host arrays
• Host array of pointers are not allowed.
• Multidimensional host arrays are also not allowed with the exception of char data type
• When used in SQL statements host arrays must not be subscripted.
Demo Host Arrays
Trang 26Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
unsigned short len;
unsigned char arr[20];
} username;
Trang 27Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
VARCHAR variables (contd.)
• Before passing an input VARCHAR variable to ORACLE, always assign the length member to a valid length
• Example:
varchar userName[20];
…
strcpy((char *)userName.arr, “dummy”);
userName.len = strlen((char *)userName.arr);
Trang 28Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
• After obtaining a value into an Output host variable of type VARCHAR, always terminate the variable value with string terminator(‘\0’)
Trang 29Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Referencing VARCHAR variables
• In SQL statements: struct name preceded by colon
• In C statements: struct name.member name
Trang 30Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Context Areas,Cursors & Active sets
• Work area where SQL statements are processed are called context areas/Private SQL Area
• Cursors are defined to process multiple rows.
– Implicit Cursor: Implicitly declared by ORACLE for all DDL and DML statements
– Explicit Cursor: Declared by programmer
• The returned set of rows is called the active set
Trang 31Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Example-Using Cursors
• The cursor control statements DECLARE, OPEN, FETCH and CLOSE allow a user to manipulate the contents of the cursor
Demo Cursors
Trang 32Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Transactions
• Transactions are a set of logically related SQL statements
• All statements executed between a COMMIT and ROLLBACK comprise a transaction
• Complete execution of a DDL statement is also treated as one complete transaction
• Example:
EXEC SQL COMMIT WORK RELEASE;
EXEC SQL ROLLBACK WORK RELEASE;
• The keyword WORK RELEASE releases the ORACLE connection
Trang 33Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Errors and Warnings
• Generated whenever an SQL statement fails
• Oracle pre-compilers provide a method to handle and check error statuses using the
• SQLCA (SQL Communication Area)
– Updated after every executable SQL statement
– Contains errors,warning and status information
• WHENEVER (explained further)
– To determine the outcome one can check the variables in the SQLCA explicitly or implicitly with the WHENEVER statement
• ORACA (Oracle Communication Area)
Trang 34Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 35Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Processing logic EXCEPTION
handlers END;
END-EXEC;
Trang 36Appendix B – SQLCA structure
Trang 37Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Trang 38Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
• sqlcaid - is a character string initialized to ‘SQLCA’ when
allocated
• sqlcabc - is a long integer set to the length in bytes of the
SQLCA structure itself
• sqlcode - is a long integer which summarizes the result of a SQL
statement =0 - succesful execution
>0 - successful execution with a status code ( eg
1403 for ‘no row found)
<0 - error in program or system failure
• sqlerrm.sqlerrml - length of SQL error message.
• sqlerrm.sqlerrmc - text of error found
• sqlerrd - array of long to describe the internal state of ORACLE
RDBMS The third element is used to indicate the number of
rows fetched by the DML operations such as INSERT or
UPDATE
SQLCA Structure explanation-1/2
Trang 39Copyright © 2005, Infosys Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
• sqlwarn - consists of eight single character elements to indicate various
warns
[0] - is set to ‘W’ if warnings are set for the particular statement else
set to blank
[1] - is set to ‘W’ if one or more character fields are set truncated
[2] - is set to ‘W’ if one or more null values are ignored in
ation of a function
[3] - is set to ‘W’ if number of columns selected does not equal to
host variables specified in the INTO clause
[4] - is set to ‘W’ to signify that UPDATE and DELETE have been
specified without a where clause
[5] - CREATE failed because of PL/SQL compilation error
[6] - not used
[7] - not used
• sqlext - currently unused.
SQLCA Structure explanation-2/2
Trang 40Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003 Version No 2.0
Thank You!