Re: char(1) columns, embedded SQL

From: Tim Smith <tssmith_at_netcom.com>
Date: Tue, 13 Dec 1994 13:52:09 GMT
Message-ID: <tssmithD0r56x.5Hw_at_netcom.com>


pp001671_at_interramp.com writes:
>I am not sure which Pro*C or Oracle version you are using. I am using
>Oracle 7.1.3 with Pro*C 1.6 on IBM RS/6000 AIX and I had the similar question
>several months ago and this is what I found out ...
>If you precompile with MODE=ORACLE (default mode) then charval[ 0] will
>contain the correct the value.
>However, if you use MODE=ANSI then charval[ 0] will contain a null terminated
>string and the original values is lost. Therefore, you must declare
>char(1) columns as char(2) or varchar2(2) in your Pro*C programs, if you
>are going to use MODE=ANSI option. (I think in future versions, Oracle
>will force ANSI option by default).
> i.e. char charval[ 2];
> then
> charval[ 0] = 'x'; /* character from table */
> charval[ 1] = '\0';

Pro*C 2.x users should be aware that this character behavior (whether a string is null-terminated or not) is now governed by the DBMS option:

DBMS=V6       -- complete V6 semantics
DBMS=V6_CHAR  -- V6 semantics WRT character cols. only
DBMS=V7       -- Oracle7 semantics (must be connected to Oracle7)
DBMS=NATIVE   -- the semantics of the server you are connected to (the
                 default setting)

If you precompile using DBMS=V7 (or DBMS=NATIVE, and you are connected to an Oracle7 server), and you perform these embedded SQL statements:

exec sql create table t1 (c1 char, c2 char(3)); exec sql insert into t1 values ('a', 'bcd');

char single_char;
char str[3];

exec sql select c1, c2 into :single_char, :str from t1;

then you get:

'\0' in single_char
'b', 'c', '\0' in str

This is because the default datatype for C char host variables is CHARZ (datatype code 97), which always null terminates on output (and expects null termination on input), even if truncation is necessary.

To get 'a' in single_char, and 'b', 'c', 'd' in str, either precompile with DBMS=V6_CHAR, or use the TYPE command to change the default external datatype of single_char and str. It's in the manual.

As others have said, Pro*C will not write outside the boundaries of an array.

-Tim (tssmith_at_oracle.com) Received on Tue Dec 13 1994 - 14:52:09 CET

Original text of this message