- 论坛徽章:
- 0
|
select col1, col2 from table1; //我不确定是否insert也可以,但是informix下,肯定是不可以的。还是用select吧
然后prepare这个语句,再describe
就能得到col1, col2的类型。
实际上 prepare,然后describe,是sql92标准定义的。不同的编程环境
提供了不同的实现,但是基本的思路都是一致的。
给你个例子
int i = 0;
ub4 numcols = 0;
ub2 type = 0;
OCIParam *colhd = (OCIParam *) 0; /* column handle */
text *sqlstmt = (text *)"insert into table1(col1,col2) values(?,?)"; //试试这样?
checkerr(errhp, OCIStmtPrepare(stmthp, errhp, (OraText *)sqlstmt,
(ub4)strlen((char *)sqlstmt),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT));
/* initialize svchp, stmhp, errhp, rowoff, iters, snap_in, snap_out */
/* set the execution mode to OCI_DESCRIBE_ONLY. Note that setting the mode to
OCI_DEFAULT does an implicit describe of the statement in addition to executing
the statement */
checkerr(errhp, OCIStmtExecute(svchp, stmthp, errhp, 0, 0,
(OCISnapshot *) 0, (OCISnapshot *) 0, OCI_DESCRIBE_ONLY));
/* Get the number of columns in the query */
checkerr(errhp, OCIAttrGet((dvoid *)stmthp, OCI_HTYPE_STMT, (dvoid *)&numcols,
(ub4 *)0, OCI_ATTR_PARAM_COUNT, errhp));
/* go through the column list and retrieve the datatype of each column. We
start from pos = 1 */
for (i = 1; i <= numcols; i++)
{
/* get parameter for column i */
checkerr(errhp, OCIParamGet((dvoid *)stmthp, OCI_HTYPE_STMT, errhp, (dvoid **)&colhd, i));
/* get data-type of column i */
type = 0;
checkerr(errhp, OCIAttrGet((dvoid *)colhd, OCI_DTYPE_PARAM,
(dvoid *)&type, (ub4 *)0, OCI_ATTR_DATA_TYPE, errhp));
}
[ 本帖最后由 ivhb 于 2007-7-10 09:17 编辑 ] |
|