'DynamicName'에 해당되는 글 1건

  1. 2007.04.06 [PRO*C] Select 시 Table 이름을 Dynamic 하게 이용하기 by pino93

--------------------------------------------------------------------------------
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);
}

 

- oraclejava 펌 -

Posted by pino93