Tải bản đầy đủ (.ppt) (40 trang)

oracle slides04 fp2005 ver 1.0

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (250.19 KB, 40 trang )

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.
11
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Example: Using DECLARE
EXEC SQL BEGIN DECLARE SECTION;
int emp_no;
char emp_name[30];

float emp_sales;
short ind_sales;
EXEC SQL END DECLARE SECTION;
12
Copyright © 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)
13
Copyright © 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.
14
Copyright © 2005, Infosys
Technologies Ltd
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
15
Copyright © 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
16
Copyright © 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
17
Copyright © 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;
if (ind_sales == -1)
printf(“No sales achieved by %s”,emp_name);
else
printf(“sales achieved ny %s is %d”,emp_name,emp_sales);
}
Demo Host & Indicator variables
18
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0

WHENEVER statement is used to do automatic checking and error handling


Syntax:
EXEC SQL WHENEVER <condition> <action>;
<condition>
{ [SQLWARNING] |
[SQLERROR] |
[NOT FOUND]
}
<action>
{ [CONTINUE] |
[DO function_call() | break ]
[goto statement_label] |
[STOP]
}

The scope of the WHENEVER is positional, not logical.
WHENEVER
19
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
WHENEVER - Actions

CONTINUE

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

GOTO <label>

Control transferred to labeled statement

STOP

Execution of Program stops, uncommitted work rolled back
20
Copyright © 2005, Infosys
Technologies Ltd
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;
}
21
Copyright © 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
Appendix A – More about Pro C
23
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Pointer variables

C supports pointers, which point to other variables. One can define

pointers as host variables in the declare section of your program.
EXEC SQL BEGIN DECLARE SECTION;
int *int_ptr;
char *char_ptr;
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
24
Copyright © 2005, Infosys
Technologies Ltd
ER/CORP/CRS/DB25/003
Version No. 2.0
Host arrays

When a collection of related items is declared as a host variable it is
called a host array.
EXEC BEGIN DECLARE SECTION;
char emp_name[20][20];
int salary[20];
EXEC SQL END DECLARE SECTION;

Likewise a collection of indicator variables is an indicator array.
25
Copyright © 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

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×