动态SQL中DESCRIPTOR的应用
王光红
动态SQL具有的灵活性是众所周知的,本人在此介绍一种应用DESCRIPTOR的动态SQL:
$short b_num;
$char *command;
$char Sql[1024];
$short flag;
$date Date;
$long Number, b_count, j;
$long prec;
$long type;
$long scale;
$double money;
sprintf(Sql, "select * from table");
$prepare qid from $Sql;
if(SQLCODE) return -1;
$declare BROWSE scroll cursor for qid;
if(SQLCODE) return -2;
$allocate descriptor 'browsdesc' with max :b_num; //在描述区分配空间,并设置最大的项目数
$get descriptor 'browsdesc' :b_count=count;//得到查询的字段个数
if(SQLCODE){
SqlErrHandle(SQLCODE, SELECT);
goto EXIT0;
}
$fetch BROWSE using sql descriptor 'browsdesc';
if(SQLCODE){
strcpy(RetPacket.data, "SELECT ERROR|");
SqlErrHandle(SQLCODE, SELECT);
goto EXIT0;
}
for(j=1;j<=b_count;j++){
$get descriptor 'browsdesc' value $j //当前字段序号
$prec=precision, /*money、decimal*/
$scale=scale, /*money、decimal*/
$type=type;
if(SQLCODE){
SqlErrHandle(SQLCODE, SELECT);
goto EXIT0;
}
switch(type){
case SQLFLOAT:
case SQLSMFLOAT:
case SQLDECIMAL:
case SQLMONEY:
if(prec)
sprintf(fmt, "%s%d.%df\0", "%",prec+1, scale);
else
strcpy(fmt, "%f");
$get descriptor 'browsdesc' value $j
$money=data;
sprintf(result, fmt, money);
if(SQLCODE==DATAISNULL) result[0]=0;
break;
default:
$get descriptor 'browsdesc' value $j
$result=data;
}//switch
if(SQLCODE==DATAISNULL) SQLCODE=0;
if(SQLCODE){
SqlErrHandle(SQLCODE, SELECT);
goto EXIT0;
}
OutPut(result);//输出结果
}//for