Programming Using Pro *C Oracle Day 4 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Objectives Introduction to Oracle Pre-compilers Embedded SQL Pro *C 3 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Pre-compilers • Pre-compilers are tools that allow you to embed SQL statements in the HLL (High Level Language) source code. • Pre-compilers accept SQL statements , translate the SQL statements into runtime calls , generate a source code that can be compiled, linked and executed. 4 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Oracle Pre-compilers • Oracle supports pre-compilers for the following HLL, • C • COBOL • Fortran • Pascal • PL/I • Ada • Oracle supports all this on different platforms like UNIX,DOS, NETWARE,WINDOWS,VAX,SUN etc 5 Copyright © 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. 6 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Embedded SQL- Definition • SQL statements within a programming language called Embedded SQL statements. • The source code containing the embedded SQL is called Host program. • Embedded SQL statements in the source code begin with EXEC SQL • Example: EXEC SQL INSERT INTO branch (bcode,location) VALUES (:br_code.:br_location); 7 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0 Embedded SQL Program Dev – The big picture Embedded SQL Program Editor ORACLE Pre-compiler Translated Source Program Compiler Object Program Linker Executable Program ORACLE Run time Library(SQLLIB) Resolve calls SQL statements replaced by Library calls 8 Copyright © 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 9 Copyright © 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); 10 Copyright © 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. [...]... 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 >=0 • Oracle assigns NULL value to the column Oracle assigns value of host variables For output host variables, Oracle assigns the indicator variables with a value -1 0 >0 column value is NULL Oracle assigns column value to host Oracle. .. Technologies Ltd 13 ER/CORP/CRS/DB25/003 Version No 2.0 Host variables • Host variables are of two types – Input Program assigns values to input host variables and passes data to ORACLE – Output ORACLE assigns values to output host variables and passes data to program Copyright © 2005, Infosys Technologies Ltd 14 ER/CORP/CRS/DB25/003 Version No 2.0 Indicator Variables • Every host variable can be associated... Infosys Technologies Ltd 17 ER/CORP/CRS/DB25/003 Version No 2.0 WHENEVER • WHENEVER statement is used to do automatic checking and error handling • Syntax: EXEC SQL WHENEVER ; { [SQLWARNING] | [SQLERROR] | [NOT FOUND] } { [CONTINUE] | [DO function_call() | break ] [goto statement_label] | [STOP] } • The scope of the WHENEVER is positional, not logical Copyright ©... 2005, Infosys Technologies Ltd 32 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... 19 ER/CORP/CRS/DB25/003 Version No 2.0 WHENEVER - Example main() { //Some piece of code EXEC SQL DECLARE emp_cursor FOR SELECT empno,ename FROM emp; 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; } Copyright © 2005, Infosys Technologies Ltd 20 ER/CORP/CRS/DB25/003 Version No 2.0 Summary •... 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) Copyright © 2005, Infosys Technologies Ltd 12 ER/CORP/CRS/DB25/003 Version No 2.0 Host variables • Host variables allow communication between Oracle and your... ER/CORP/CRS/DB25/003 Version No 2.0 VARCHAR Variables • VARCHAR is a pre-declared structure • Example: EXEC SQL BEGIN DECLARE SECTION; varchar username[20]; EXEC SQL END DECLARE SECTION; translates to: struct{ unsigned short len; unsigned char arr[20]; } username; Copyright © 2005, Infosys Technologies Ltd 26 ER/CORP/CRS/DB25/003 Version No 2.0 VARCHAR variables (contd.) • Before passing an input VARCHAR variable to ORACLE, ... 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) Copyright © 2005, Infosys Technologies Ltd 33 ER/CORP/CRS/DB25/003 Version No 2.0 PL/SQL in Pro*C • The Pro*C pre-compiler treats a PL/SQL block as though it were single embedded SQL statement • One can place... 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 • Example: select name into :empName FROM employee where empNo = 1001; empName.arr[empName.len] = ‘\0’; printf(“\nName : %s “,empName.arr); Demo varchar Copyright © 2005, Infosys Technologies Ltd 29 ER/CORP/CRS/DB25/003 Version No 2.0 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 Copyright © 2005, Infosys Technologies Ltd 30 ER/CORP/CRS/DB25/003 Version No 2.0 Example-Using Cursors • The cursor control statements DECLARE, OPEN, FETCH and CLOSE allow . value is NULL 0 Oracle assigns column value to host > ;0 Oracle assigns truncated value to host 17 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 Example: Using. *C Oracle Day 4 2 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 Objectives Introduction to Oracle Pre-compilers Embedded SQL Pro *C 3 Copyright © 200 5,. | [STOP] } • The scope of the WHENEVER is positional, not logical. WHENEVER 19 Copyright © 200 5, Infosys Technologies Ltd ER/CORP/CRS/DB25 /00 3 Version No. 2 .0 WHENEVER - Actions • CONTINUE – Continue