Professional Documents
Culture Documents
(../../default.aspx)
What is Pro*C?
Why Pro*C?
Embedded Commands
Host Variables
Error Handling
Compiling
Update Salary
What is Pro*C
Pro*c is writing a C program with embedded SQL statements. It is possible to access Oracle database
from C program using Pro*C. It allows you to use the power and flexibility of C language for data
processing and SQL to retrieve and manipulate data of Oracle database.
Oracle provides PROC compiler, which takes a C program with embedded SQL statements and converts
the embedded SQL statements to Oracle Runtime Library (also supplied by Oracle).
The compiled C program is a pure C code and can be compiled with C compiler. Pro*C supports
Microsoft Visual C++ 6.0 compiler.
Why Pro*C?
The following are some of the reasons why use have to use PreCompiler such as Pro*C.
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 1/10
8/17/2021 Getting Started With Pro*C
The above list is by no means complete. It is only to give you some idea about what can be done and
not a comprehensive list of all possibilities.
Depending on the language you want to use load appropriate compiler. The languages supported by
Precompiler is C/C++, COBOL , FORTRAN, and ADA.
As we are talking about Pro*C, we need to install C/C++ compiler that is supported by Oracle. On
Windows, Oracle supports Visual C++ 6.0 compiler. So make sure you have installed Visual C++ 6.0.
So far knowledge is concerned, you must be aware of C of course and SQL and if required PL/SQL.
The following is a simple Pro*C program to connect to Oracle using SCOTT username and password
TIGER.
Write the following code as save the file with .PC extension. The example is saved with the name
SAMPLE.PC.
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 2/10
8/17/2021 Getting Started With Pro*C
#include <stdio.h>
#include <string.h>
#include <sqlda.h>
#include <sqlcpr.h>
VARCHAR uid[30];
VARCHAR pwd[30];
void main()
strcpy(uid.arr,"SCOTT");
uid.len =strlen(uid.arr);
strcpy(pwd.arr,"TIGER");
pwd.len = strlen(pwd.arr);
return;
errexit:
printf("Connection failed");
return;
} /* end of main */
Embedded Commands
These commands are the commands that Pro*C compiler takes care of. They are meant for Pro*C
compiler. They are either converted to some calls to runtime library of Oracle or provide required
information to Pro*C compiler. So these command are of two types.
Executable - they are executed by Oracle to perform some operation
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 3/10
8/17/2021 Getting Started With Pro*C
All embedded commands are included into C program using the prefix EXEC SQL. That means Pro*C
compiler is converting all lines that start with EXEC SQL into appropriate C code.
Declaring variables that are to be used with SQL statements is to be done inside BEGIN DECLARE
SECTION and END DECLARE SECTION. This section is used to
declare host variables.
VARCHAR uid[30];
VARCHAR pwd[30];
Host Variables
Host variables are variables of the host language that are used with SQL command in embedded sql.
Host variables are used to communicate with Oracle. Host variables are used to send values from C to
Oracle and also to receive values from Oracle.
Host variables are declared between BEGIN DECLARE SECTION and END DECLARE SECTION and used in
embedded sql with prefix : (colon).
A host variables can be associated with Indicator Variable that indicates the value or condition of the
host variable.
In the sample program we created two host variables - uid and pwd. Each of type VARCHAR, which in
internally converted to a structure with two members - arr and len.
The statement INCLUDE SQLCA.H is used to include a structure called SQLCA, which stands for SQL
Communication Area. This is used by Oracle to provide information about the most recently executed
command. We will see more on this in later articles.
In order to use a host variable of VARCHAR type, we need to copy the string to arr and length of the
string to len members.
strcpy(uid.arr,"SCOTT");
uid.len =strlen(uid.arr);
strcpy(pwd.arr,"TIGER");
pwd.len = strlen(pwd.arr);
Error handling
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 4/10
8/17/2021 Getting Started With Pro*C
WHENEVER statement is used to specify what action to be taken when there is an error in the
subsequent statements.
The above statement specifies if there is an error in any of the subsequent statements then control
should goto a label called errexit.
There are other forms of WHENEVER statement that we will examine in later articles.
The next statement used CONNECT command to connect to Oracle. It uses host variables - uid and pwd
to supply username and password. Once connection is successful then it displays a message and then
COMMIT WORK RELEASE will release the resources and log off.
return;
Statements given after lable errexit will be executed when connection is not successful. It displays an
error message and terminates main().
errexit:
printf("Connection failed");
return;
Compiling
After the sample program is saved under the name SAMPLE.PC. Take the following steps to compile and
run the program.
The following steps assume that SAMPLE.PC is in C:\SRIKANTH\PROC directory and Oracle is installed
into C:\ORACLE8I directory.
Also make sure BIN directory of Oracle and BIN directory of Visual C++ are in system PATH.
Make C:\SRIKANTH\PROC directory the current directory.
Use the following command to compile .PC to create .C.
PROC SAMPLE.C
Once SAMPLE.C file is created in the current directory then compile it using command line
compiler of VC++ as follows to create SAMPLE.EXE.
/LINK is used to pass information to linker. In this case we passwed name of library to be used by
the linker.
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 5/10
8/17/2021 Getting Started With Pro*C
C:\SRIKANTh\PROC\>SAMPLE <enter>
The following sample program takes employee number and new salary and changes SAL column of EMP
table to the given value. It takes employee number from user and displays the name and salary of the
employee. Then it takes new salary from user and changes the current salary to the new salary.
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 6/10
8/17/2021 Getting Started With Pro*C
#include <stdio.h>
#include <string.h>
#include <sqlda.h>
#include <sqlcpr.h>
VARCHAR uid[80];
VARCHAR pwd[20];
int empno;
int sal;
VARCHAR ename[30];
int newsal;
void main()
strcpy(uid.arr,"SCOTT");
uid.len =strlen(uid.arr);
strcpy(pwd.arr,"TIGER");
pwd.len = strlen(pwd.arr);
scanf("%d",&empno);
EXEC SQL select ename, sal into :ename, :sal from emp where empno = :empno
scanf("%d", &newsal);
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 7/10
8/17/2021 Getting Started With Pro*C
EXEC SQL update emp set sal = :newsal where empno = :empno;
printf("Updation Successful");
goto normalexit;
noemp:
normalexit:
return;
errexit:
First we connect to Oracle using SCOTT/TIGER. Then we prompt user to enter the number of employee
whose salary is to be updated.
scanf("%d",&empno);
Then we user SELECT command to retrieve the name and salary of the employee using ENAME and SAL
columns of EMP table. It uses two more host variables - ename and sal, to copy the data retrieved using
SELECT with INTO clause.
EXEC SQL select ename, sal into :ename, :sal from emp where empno = :empno;
WHENEVER statement
WHENEVER command with NOTFOUND option is used to specify that control should goto label noemp
if SELECT command doesn't find a row. This is to handle the cases where the employee number entered
by user is not valid. The following code at label noemp will display a message and then procedure
further to terminate the program.
noemp:
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 8/10
8/17/2021 Getting Started With Pro*C
CONTINUE Indicates that the program should continue with the next statement.
When SELECT command is successful the data is copied into host variables - ename and sal.
Host
variable of type VARCHAR is internally declared as a structure that contains two members
- arr and len.
Oracle copies the length of the string into len member of the structure and data into arr.
As the string is
not null terminated and terminating string with null character is
required in C, we place null character
into string at len position.
scanf("%d", &newsal);
The statement INCLUDE SQLCA inserts the declaration of structure and the declaration of variable -
sqlca, which is of type struct sqlca. This structure variable can be used to get information about
previous execution.
The following table lists some of the important members of this structure.
Output
Compile and run the program and you must see the following output.
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 9/10
8/17/2021 Getting Started With Pro*C
www.srikanthtechnologies.com/blog/oracle/procgetstarted.aspx 10/10