返回首页

gbase数据、南大通用产品文档:GBase8srtypwidth() 函数

更新日期:2024年09月11日

当您将带有 SQL 数据类型的值转换为字符数据类型时,rtypwidth() 函数返回字符数
据类型需要避免截断的最小字符数。


GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 923 -

语法
mint rtypwidth(sqltype, sqllen)
mint sqltype;
mint sqllen;

sqltype
SQL 数据类型的整数代码。
sqllen
对于指定的 SQL 数据类型,在数据文件中的字节数。

用法

随同 DESCRIBE 语句初始化的 sqlda,提供 rtypwidth() 函数来使用。sqltype 和
sqllen 组件对应于每一 sqlda.sqlvar 结构中同名的组件。

返回代码
0
sqltype 不是有效的 SQL 数据类型。
>0
返回值是 sqltype 数据类型要求的最小字符数。

示例

此样例程序在 demo 目录中的 rtypwidth.ec 文件中。
/*
* rtypwidth.ec *

This program displays the name of each column in the 'orders' table and the number of
characters required to store the column when the data type is converted to characters.
*/

#include


GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 924 -

#define WARNNOTIFY 1
#define NOWARNNOTIFY 0

main(argc, argv)
int argc;
char *argv[];
{
mint i, numchars;

int4 exp_chk();
char db_stmnt[50];
struct sqlda *sql_desc;
struct sqlvar_struct *col;

EXEC SQL BEGIN DECLARE SECTION;
char db_name[20];
EXEC SQL END DECLARE SECTION;

printf("RTYPWIDTH Sample ESQL Program running.\n\n");

if (argc > 2) /* correct no. of args? */
{
printf("\nUsage: %s [database]\nIncorrect no. of argument(s)\n",argv[0]);
exit(1);
}
strcpy(db_name, "stores7");
if (argc == 2)
strcpy(db_name, argv[1]);

EXEC SQL connect to :db_name;
sprintf(db_stmnt, "CONNECT TO %s", argv[1]);
exp_chk(db_stmnt, NOWARNNOTIFY);


GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 925 -

printf("Connected to %s\n", db_name);
EXEC SQL prepare query_1 from 'select * from orders';
/* prepare select */
if(exp_chk("Prepare", WARNNOTIFY) == 1)
exit(1);
EXEC SQL describe query_1 into sql_desc;
/* setup sqlda */
if(exp_chk("Describe", WARNNOTIFY) == 1)
exit(1);
printf("\n\tColumn Name \t# chars\n");

/*
* For each column in orders print the column name and the minimum number of
characters required to convert the SQL type to a character data type
*/
for (i = 0, col = sql_desc->sqlvar; i < sql_desc->sqld; i++, col++)
{
numchars = rtypwidth(col->sqltype, col->sqllen);
printf("\t%-15s\t%d\n", col->sqlname, numchars);
}

printf("\nRTYPWIDTH Sample Program over.\n\n");
}

/*
The exp_chk() file contains the exception handling functions to
heck the SQLSTATE status variable to see if an error has occurred following an SQL
statement. If a warning or an error has occurred, exp_chk() executes the GET DIAGNOSTICS
statement and prints the detail for each exception that is returned.
*/
EXEC SQL include exp_chk.ec
输出

RTYPWIDTH Sample ESQL Program running.

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 926 -


Connected to stores7

Column Name # chars
order_num 11
order_date 10
customer_num 11
ship_instruct 40
backlog 1
po_num 10
ship_date 10
ship_weight 10
ship_charge 9
paid_date 10

RTYPWIDTH Sample Program over.

GBase 8a 单机
GBase 8a 集群

DECLARE
v_last_name VARCHAR2(25);
v_emp_id NUMBER(6) := 120;

GBase 8s PL/SQL手册
南大通用数据技术股份有限公司
- 58 -

BEGIN
<>
SELECT last_name INTO v_last_name
FROM employees
WHERE employee_id = v_emp_id;
BEGIN
DBMS_OUTPUT.PUT_LINE (v_last_name);
v_emp_id := v_emp_id + 5;
IF v_emp_id < 120 THEN
GOTO get_name;
END IF;
END;
END;

--Result:
--Weiss