원본 http://blog.naver.com/julymorning4/100024942863

Select 시 Table 이름을 Dynamic 하게 이용하기
--------------------------------------------------------------------------------
SELECT 시 TABLE 이름을 DYNAMIC 하게 이용하기 (PRO*C)
Bulletin no : 11139
--------------------------------------------------------------------------------
다음은 method 3 를 이용해 select 하는 table 명을 dynamic 하게 가져가는
program 이다.

#include
#include

#define USERNAME "scott"
#define PASSWORD "tiger"

#include
#include

EXEC ORACLE OPTION (ORACA=YES);

char    *username = USERNAME;
char    *password = PASSWORD;
VARCHAR  dynstmt[80];
varchar     ename[10] ;
int      deptno = 10;
char     tab[10];
void sql_error();


main()
{
    EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle error");
    oraca.orastxtf = ORASTFERR;

    EXEC SQL CONNECT :username IDENTIFIED BY :password;
    puts("\nConnected to Oracle.\n");
 
/*exec sql declare s statement;*/
 
  gets(tab);
    strcpy((char *)dynstmt.arr,
        "SELECT  ename FROM ");
   strcat((char*) dynstmt.arr,tab);
 
  dynstmt.len = strlen((char *)dynstmt.arr);

    puts((char *) dynstmt.arr);
    printf("   table name is  %s\n", tab );
    printf("\nEmployee\n");
    printf("--------\n");
  /*sprintf (statement ,"select ename from :tab");*/
    EXEC SQL PREPARE S FROM :dynstmt;
    EXEC SQL DECLARE C CURSOR FOR S;
    EXEC SQL OPEN C ;
    EXEC SQL WHENEVER NOT FOUND DO break;

    for (;;)
    {
        EXEC SQL FETCH C INTO :ename;

        ename.arr[ename.len] = '\0';
        puts((char *) ename.arr);
        }

    printf("\nQuery returned %d row%s.\n\n", sqlca.sqlerrd[2],
        (sqlca.sqlerrd[2] == 1) ? "" : "s");

    EXEC SQL CLOSE C;
    EXEC SQL COMMIT RELEASE;
    exit(0);
}


void sql_error(char *msg)
{
    printf("\n%s", msg);
    sqlca.sqlerrm.sqlerrmc[sqlca.sqlerrm.sqlerrml] = '\0';
    oraca.orastxt.orastxtc[oraca.orastxt.orastxtl] = '\0';
    oraca.orasfnm.orasfnmc[oraca.orasfnm.orasfnml] = '\0';
    printf("\n%s\n", sqlca.sqlerrm.sqlerrmc);
    printf("in \"%s...\"\n", oraca.orastxt.orastxtc);
    printf("on line %d of %s.\n\n", oraca.oraslnr,
       oraca.orasfnm.orasfnmc);

    EXEC SQL WHENEVER SQLERROR CONTINUE;
    EXEC SQL CLOSE C;
    EXEC SQL ROLLBACK RELEASE;!!!! exit(1);
}

'Oracle' 카테고리의 다른 글

톰캣 설치후 오라클 DB 생성하기  (0) 2009.03.12
간단한 Linux Pro*C 컴파일  (0) 2007.01.22
Pro*C를 이용한 간단 Data Select  (0) 2007.01.22
Pro*c로 PL/SQL 실행하기  (0) 2007.01.22
간단한 pro*c 예제  (0) 2007.01.22
, .