Oracle

게시글 보기
작성자 유건데이타 등록일 2015-05-21
제목 SAMPLE: DYNAMIC SQL METHOD3 SAMPLE(SELECT)
**************************************************************
Sample Program 8: Dynamic SQL Method 3
**************************************************************/
#define USERNAME "SCOTT"
#define PASSWORD "TIGER"
#include
#include
EXEC SQL INCLUDE sqlca;
#ifdef TRUE
#undef TRUE
#endif
#define TRUE 1
EXEC SQL BEGIN DECLARE SECTION;
char *username = USERNAME;
char *password = PASSWORD;
VARCHAR sqlstmt[80];
VARCHAR ename[10];
VARCHAR tname[10];
int deptno = 10;
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL CONNECT :username IDENTIFIED BY :password;
puts((char *) "\nConnected to ORACLE.\n");
sprintf(sqlstmt.arr,
"SELECT ENAME FROM %s WHERE DEPTNO = :v1","EMP");
sqlstmt.len = strlen((char *) sqlstmt.arr);
/* Display the SQL statement and its current input host
variable. */
puts((char *) sqlstmt.arr);
printf(" v1 = %d\n", deptno);
printf("\nEmployee\n");
printf("--------\n");

EXEC SQL PREPARE S FROM :sqlstmt;
EXEC SQL DECLARE C CURSOR FOR S;
EXEC SQL OPEN C USING :deptno;
EXEC SQL WHENEVER NOT FOUND GOTO notfound;
/* Loop until the NOT FOUND condition is detected. */
while (TRUE)
{
EXEC SQL FETCH C INTO :ename;
/* Null-terminate the array before output. */
ename.arr[ename.len] = '\0';
puts((char *) ename.arr);
}
notfound:
printf("\nQuery returned %d row%c.\n\n", sqlca.sqlerrd[2],
(sqlca.sqlerrd[2] == 1) ? '\0' : 's');
EXEC SQL CLOSE C;
/* Commit any pending changes and disconnect from
ORACLE. */
EXEC SQL COMMIT RELEASE;
puts((char *) "Have a good day!\n");
exit(0);
sqlerror:
printf("\n%.*s\n", sqlca.sqlerrm.sqlerrml,
sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
/* Release resources associated with the cursor. */
EXEC SQL CLOSE C;
/* Roll back any pending changes and disconnect from
ORACLE. */
EXEC SQL ROLLBACK RELEASE;
exit(1);
}
Comment
등록된 코멘트가 없습니다.