본문 바로가기

ProC

Pro*c 예제, 내장함수 예제

1318751164_dynmic_ex.pc

 

1318751164_proc_ex2.pc

 

1318751164_proc_ex.pc

 

1318751164_proc_ex_cgi.pc

 

1318751164_proc_ex_stored_procedure.pc

 

Pro*c 예시.

번호: 84 / 작성자: jinyedge / 등록일: 2004-10-23 17:08:06 / 조회: 170
#include <stdio.h>
EXEC SQL INCLUDE SQLCA;

/*Define*/
#define IS_EMPTY(value) (value == NULL || ! value[0])

/*----------------*/
void ora_conn(){
EXEC SQL BEGIN DECLARE SECTION;
char uid[100] = "scott/tiger@oragizmo";
EXEC SQL END DECLARE SECTION;

/*For cgi*/
putenv("ORACLE_HOME=/home/oracle/ora9204");
putenv("ORACLE_SID=oragizmo");
putenv("NLS_LANG=American_america.KO16KSC5601");
putenv("LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME/lib");

EXEC SQL CONNECT :uid;
if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(-1);
}
}

/*----------------*/
void ora_close(){
if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(-1);
}
}

/*----------------*/
void get_date(){
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR date[100];
EXEC SQL END DECLARE SECTION;

EXEC SQL
SELECT sysdate
INTO :date
FROM dual;

date.arr[date.len] = '\0';
printf("+%s+\n", date.arr);
}

/*----------------*/
void get_tab(){
EXEC SQL BEGIN DECLARE SECTION;
int i = 0, cnt = 0;
VARCHAR tab_arr[100][100];
EXEC SQL END DECLARE SECTION;

EXEC SQL
SELECT count(*)
INTO :cnt
FROM tab;

EXEC SQL
SELECT tname
INTO :tab_arr
FROM tab;

for(i = 0; i < 100 && i < cnt; i++){
tab_arr[i].arr[tab_arr[i].len] = '\0';

puts(tab_arr[i].arr);
}
}

/*----------------*/
void make_table(){
EXEC SQL BEGIN DECLARE SECTION;
int cnt = 0;
char *tname = "ptest";
EXEC SQL END DECLARE SECTION;

EXEC SQL
SELECT count(*)
INTO :cnt
FROM tab
WHERE tname = :tname;

if(cnt > 0){
return;
}

EXEC SQL
CREATE TABLE ptest(
no number
, name varchar2(100)
);
}

/*----------------*/
void delete_all(){
EXEC SQL
DELETE
FROM ptest;

if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK;
}
EXEC SQL COMMIT;
}

/*----------------*/
void insert_one(){
EXEC SQL BEGIN DECLARE SECTION;
int no = 1;
char *name = "효도르";
EXEC SQL END DECLARE SECTION;

EXEC SQL
INSERT INTO ptest(no, name)
VALUES(:no, :name);

if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK;
}
EXEC SQL COMMIT;
}

/*----------------*/
void insert_arr(){
EXEC SQL BEGIN DECLARE SECTION;
int no_arr[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
char name_arr[10][100] = {"헌트", "세포", "크로캅", "헨조", "힉슨", "사쿠라바", "모", "타이슨", "피터", "반나"};
EXEC SQL END DECLARE SECTION;

EXEC SQL
INSERT INTO ptest(no, name)
VALUES(:no_arr, :name_arr);

if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK;
}
EXEC SQL COMMIT;
}

/*----------------*/
void update_one(){
EXEC SQL BEGIN DECLARE SECTION;
int no = 100;
char *name = "이고르";
EXEC SQL END DECLARE SECTION;

EXEC SQL
UPDATE ptest
SET name = :name
WHERE no = :no;

if(sqlca.sqlcode < 0){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK;
}
EXEC SQL COMMIT;
}

/*----------------*/
void select_some(){
EXEC SQL BEGIN DECLARE SECTION;
int i = 0;
VARCHAR name_arr[100][100];
EXEC SQL END DECLARE SECTION;

/*Init name_arr*/
for(i = 0; i < 100; i++){
strcpy(name_arr[i].arr, "");
}

EXEC SQL
SELECT name
INTO :name_arr
FROM ptest
WHERE ROWNUM <= 100
ORDER BY no;

for(i = 0; i < 100 && ! IS_EMPTY(name_arr[i].arr); i++){
name_arr[i].arr[name_arr[i].len] = '\0';
puts(name_arr[i].arr);
}
}

/*----------------*/
void select_all(){
EXEC SQL BEGIN DECLARE SECTION;
int i = 0, no = 0;
char q[200] = "";
VARCHAR name[100];
EXEC SQL END DECLARE SECTION;

sprintf(q, "select no, name from ptest order by no");
EXEC SQL PREPARE stmt FROM :q;
EXEC SQL DECLARE c1 CURSOR FOR stmt;
EXEC SQL OPEN c1;

EXEC SQL WHENEVER NOT FOUND DO break;
while(i++ < 10000){
EXEC SQL
FETCH c1
INTO :no, :name;

name.arr[name.len] = '\0';
printf("no: %d, name: %s\n", no, name.arr);
}
EXEC SQL CLOSE c1;
}

/*----------------*/
int main(int argc, char *argv[]){
/*Connect*/
ora_conn();

/*Get date*/
puts("");
puts("--Get date from dual.");
get_date();

/*Get table name*/
puts("");
puts("--Get table name from tab.");
get_tab();

/*Make table*/
make_table();

/*Delete data*/
delete_all();

/*Insert some data*/
insert_one();
insert_arr();

/*Update data*/
update_one();

/*Select some*/
puts("");
puts("--Select some data from ptest.");
select_some();

/*Select all*/
puts("");
puts("--Select all data from ptest.");
select_all();

/*Close*/
ora_close();

return 0;
}
proc example - cgi
번호: 68 / 작성자: jinyedge / 등록일: 2002-09-27 17:52:44 / 조회: 218

/*
This is a cgi program which connects oracle using proc. But before you test this program
you have to edit /etc/ld.so.conf file. Add following line to your /etc/ld.so.conf and enter
ldconfig command in the command line.

/home/oracle/product/8.1.7/lib

And if you don't want to use putenv in your cgi program you can add following lines at the
end of your http.conf file.

SetEnv ORACLE_HOME /home/oracle/product/8.1.7
SetEnv ORACLE_SID ORCL
SetEnv NLS_LANG Korean
SetEnv LD_LIBRARY_PATH /home/oracle/product/8.1.7/lib
*/

#include <stdio.h>
#include <string.h>

/*--------------*/
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR name[50];
VARCHAR varbuf[1024];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA;

/*--------------*/
void proc_perror(void){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(-1);
}

/*--------------*/
void proc_conn(char *uid, char *pwd){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
}

/*--------------*/
void proc_conn2(char *uid, char *pwd, char *sid){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; USING :sid;
}

/*--------------*/
void proc_close(void){
EXEC SQL COMMIT WORK RELEASE;
}

/*--------------*/
void proc_exec(char *q){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL EXECUTE IMMEDIATE :q;
EXEC SQL COMMIT WORK;
}

/*--------------*/
void proc_exec2(char *q){/*트랜잭션 모드*/
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL EXECUTE IMMEDIATE :q;
}

/*--------------*/
void proc_commit(void){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL COMMIT WORK;
}

/*--------------*/
void proc_rollback(void){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL ROLLBACK WORK;
}

/*--------------*/
void proc_one_str(char *q, char *buf){
/*커서 OPEN*/
EXEC SQL PREPARE s_one_str FROM :q;
EXEC SQL DECLARE c_one_str CURSOR FOR s_one_str;
EXEC SQL OPEN c_one_str;

EXEC SQL FETCH c_one_str INTO :varbuf;
/*커서 CLOSE*/
EXEC SQL CLOSE c_one_str;

strcpy(buf, varbuf.arr);
}

/*--------------*/
int proc_one_int(char *q){
int res = 0;

/*커서 OPEN*/
EXEC SQL PREPARE s_one_int FROM :q;
EXEC SQL DECLARE c_one_int CURSOR FOR s_one_int;
EXEC SQL OPEN c_one_int;

EXEC SQL FETCH c_one_int INTO :res;
/*커서 CLOSE*/
EXEC SQL CLOSE c_one_int;
return res;
}

/*--------------*/
int proc_is_table(char *tab){
int res = 0;
char q[200];

sprintf(q, "select count(*) from tab where tname = '%s'", tab);

/*커서 OPEN*/
EXEC SQL PREPARE t_one_int FROM :q;
EXEC SQL DECLARE t_one_int CURSOR FOR t_one_int;
EXEC SQL OPEN t_one_int;

EXEC SQL FETCH t_one_int INTO :res;
/*커서 CLOSE*/
EXEC SQL CLOSE t_one_int;

return res;
}

/*--------------*/
int main(void){
int no, age;
char q[300];
char buf[100];

printf("Content-type: text/html\n\n");
putenv("ORACLE_HOME=/home/oracle/product/8.1.7");
putenv("ORACLE_SID=ORCL");
putenv("LD_LIBRARY_PATH=/home/oracle/product/8.1.7/lib");
putenv("NLS_LANG=Korean");

/*오라클 연결*/
proc_conn("scott", "tiger");

/*delete table*/
if(proc_is_table("TEMP")){
proc_exec("drop table temp");
}

/*create table*/
proc_exec("create table temp(no number, name varchar2(100), age number)");

/*insert data*/
proc_exec("insert into temp(no, name, age) values(1, '박찬호', 30)");
proc_exec("insert into temp(no, name, age) values(2, '김병현', 23)");
proc_exec("insert into temp(no, name, age) values(3, '최희섭', 22)");

/*select one str*/
puts("select one string..<br>");
proc_one_str("select name from temp where no = 3", buf);
printf("name: %s\n<br>", buf);

/*select one int*/
puts("<br>select one int..<br>");
age = proc_one_int("select age from temp where no = 3");
printf("age: %d\n<br>", age);

/*커서 OPEN*/
strcpy(q, "select no, name, age from temp order by no");
EXEC SQL PREPARE S1 FROM :q;
EXEC SQL DECLARE C1 CURSOR FOR S1;
EXEC SQL OPEN C1;

EXEC SQL WHENEVER NOT FOUND DO break;
puts("<br>fetch rows..<br>");
while(1){
EXEC SQL FETCH C1
INTO :no, :name, :age;
printf("no: %d, name: %s, age: %d\n<br>", no, name.arr, age);
}

/*커서 CLOSE*/
EXEC SQL CLOSE C1;

/*연결 종료*/
proc_close();

return 0;
}

proc example - dynamic sql
번호: 61 / 작성자: jinyedge / 등록일: 2002-05-10 12:24:15 / 조회: 189
#include <stdio.h>
#include <string.h>

/*--------------*/
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR name[50];
VARCHAR varbuf[1024];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA;

/*--------------*/
void proc_perror(void){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(-1);
}

/*--------------*/
void proc_conn(char *uid, char *pwd){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
}

/*--------------*/
void proc_conn2(char *uid, char *pwd, char *sid){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; USING :sid;
}

/*--------------*/
void proc_close(void){
EXEC SQL COMMIT WORK RELEASE;
}

/*--------------*/
void proc_exec(char *q){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL EXECUTE IMMEDIATE :q;
EXEC SQL COMMIT WORK;
}

/*--------------*/
void proc_exec2(char *q){/*트랜잭션 모드*/
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL EXECUTE IMMEDIATE :q;
}

/*--------------*/
void proc_commit(void){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL COMMIT WORK;
}

/*--------------*/
void proc_rollback(void){
EXEC SQL WHENEVER SQLERROR DO proc_perror();
EXEC SQL ROLLBACK WORK;
}

/*--------------*/
void proc_one_str(char *q, char *buf){
/*커서 OPEN*/
EXEC SQL PREPARE s_one_str FROM :q;
EXEC SQL DECLARE c_one_str CURSOR FOR s_one_str;
EXEC SQL OPEN c_one_str;

EXEC SQL FETCH c_one_str INTO :varbuf;
/*커서 CLOSE*/
EXEC SQL CLOSE c_one_str;

strcpy(buf, varbuf.arr);
}

/*--------------*/
int proc_one_int(char *q){
int res = 0;

/*커서 OPEN*/
EXEC SQL PREPARE s_one_int FROM :q;
EXEC SQL DECLARE c_one_int CURSOR FOR s_one_int;
EXEC SQL OPEN c_one_int;

EXEC SQL FETCH c_one_int INTO :res;
/*커서 CLOSE*/
EXEC SQL CLOSE c_one_int;
return res;
}

/*--------------*/
int proc_is_table(char *tab){
int res = 0;
char q[200];

sprintf(q, "select count(*) from tab where tname = '%s'", tab);

/*커서 OPEN*/
EXEC SQL PREPARE t_one_int FROM :q;
EXEC SQL DECLARE t_one_int CURSOR FOR t_one_int;
EXEC SQL OPEN t_one_int;

EXEC SQL FETCH t_one_int INTO :res;
/*커서 CLOSE*/
EXEC SQL CLOSE t_one_int;

return res;
}

/*--------------*/
int main(void){
int no, age;
char q[300];
char buf[100];

/*오라클 연결*/
proc_conn("scott", "tiger");

/*delete table*/
if(proc_is_table("TEMP")){
proc_exec("drop table temp");
}

/*create table*/
proc_exec("create table temp(no number, name varchar2(100), age number)");

/*insert data*/
proc_exec("insert into temp(no, name, age) values(1, '박찬호', 30)");
proc_exec("insert into temp(no, name, age) values(2, '김병현', 23)");
proc_exec("insert into temp(no, name, age) values(3, '최희섭', 22)");

/*select one str*/
puts("select one string..");
proc_one_str("select name from temp where no = 3", buf);
printf("name: %s\n", buf);

/*select one int*/
puts("\nselect one int..");
age = proc_one_int("select age from temp where no = 3");
printf("age: %d\n", age);

/*커서 OPEN*/
strcpy(q, "select no, name, age from temp order by no");
EXEC SQL PREPARE S1 FROM :q;
EXEC SQL DECLARE C1 CURSOR FOR S1;
EXEC SQL OPEN C1;

EXEC SQL WHENEVER NOT FOUND DO break;
puts("\nfetch rows..");
while(1){
EXEC SQL FETCH C1
INTO :no, :name, :age;
printf("no: %d, name: %s, age: %d\n", no, name.arr, age);
}

/*커서 CLOSE*/
EXEC SQL CLOSE C1;

/*연결 종료*/
proc_close();

return 0;
}
proc example
번호: 60 / 작성자: jinyedge / 등록일: 2002-05-09 20:36:59 / 조회: 235
#include <stdio.h>
#include <string.h>

/*--------------*/
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR uid[20];
VARCHAR pwd[20];
VARCHAR sid[20];
VARCHAR name[20];
int no;
int age;
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE SQLCA;

/*--------------*/
void proc_perror(void){
printf("%s\n", sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(-1);
}

/*--------------*/
int main(void){
strcpy(uid.arr, "scott");
uid.len = strlen(uid.arr);
strcpy(pwd.arr, "tiger");
pwd.len = strlen(pwd.arr);
strcpy(sid.arr, "ora817");
sid.len = strlen(sid.arr);

/*에러 핸들러*/
EXEC SQL WHENEVER SQLERROR DO proc_perror();

/*오라클 연결*/
EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
/*EXEC SQL CONNECT :uid IDENTIFIED BY :pwd; USING :sid;*/

/*delete data*/
EXEC SQL DELETE FROM TEMP;
EXEC SQL COMMIT WORK;

/*insert data*/
no = 1;
strcpy(name.arr, "박찬호");
name.len = strlen(name.arr);
age = 30;
EXEC SQL INSERT INTO TEMP(NO, NAME, AGE) VALUES(:no, :name, :age);

strcpy(name.arr, "김병현");/*문자열의 경우는 변수로 처리한다.*/
name.len = strlen(name.arr);
EXEC SQL INSERT INTO TEMP(NO, NAME, AGE) VALUES(2, :name, 23);

strcpy(name.arr, "최희섭");
name.len = strlen(name.arr);
EXEC SQL INSERT INTO TEMP(NO, NAME, AGE) VALUES(3, '최희섭', 22);

EXEC SQL COMMIT WORK;

/*set error handler*/
EXEC SQL WHENEVER NOT FOUND GOTO notfound;
/*select one col*/
EXEC SQL SELECT NAME
INTO :name
FROM TEMP
WHERE NO = 1;

puts("select one col..");
printf("name: %s\n", name.arr);

/*select one row*/
EXEC SQL SELECT NAME, AGE
INTO :name, :age
FROM TEMP
WHERE NO = 2;

puts("\nselect one row..");
printf("name: %s, age: %d\n", name.arr, age);

/*커서 선언*/
EXEC SQL DECLARE my_cursor CURSOR FOR
SELECT NO, NAME, AGE
FROM TEMP
ORDER BY NO ASC;

/*커서 OPEN*/
EXEC SQL OPEN my_cursor;

EXEC SQL WHENEVER NOT FOUND DO break;
puts("\nfetch rows..");
while(1){
EXEC SQL FETCH my_cursor
INTO :no, :name, :age;
printf("no: %d, name: %s, age: %d\n", no, name.arr, age);
}

/*커서 CLOSE*/
EXEC SQL CLOSE my_cursor;

/*연결 종료*/
EXEC SQL COMMIT WORK RELEASE;
return 0;

notfound:
puts("\nThere is no data!!");
return 0;
}