/*******************************************************************
Sample Program 10: Dynamic SQL Method 4 This program connects you to ORACLE using your username and
password, then prompts you for a SQL statement. You can enter
any legal SQL statement. Use regular SQL syntax, not embedded SQL.
Your statement will be processed. If it is a query, the rows
fetched are displayed.
You can enter multiline statements. The limit is 1023 characters.
This sample program only processes up to MAX_ITEMS bind variables and
MAX_ITEMS select-list items. MAX_ITEMS is #defined to be 40.
*******************************************************************/ #include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <sqlda.h>
#include <stdlib.h>
#include <sqlcpr.h> #define SQL_SINGLE_RCTX ((void *)0) /* Maximum number of select-list items or bind variables. */
#define MAX_ITEMS 40 /* Maximum lengths of the _names_ of the
select-list items or indicator variables. */
#define MAX_VNAME_LEN 30
#define MAX_INAME_LEN 30 #ifndef NULL
#define NULL 0
#endif /* Prototypes */
#if defined(__STDC__)
void sql_error(void);
int oracle_connect(void);
int alloc_descriptors(int, int, int);
int get_dyn_statement(void);
void set_bind_variables(void);
void process_select_list(void);
void help(void);
#else
void sql_error(/*_ void _*/);
int oracle_connect(/*_ void _*/);
int alloc_descriptors(/*_ int, int, int _*/);
int get_dyn_statement(/* void _*/);
void set_bind_variables(/*_ void -*/);
void process_select_list(/*_ void _*/);
void help(/*_ void _*/);
#endif char *dml_commands[] = {"SELECT", "select", "INSERT", "insert",
"UPDATE", "update", "DELETE", "delete"}; EXEC SQL INCLUDE sqlda;
EXEC SQL INCLUDE sqlca; EXEC SQL BEGIN DECLARE SECTION;
char dyn_statement[];
EXEC SQL VAR dyn_statement IS STRING();
EXEC SQL END DECLARE SECTION; SQLDA *bind_dp;
SQLDA *select_dp; /* Define a buffer to hold longjmp state info. */
jmp_buf jmp_continue; /* A global flag for the error routine. */
int parse_flag = ; void main()
{
int i; /* Connect to the database. */
if (oracle_connect() != )
exit(); /* Allocate memory for the select and bind descriptors. */
if (alloc_descriptors(MAX_ITEMS, MAX_VNAME_LEN, MAX_INAME_LEN) != )
exit(); /* Process SQL statements. */
for (;;)
{
(void) setjmp(jmp_continue); /* Get the statement. Break on "exit". */
if (get_dyn_statement() != )
break; /* Prepare the statement and declare a cursor. */
EXEC SQL WHENEVER SQLERROR DO sql_error(); parse_flag = ; /* Set a flag for sql_error(). */
EXEC SQL PREPARE S FROM :dyn_statement;
parse_flag = ; /* Unset the flag. */ EXEC SQL DECLARE C CURSOR FOR S; /* Set the bind variables for any placeholders in the
SQL statement. */
set_bind_variables(); /* Open the cursor and execute the statement.
* If the statement is not a query (SELECT), the
* statement processing is completed after the
* OPEN.
*/ EXEC SQL OPEN C USING DESCRIPTOR bind_dp; /* Call the function that processes the select-list.
* If the statement is not a query, this function
* just returns, doing nothing.
*/
process_select_list(); /* Tell user how many rows processed. */
for (i = ; i < ; i++)
{
if (strncmp(dyn_statement, dml_commands[i], ) == )
{
printf("\n\n%d row%c processed.\n", sqlca.sqlerrd[],
sqlca.sqlerrd[] == ? '\0' : 's');
break;
}
}
} /* end of for(;;) statement-processing loop */ /* When done, free the memory allocated for
pointers in the bind and select descriptors. */
for (i = ; i < MAX_ITEMS; i++)
{
if (bind_dp->V[i] != (char *) )
free(bind_dp->V[i]);
free(bind_dp->I[i]); /* MAX_ITEMS were allocated. */
if (select_dp->V[i] != (char *) )
free(select_dp->V[i]);
free(select_dp->I[i]); /* MAX_ITEMS were allocated. */
} /* Free space used by the descriptors themselves. */
SQLSQLDAFree(SQL_SINGLE_RCTX, bind_dp);
SQLSQLDAFree(SQL_SINGLE_RCTX, select_dp); EXEC SQL WHENEVER SQLERROR CONTINUE;
/* Close the cursor. */
EXEC SQL CLOSE C; EXEC SQL COMMIT WORK RELEASE;
puts("\nHave a good day!\n"); EXEC SQL WHENEVER SQLERROR DO sql_error();
return;
} int oracle_connect()
{
EXEC SQL BEGIN DECLARE SECTION;
VARCHAR username[];
VARCHAR password[];
EXEC SQL END DECLARE SECTION; printf("\nusername: ");
fgets((char *) username.arr, sizeof username.arr, stdin);
username.arr[strlen((char *) username.arr)-] = '\0';
username.len = (unsigned short)strlen((char *) username.arr); printf("password: ");
fgets((char *) password.arr, sizeof password.arr, stdin);
password.arr[strlen((char *) password.arr) - ] = '\0';
password.len = (unsigned short)strlen((char *) password.arr); EXEC SQL WHENEVER SQLERROR GOTO connect_error; EXEC SQL CONNECT :username IDENTIFIED BY :password; printf("\nConnected to ORACLE as user %s.\n", username.arr); return ; connect_error:
fprintf(stderr, "Cannot connect to ORACLE as user %s\n", username.arr);
return -;
} /*
* Allocate the BIND and SELECT descriptors using SQLSQLDAAlloc().
* Also allocate the pointers to indicator variables
* in each descriptor. The pointers to the actual bind
* variables and the select-list items are realloc'ed in
* the set_bind_variables() or process_select_list()
* routines. This routine allocates 1 byte for select_dp->V[i]
* and bind_dp->V[i], so the realloc will work correctly.
*/ alloc_descriptors(size, max_vname_len, max_iname_len)
int size;
int max_vname_len;
int max_iname_len;
{
int i; /*
* The first SQLSQLDAAlloc parameter is the runtime context. * The second parameter determines the maximum number of
* array elements in each variable in the descriptor. In
* other words, it determines the maximum number of bind
* variables or select-list items in the SQL statement.
*
* The third parameter determines the maximum length of
* strings used to hold the names of select-list items
* or placeholders. The maximum length of column
* names in ORACLE is 30, but you can allocate more or less
* as needed.
*
* The fourth parameter determines the maximum length of
* strings used to hold the names of any indicator
* variables. To follow ORACLE standards, the maximum
* length of these should be 30. But, you can allocate
* more or less as needed.
*/ if ((bind_dp =
SQLSQLDAAlloc(SQL_SINGLE_RCTX, size, max_vname_len, max_iname_len)) ==
(SQLDA *) )
{
fprintf(stderr,
"Cannot allocate memory for bind descriptor.");
return -; /* Have to exit in this case. */
} if ((select_dp =
SQLSQLDAAlloc (SQL_SINGLE_RCTX, size, max_vname_len, max_iname_len)) ==
(SQLDA *) )
{
fprintf(stderr,
"Cannot allocate memory for select descriptor.");
return -;
}
select_dp->N = MAX_ITEMS; /* Allocate the pointers to the indicator variables, and the
actual data. */
for (i = ; i < MAX_ITEMS; i++) {
bind_dp->I[i] = (short *) malloc(sizeof (short));
select_dp->I[i] = (short *) malloc(sizeof(short));
bind_dp->V[i] = (char *) malloc();
select_dp->V[i] = (char *) malloc();
} return ;
} int get_dyn_statement()
{
char *cp, linebuf[];
int iter, plsql; for (plsql = , iter = ; ;)
{
if (iter == )
{
printf("\nSQL> ");
dyn_statement[] = '\0';
} fgets(linebuf, sizeof linebuf, stdin); cp = strrchr(linebuf, '\n');
if (cp && cp != linebuf)
*cp = ' ';
else if (cp == linebuf)
continue; if ((strncmp(linebuf, "EXIT", ) == ) ||
(strncmp(linebuf, "exit", ) == ))
{
return -;
} else if (linebuf[] == '?' ||
(strncmp(linebuf, "HELP", ) == ) ||
(strncmp(linebuf, "help", ) == ))
{
help();
iter = ;
continue;
} if (strstr(linebuf, "BEGIN") ||
(strstr(linebuf, "begin")))
{
plsql = ;
} strcat(dyn_statement, linebuf); if ((plsql && (cp = strrchr(dyn_statement, '/'))) ||
(!plsql && (cp = strrchr(dyn_statement, ';'))))
{
*cp = '\0';
break;
}
else
{
iter++;
printf("%3d ", iter);
}
}
return ;
} void set_bind_variables()
{
int i, n;
char bind_var[]; /* Describe any bind variables (input host variables) */
EXEC SQL WHENEVER SQLERROR DO sql_error(); bind_dp->N = MAX_ITEMS; /* Initialize count of array elements. */
EXEC SQL DESCRIBE BIND VARIABLES FOR S INTO bind_dp; /* If F is negative, there were more bind variables
than originally allocated by SQLSQLDAAlloc(). */
if (bind_dp->F < )
{
printf ("\nToo many bind variables (%d), maximum is %d\n.",
-bind_dp->F, MAX_ITEMS);
return;
} /* Set the maximum number of array elements in the
descriptor to the number found. */
bind_dp->N = bind_dp->F; /* Get the value of each bind variable as a
* character string.
*
* C[i] contains the length of the bind variable
* name used in the SQL statement.
* S[i] contains the actual name of the bind variable
* used in the SQL statement.
*
* L[i] will contain the length of the data value
* entered.
*
* V[i] will contain the address of the data value
* entered.
*
* T[i] is always set to 1 because in this sample program
* data values for all bind variables are entered
* as character strings.
* ORACLE converts to the table value from CHAR.
*
* I[i] will point to the indicator value, which is
* set to -1 when the bind variable value is "null".
*/
for (i = ; i < bind_dp->F; i++)
{
printf ("\nEnter value for bind variable %.*s: ",
(int)bind_dp->C[i], bind_dp->S[i]);
fgets(bind_var, sizeof bind_var, stdin); /* Get length and remove the new line character. */
n = strlen(bind_var) - ; /* Set it in the descriptor. */
bind_dp->L[i] = n; /* (re-)allocate the buffer for the value.
SQLSQLDAAlloc() reserves a pointer location for
V[i] but does not allocate the full space for
the pointer. */ bind_dp->V[i] = (char *) realloc(bind_dp->V[i],
(bind_dp->L[i] + )); /* And copy it in. */
strncpy(bind_dp->V[i], bind_var, n); /* Set the indicator variable's value. */
if ((strncmp(bind_dp->V[i], "NULL", ) == ) ||
(strncmp(bind_dp->V[i], "null", ) == ))
*bind_dp->I[i] = -;
else
*bind_dp->I[i] = ; /* Set the bind datatype to 1 for CHAR. */
bind_dp->T[i] = ;
}
return;
} void process_select_list()
{
int i, null_ok, precision, scale; if ((strncmp(dyn_statement, "SELECT", ) != ) &&
(strncmp(dyn_statement, "select", ) != ))
{
select_dp->F = ;
return;
} /* If the SQL statement is a SELECT, describe the
select-list items. The DESCRIBE function returns
their names, datatypes, lengths (including precision
and scale), and NULL/NOT NULL statuses. */ select_dp->N = MAX_ITEMS; EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp; /* If F is negative, there were more select-list
items than originally allocated by SQLSQLDAAlloc(). */
if (select_dp->F < )
{
printf ("\nToo many select-list items (%d), maximum is %d\n",
-(select_dp->F), MAX_ITEMS);
return;
} /* Set the maximum number of array elements in the
descriptor to the number found. */
select_dp->N = select_dp->F; /* Allocate storage for each select-list item. SQLNumberPrecV6() is used to extract precision and scale
from the length (select_dp->L[i]). sqlcolumnNullCheck() is used to reset the high-order bit of
the datatype and to check whether the column
is NOT NULL. CHAR datatypes have length, but zero precision and
scale. The length is defined at CREATE time. NUMBER datatypes have precision and scale only if
defined at CREATE time. If the column
definition was just NUMBER, the precision
and scale are zero, and you must allocate
the required maximum length. DATE datatypes return a length of 7 if the default
format is used. This should be increased to
9 to store the actual date character string.
If you use the TO_CHAR function, the maximum
length could be 75, but will probably be less
(you can see the effects of this in SQL*Plus). ROWID datatype always returns a fixed length of 18 if
coerced to CHAR. LONG and
LONG RAW datatypes return a length of 0 (zero),
so you need to set a maximum. In this example,
it is 240 characters. */ printf ("\n");
for (i = ; i < select_dp->F; i++)
{
char title[MAX_VNAME_LEN];
/* Turn off high-order bit of datatype (in this example,
it does not matter if the column is NOT NULL). */
SQLColumnNullCheck (, (unsigned short *)&(select_dp->T[i]),
(unsigned short *)&(select_dp->T[i]), &null_ok); switch (select_dp->T[i])
{
case : /* CHAR datatype: no change in length
needed, except possibly for TO_CHAR
conversions (not handled here). */
break;
case : /* NUMBER datatype: use SQLNumberPrecV6() to
extract precision and scale. */
SQLNumberPrecV6( SQL_SINGLE_RCTX,
(unsigned long *)&(select_dp->L[i]), &precision, &scale);
/* Allow for maximum size of NUMBER. */
if (precision == ) precision = ;
/* Also allow for decimal point and
possible sign. */
/* convert NUMBER datatype to FLOAT if scale > 0,
INT otherwise. */
if (scale > )
select_dp->L[i] = sizeof(float);
else
select_dp->L[i] = sizeof(int);
break; case : /* LONG datatype */
select_dp->L[i] = ;
break; case : /* ROWID datatype */
select_dp->L[i] = ;
break; case : /* DATE datatype */
select_dp->L[i] = ;
break; case : /* RAW datatype */
break; case : /* LONG RAW datatype */
select_dp->L[i] = ;
break;
}
/* Allocate space for the select-list data values.
SQLSQLDAAlloc() reserves a pointer location for
V[i] but does not allocate the full space for
the pointer. */ if (select_dp->T[i] != )
select_dp->V[i] = (char *) realloc(select_dp->V[i],
select_dp->L[i] + );
else
select_dp->V[i] = (char *) realloc(select_dp->V[i],
select_dp->L[i]); /* Print column headings, right-justifying number
column headings. */ /* Copy to temporary buffer in case name is null-terminated */
memset(title, ' ', MAX_VNAME_LEN);
strncpy(title, select_dp->S[i], select_dp->C[i]);
if (select_dp->T[i] == )
if (scale > )
printf ("%.*s ", select_dp->L[i]+, title);
else
printf ("%.*s ", select_dp->L[i], title);
else
printf("%-.*s ", select_dp->L[i], title); /* Coerce ALL datatypes except for LONG RAW and NUMBER to
character. */
if (select_dp->T[i] != && select_dp->T[i] != )
select_dp->T[i] = ; /* Coerce the datatypes of NUMBERs to float or int depending on
the scale. */
if (select_dp->T[i] == )
if (scale > )
select_dp->T[i] = ; /* float */
else
select_dp->T[i] = ; /* int */
}
printf ("\n\n"); /* FETCH each row selected and print the column values. */
EXEC SQL WHENEVER NOT FOUND GOTO end_select_loop; for (;;)
{
EXEC SQL FETCH C USING DESCRIPTOR select_dp; /* Since each variable returned has been coerced to a
character string, int, or float very little processing
is required here. This routine just prints out the
values on the terminal. */
for (i = ; i < select_dp->F; i++)
{
if (*select_dp->I[i] < )
if (select_dp->T[i] == )
printf ("%-*c ",(int)select_dp->L[i]+, ' ');
else
printf ("%-*c ",(int)select_dp->L[i], ' ');
else
if (select_dp->T[i] == ) /* int datatype */
printf ("%*d ", (int)select_dp->L[i],
*(int *)select_dp->V[i]);
else if (select_dp->T[i] == ) /* float datatype */
printf ("%*.2f ", (int)select_dp->L[i],
*(float *)select_dp->V[i]);
else /* character string */
printf ("%-*.*s ", (int)select_dp->L[i],
(int)select_dp->L[i], select_dp->V[i]);
}
printf ("\n");
}
end_select_loop:
return;
} void help()
{
puts("\n\nEnter a SQL statement or a PL/SQL block at the SQL> prompt.");
puts("Statements can be continued over several lines, except");
puts("within string literals.");
puts("Terminate a SQL statement with a semicolon.");
puts("Terminate a PL/SQL block (which can contain embedded semicolons)");
puts("with a slash (/).");
puts("Typing \"exit\" (no semicolon needed) exits the program.");
puts("You typed \"?\" or \"help\" to get this message.\n\n");
} void sql_error()
{
/* ORACLE error handler */
printf ("\n\n%.70s\n",sqlca.sqlerrm.sqlerrmc);
if (parse_flag)
printf
("Parse error at character offset %d in SQL statement.\n",
sqlca.sqlerrd[]); EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK WORK;
longjmp(jmp_continue, );
}

1.实现描述区动态扩容

2.包含两个缓冲区:1)输出描述区(存储select结果集)2)输入描述区(存储sql语句)。

3.难点在于自实现动态扩容。

4.如何处理select语句 和 非select语句。---》二者的区别在于:应用程序是否能处理select结果集。

5.输出描述区:(保证存下返回的结果集,并一行一行的解析数据库返回的结果集)

这是一段对上面代码的精简;带注释。

 /* 包含C头文件 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <setjmp.h>
#include <sqlcpr.h> /* 包含SQLDA和SQLCA结构 */
#include <sqlda.h>
#include <sqlca.h> /* 定义绑定变量和选择列表项的最大个数 */
#define MAX_ITEMS 40 /* 定义绑定变量和选择列表项名称的最大长度 */
#define MAX_VNAME_LEN 30 /* 定义指示变量名称的最大长度 */
#define MAX_INAME_LEN 30 void connect();
void sql_error();
void alloc_descriptors(int , int , int);
void dealloc_descriptors();
void set_bind_variables();
void process_select_list(); /* 定义绑定描述区和选择描述区 */
SQLDA* bind_dp;
SQLDA* select_dp; /* 定义输入宿主变量:存放动态SQL语句 */
char sql_stat[];
char current_date[]; int main()
{
/* 安装错误处理句柄 */
exec sql whenever sqlerror do sql_error(); /* 连接到数据库 */
connect2(); /* 分配绑定描述区和选择描述区 */
alloc_descriptors(MAX_ITEMS , MAX_VNAME_LEN , MAX_INAME_LEN); for( ; ; )
{
printf("请输入动态SQL语句(exit:退出):");
gets(sql_stat); /* EXIT(exit):退出 */
if( == strncmp(sql_stat , "exit" , ) || == strncmp(sql_stat , "EXIT" , ))
break; /* 准备动态SQL语句 */
exec sql prepare s from :sql_stat; /* 定义游标 */
exec sql declare c cursor for s; /* 出错,继续下一循环 */
if( != sqlca.sqlcode)
continue; /* 设置绑定变量 */
set_bind_variables(); /*
* 打开游标
* select语句:生成结果集
* 其他SQL语句:执行语句
*/
exec sql open c using descriptor bind_dp; /*
* select语句
*/
if( == strncmp(sql_stat , "select" , ) || == strncmp(sql_stat , "SELECT" , ))
{
process_select_list();
}
/* 关闭游标 */
exec sql close c;
} /* 释放选择描述区和选择描述区 */
dealloc_descriptors(); /* 提交事务,断开连接 */
exec sql commit work release;
puts("谢谢使用Oracle动态SQL方法四!\n"); return ;
} void connect2()
{
/* 定义宿主变量 */
char username[] , password[] , server[]; /* 输入用户名、口令和网络服务名 */
printf("username:");
gets(username); printf("password: ");
gets(password); printf("server:");
gets(server); /* 连接到数据库 */
EXEC SQL CONNECT :username identified by :password using :server;
} void sql_error()
{
/* 显示SQL错误信息 */
printf("%.*s\n" , sqlca.sqlerrm.sqlerrml , sqlca.sqlerrm.sqlerrmc);
} //struct SQLDA {
// ub4 // int N; // Descriptor size in number of entries //
// text** // char **V; // Ptr to Arr of addresses of main variables //
// ub4* // int *L; // Ptr to Arr of lengths of buffers //
// sb2* // short *T; // Ptr to Arr of types of buffers //
// sb2** // short **I; // Ptr to Arr of addresses of indicator vars //
// sb4 // int F; // Number of variables found by DESCRIBE //
// text** // char **S; // Ptr to Arr of variable name pointers //
// ub2* // short *M; // Ptr to Arr of max lengths of var. names //
// ub2* // short *C; // Ptr to Arr of current lengths of var. names //
// text** // char **X; // Ptr to Arr of ind. var. name pointers //
// ub2* // short *Y; // Ptr to Arr of max lengths of ind. var. names//
// ub2* // short *Z; // Ptr to Arr of cur lengths of ind. var. names//
// }; void alloc_descriptors(int size , int max_vname_len , int max_iname_len)
{ int i; /* 分配绑定描述区和选择描述区 */
bind_dp = SQLSQLDAAlloc( , size , MAX_VNAME_LEN , MAX_INAME_LEN);
select_dp = SQLSQLDAAlloc( , size , MAX_VNAME_LEN , MAX_INAME_LEN); /* 为指示变量、绑定变量和选择列表项分配内存 */
for(i = ; i != MAX_ITEMS ; ++i)
{
bind_dp->I[i] = (short*)malloc(sizeof(short));
select_dp->I[i] = (short*)malloc(sizeof(short)); bind_dp->V[i] = (char*)malloc();
select_dp->V[i] = (char*)malloc();
}
} void dealloc_descriptors()
{
int i; /* 释放指示变量、绑定变量和选择列表项占用的内存 */
for(i = ; i != MAX_ITEMS ; ++i)
{
if(bind_dp->V[i] != (char*))
free(bind_dp->V[i]);
free(bind_dp->I[i]); if(select_dp->V[i] != (char*))
free(select_dp->V[i]);
free(select_dp->I[i]);
} /* 释放绑定描述区和选择描述区 */
SQLSQLDAFree( , bind_dp);
SQLSQLDAFree( , select_dp);
} void set_bind_variables()
{
int i;
char bind_var[]; /* 设置绑定变量最大个数 */
bind_dp->N = MAX_ITEMS; /* 绑定变量名称: 绑定描述区 */
exec sql describe bind variables for s into bind_dp; /* 设置绑定变量实际个数 */
bind_dp->N = bind_dp->F; /* 循环处理绑定变量 */
for(i = ; i != bind_dp->F ; ++i)
{
/* 显示绑定变量名 */
printf("请输入绑定变量%.*s的值:" , (int)bind_dp->C[i] , bind_dp->S[i]); /* 输入绑定变量的值 */
gets(bind_var); /* 设置绑定变量的长度成员 */
bind_dp->L[i] = strlen(bind_var); /* 为绑定变量数据缓冲区重新分配内存(多一位,留给'\0') */
bind_dp->V[i] = (char*)realloc(bind_dp->V[i] , bind_dp->L[i] + ); /* 绑定变量数据: 数据缓冲区 */
strcpy(bind_dp->V[i] , bind_var); /* 设置指示变量,处理NULL */
if( == strncmp(bind_var , "NULL" , ) || == strncmp(bind_var , "null" , ))
*bind_dp->I[i] = -;
else
*bind_dp->I[i] = ; /* 设置数据缓冲区数据类型代码->char */
bind_dp->T[i] = ;
}
} void process_select_list()
{
int i , null_ok , precision , scale;
char title[MAX_VNAME_LEN]; /* 设置选择列表项的最大个数 */
select_dp->N = MAX_ITEMS; /* 选择列表项: 选择描述区 */
exec sql describe select list for s into select_dp; /* 设置选择列表项的实际个数 */
select_dp->N = select_dp->F; /* 循环处理选择列表项 */
for(i = ; i != select_dp->F ; ++i)
{
/* 清除select_dp->T[i]的高位->null */
SQLColumnNullCheck( , (unsigned short*)&select_dp->T[i]
, (unsigned short*)&select_dp->T[i] , &null_ok); /* 根据内部数据类型确定外部类型数据长度(显示长度) */
switch(select_dp->T[i])
{
case :
/* number类型,取得精度与标度 */
//SQLNumberPrecV6(0 , (unsigned short*)&select_dp->T[i] , &precision , &scale);
SQLNumberPrecV6( , (unsigned long *)&select_dp->L[i] , &precision , &scale); //wangbaoming modify 201409 if(scale > )
/* 实数: 显示长度:float */
select_dp->L[i] = sizeof(float);
else
/* 整数: 显示长度 int */
select_dp->L[i] = sizeof(int);
break;
case :
/* DATA数据类型(DD-MON-YY) */
select_dp->L[i] = ;
break;
} /* 根据变量长度,重新为选择列表项数据缓冲区分配内存 */
if( != select_dp->T[i])
/* 其他类型 */
select_dp->V[i] = (char*)realloc(select_dp->V[i] , select_dp->L[i] + );
else
/* number类型 */
select_dp->V[i] = (char*)realloc(select_dp->V[i] , select_dp->L[i]); /* 初始化title */
memset(title , ' ' , MAX_VNAME_LEN); /* 选择列表项名称: title */
strncpy(title , select_dp->S[i] , select_dp->C[i]); /* 显示列名 */
if( == select_dp->T[i])
if(scale > )
printf("\t%.*s" , select_dp->L[i] + , title);
else
printf("\t%.*s" , select_dp->L[i] , title);
else
printf("\t%-.*s" , select_dp->L[i] , title); /* 根据Oracle内部类型确定外部数据类型(显示类型) */
if( == select_dp->T[i])
{
/* number 类型*/
if(scale > )
/* float */
select_dp->T[i] = ;
else
/* int */
select_dp->T[i] = ;
}
else
/* char */
select_dp->T[i] = ;
} printf("\n"); /* 提取数据完毕->结束循环 */
exec sql whenever not found do break; /* 循环处理选择列表数据 */
for( ; ; )
{
/* 数据->选择描述区 */
exec sql fetch c using descriptor select_dp; /* 显示数据 */
for( i = ; i != select_dp->F ; ++i)
{
if(*select_dp->I[i] < ){
/* 处理NULL */
printf("\tNULL");
}else{
if( == select_dp->T[i]) {
/* int */
printf("\t%d" , *(int*)select_dp->V[i]);
}else if( == select_dp->T[i]){
/* float */
printf("\t%8.2f" , *(float*)select_dp->V[i]);
}else{
/* char */
printf("\t%.*s" , select_dp->L[i] , select_dp->V[i]);
}
}
}
printf("\n");
}
}

Oracle 动态sql 实现方式的更多相关文章

  1. ORACLE的SQL JOIN方式大全

    ORACLE的SQL JOIN方式大全 在ORACLE数据库中,表与表之间的SQL JOIN方式有多种(不仅表与表,还可以表与视图.物化视图等联结),官方的解释如下所示 A join is a que ...

  2. ORACLE动态sql在存储过程中出现表或视图不存在的解决方法

    Oracle动态sql在存储过程中出现表或视图不存在的解决方法 CREATE OR REPLACE PROCEDURE P_test is strsql varchar2(2000); BEGIN   ...

  3. ORACLE的SQL JOIN方式小结

    在ORACLE数据库中,表与表之间的SQL JOIN方式有多种(不仅表与表,还可以表与视图.物化视图等联结),官方的解释如下所示 A join is a query that combines row ...

  4. Oracle 动态sql

    静态SQL是前置编译绑定,动态SQL是后期执行时才编译绑定. 场景: 动态SQL适用于表名及查询字段名未知的情况.在已知查询字段名及表名的情况下,使用动态SQL(字符串拼接方式)会增加硬解析的开销,在 ...

  5. Oracle动态SQL语句

    动态SQL返回游标: create or replace procedure proc_ValidityDueQuery( p_regioncode in number, p_pscode in nu ...

  6. Oracle 动态SQL 注意细节 ORA-00911: 无效字符

    随笔 - 46  文章 - 92  评论 - 5   lv_sql:='  insert into ETL_SUCESS_AMOUNT  select SEQ_OS_ETL_AMOUNTID.NEXT ...

  7. Oracle 动态sql小例子

    错误写法: create or replace procedure testproce20130228issqlstr varchar2(8000);date1 varchar2(10);begins ...

  8. [转]ORACLE 动态执行SQL语句

    本文转自:http://zhaisx.iteye.com/blog/856472 Oracle 动态SQLOracle 动态SQL有两种写法:用 DBMS_SQL 或 execute immediat ...

  9. 在Oracle中执行动态SQL的几种方法

    转载:在Oracle中执行动态SQL的几种方法 以下为内容留存: 在Oracle中执行动态SQL的几种方法 在一般的sql操作中,sql语句基本上都是固定的,如:SELECT t.empno,t.en ...

随机推荐

  1. Java常用小笔记

    1.对list集合进行分页 //startIndex指的是开始的小标 从0开始,pageSize是每页记录数 int toIndex = new Integer(startIndex)+new Int ...

  2. CentOS 6.9下KVM虚拟机通过virt-clone克隆虚拟机(转)

    一.virt-clone概述 1.virt-clone作用简介 virt-clone主要是用来克隆kvm虚拟机,并且通过Options.General Option.Storage Configura ...

  3. Matlab横坐标从特定值开始

    set(gca,'XTick',1:1:length(x)); set(gca,'XTickLabel',{'15','20','25','30','35','40','45','50','55',' ...

  4. latex 三个不同的图放在一行且每个图都有注释

    \begin{figure}[htbp] \begin{minipage}[t]{0.3\linewidth} \centering \includegraphics[width=.2.0.eps} ...

  5. mac清理磁盘方法

    1.清除缓存文件 cd ~/Library/Caches/ rm -rf ~/Library/Caches/* 2.删除所有系统日志 sudo rm -rf /private/var/log/*3.删 ...

  6. unity quaternion vector

    做脚印呢 做了曲面细分和decal两种 先用正交camera生成 高度图 采样uv由pos 从world到camera space生成 unity对tessellation的支持限制还是比较大的 只能 ...

  7. Linux 调度器发展简述

    引言 进程调度是操作系统的核心功能.调度器只是是调度过程中的一部分,进程调度是非常复杂的过程,需要多个系统协同工作完成.本文所关注的仅为调度器,它的主要工作是在所有 RUNNING 进程中选择最合适的 ...

  8. java中return的作用

    脑补一下基础的东西,return的用法. return的常用作用有以下两种第一种用法是返回参数所用的关键字,假如一个有返回值的方法执行完了之后需要返回一个参数,示例: public string fu ...

  9. IE测试CSS兼容性测试

    我们知道IE6~8是现在浏览器的主流.但是由IE6开始,我们已经知道IE并不是完全执行W3C标准.我们在编程的时候往往遇到只兼容某一种浏览器. 我们以前经常使用IE Test进行IE的兼容性测试.但是 ...

  10. 如何在 MyEclipse 下用 Gradle 构建 Web Application

    一.安装 Gradle 插件,参考我的另一篇文章:MyEclipse 集成 Gradle开发环境 二.新建一个 Web Project, 在项目根目录下新建 build.gradle 文件, 并向其中 ...