#include <stdio.h> #include <string.h> #include <sqlda.h> #include <sqlcpr.h> EXEC SQL BEGIN DECLARE SECTION; VARCHAR uid[30]; VARCHAR pwd[30]; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE SQLCA.H; void main() { strcpy(uid.arr,"SCOTT"); uid.len =strlen(uid.arr); strcpy(pwd.arr,"TIGER"); pwd.len = strlen(pwd.arr); EXEC SQL WHENEVER SQLERROR GOTO errexit; EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; printf("Connected to Oracle8i using Scott/Tiger\n"); EXEC SQL COMMIT WORK RELEASE; return; errexit: printf("Connection failed"); return; } /* end of main */
EXEC SQL BEGIN DECLARE SECTION; VARCHAR uid[30]; VARCHAR pwd[30]; EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA.H;
strcpy(uid.arr,"SCOTT"); uid.len =strlen(uid.arr); strcpy(pwd.arr,"TIGER"); pwd.len = strlen(pwd.arr);
EXEC SQL WHENEVER SQLERROR GOTO errexit;
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; printf("Connected to Oracle8i using Scott/Tiger\n"); EXEC SQL COMMIT WORK RELEASE; return;
errexit: printf("Connection failed"); return;
PROC SAMPLE.C
CL /IC:\ORACLE8I\PRECOMP\PUBLIC SAMPLE.C /LINK C:\ORACLE8i\PRECOMP\LIB\MSVC\ORASQL8.LIB
C:\SRIKANTh\PROC\>SAMPLE <enter> Connected to Oracle8i using Scott/Tiger
/* program to change the salary of an employee */ #include <stdio.h> #include <string.h> #include <sqlda.h> #include <sqlcpr.h> EXEC SQL BEGIN DECLARE SECTION; VARCHAR uid[80]; VARCHAR pwd[20]; int empno; int sal; VARCHAR ename[30]; int newsal; EXEC SQL END DECLARE SECTION; EXEC SQL INCLUDE SQLCA.H; void main() { strcpy(uid.arr,"SCOTT"); uid.len =strlen(uid.arr); strcpy(pwd.arr,"TIGER"); pwd.len = strlen(pwd.arr); EXEC SQL WHENEVER SQLERROR GOTO errexit; EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; /* take employee number from user */ printf("Enter employee number : "); scanf("%d",&empno); /* get the details of the employee */ EXEC SQL WHENEVER NOTFOUND GOTO noemp; EXEC SQL select ename, sal into :ename, :sal from emp where empno = :empno; /* display employee name and salary */ ename.arr [ ename.len ] = '\0'; printf("Name : %s Salary : %d\n", ename.arr, sal); printf("Enter new salary : "); scanf("%d", &newsal); /* update employee record */ EXEC SQL update emp set sal = :newsal where empno = :empno; printf("Updation Successful"); goto normalexit; noemp: printf("Sorry. Invalid employee number. Quitting..."); normalexit: EXEC SQL COMMIT WORK RELEASE; return; errexit: printf("Error: %70s", sqlca.sqlerrm.sqlerrmc); }
/* take employee number from user */ printf("Enter employee number : "); scanf("%d",&empno);
EXEC SQL WHENEVER NOT FOUND GOTO noemp; EXEC SQL select ename, sal into :ename, :sal from emp where empno = :empno;
noemp: printf("Sorry. Invalid employee number. Quitting...");
WHENEVER {NOT FOUND|SQLERROR|SQLWARNING} {CONTINUE|GOTO label|STOP|DO routine}
/* display employee name and salary */ ename.arr [ ename.len ] = '\0'; printf("Name : %s Salary : %d\n", ename.arr, sal); printf("Enter new salary : "); scanf("%d", &newsal);
Enter employee number : 7369 Name : SMITH Salary : 800 Enter new salary : 900 Updation Successful. No. of rows updated: 1