Home » Developer & Programmer » Precompilers, OCI & OCCI » pro*c varchar datatype
pro*c varchar datatype [message #94521] Mon, 03 January 2005 05:17 Go to next message
bhanu prasad
Messages: 6
Registered: December 2004
Junior Member
why we have to use varchar datatype in pro*c instead of char datatype please explain briefly with example
Re: pro*c varchar datatype [message #94522 is a reply to message #94521] Mon, 03 January 2005 16:25 Go to previous message
Jai Vrat Singh
Messages: 205
Registered: September 2002
Location: Singapore
Senior Member

char another_dt[21];
EXEC SQL VAR another_dt IS STRING(21);

<b>
EXEC SQL BEGIN DECLARE SECTION;
varchar dt[21];
EXEC SQL END   DECLARE SECTION;
</b>

strcpy(user,"XXXXX@SIDMY.WORLD");
strcpy(passwd,"XXXXX");

EXEC SQL WHENEVER SQLERROR DO print_error_msg();
EXEC SQL WHENEVER SQLWARNING DO print_warning_msg();

EXEC SQL CONNECT :user IDENTIFIED BY :passwd;

memset(dt.arr,'',21);
EXEC SQL SELECT TO_CHAR(sysdate,'DD/MON/YYYY HH24:MI:SS') into :dt from dual;
EXEC SQL SELECT TO_CHAR(sysdate,'DD/MON/YYYY HH24:MI:SS') into :another_dt from dual;
dt.len = strlen(dt.arr);
printf("Date is :%sn",dt.arr);
printf("Another Date is :%sn",another_dt);


You can see that after pre-compilation phase, the varchar declaration is transformed to structure like this. Everytime you have to reset the memory ( or null terminate) using memset.. as there is a possbility of garbage everytime.. Better to use dataype equivalencing ( see use of variable another_dt )
/* varchar dt[21]; */ 
struct { unsigned short len; char arr[21]; } dt;
Previous Topic: How to compile LOB Demo
Next Topic: Oracle 9i OCI (Solaris vs AIX)
Goto Forum:
  


Current Time: Tue Dec 11 11:52:27 CST 2018