返回首页

gbase数据、南大通用产品文档:GBase8cRAISE 语法

更新日期:2024年09月11日

有以下五种语法格式:
图1 raise_format::=
图17-11 raise_format::=
图17-12 raise_condition::=
图17-13 raise_sqlstate::=

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
717
图17-14 raise_option::=
图17-15 raise::=
参数说明:

level 选项用于指定错误级别,有DEBUG,LOG,INFO,NOTICE,WARNING 以及
EXCEPTION(默认值)。EXCEPTION 抛出一个正常终止当前事务的异常,其他的仅
产生不同异常级别的信息。
特殊级别的错误信息是否报告到客户端、
写到服务器日志由
log_min_messages 和client_min_messages 这两个配置参数控制。

format:格式字符串,指定要报告的错误消息文本。格式字符串后可跟表达式,用于向
消息文本中插入。在格式字符串中,%由format 后面跟着的参数的值替换,%%用于打
印出%。例如:
--v_job_id 将替换字符串中的%:
RAISE NOTICE 'Calling cs_create_job(%)',v_job_id;

option = expression:向错误报告中添加另外的信息。关键字option 可以是MESSAGE、
DETAIL、HINT 以及ERRCODE,并且每一个expression 可以是任意的字符串。

MESSAGE,指定错误消息文本,这个选项不能用于在USING 前包含一个格式字
符串的RAISE 语句中。

DETAIL,说明错误的详细信息。

HINT,用于打印出提示信息。

ERRCODE,向报告中指定错误码(SQLSTATE)。可以使用条件名称或者直接用
五位字符的SQLSTATE 错误码。

condition_name:错误码对应的条件名。

sqlstate:错误码。
如果在RAISE EXCEPTION 命令中既没有指定条件名也没有指定SQLSTATE,默认用

GBase 8c V5 开发者手册
南大通用数据技术股份有限公司
718
RAISE EXCEPTION (P0001)。如果没有指定消息文本,默认用条件名或者SQLSTATE 作为
消息文本。
须知:- 当由SQLSTATE 指定了错误码,则不局限于已定义的错误码,可以选
择任意包含五个数字或者大写的ASCII 字母的错误码,
而不是00000。建议避免使用以三个
0结尾的错误码,
因为这种错误码是类别码,
会被整个种类捕获。- 兼容O模式下,
SQLCODE
等于SQLSTATE。
说明:图17-15 所示的语法不接任何参数。这种形式仅用于一个BEGIN 块中的
EXCEPTION 语句,它使得错误重新被处理。

功能
这个参数用于设置是否把insert into
select…转化成insert into values 的方式。
参数取值含义说明

参数= 0:不转换;

参数= 1:转换。
该参数的默认值是0。
表6- 15 参数值范围说明表
默认值
最小值
最大值
0
0
1

rsetnull() 函数将 C 变量设置为对应于数据库空值的值。

语法
mint rsetnull(type, ptrvar)
mint type;

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 894 -
char *ptrvar;
type
对应于 C 或 GBase 8s ESQL/C 变量的数据类型的 mint。此 type 可为除
了 var binary 或 lvarchar 指针变量之外的任何数据类型。
ptrvar
指向 C 或 GBase 8s ESQL/C 变量的指针。

用法

rsetnull() 函数将除了 var
binary 和 lvarchar 指针主变量之外的任何
数据类型的 GBase
8s
ESQL/C 变量设置为空。
要将 var
binary 或 lvarchar 指
针主变量设置为空,请使用 ifx_var_setnull() 宏。

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

This program fetches rows from the stock table for a chosen manufacturer and allows the
user to set the unit_price to NULL.
*/

#include
#include
EXEC SQL include decimal;
EXEC SQL include sqltypes;

#define WARNNOTIFY 1
#define NOWARNNOTIFY 0

#define LCASE(c) (isupper(c) ? tolower(c) : (c))

char format[] = "($$,$$$,$$$.&&)";

main()

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 895 -
{
char decdsply[20];
char ans;
int4 ret, exp_chk();

EXEC SQL BEGIN DECLARE SECTION;
short stock_num;
char description[16];
dec_t unit_price;
char manu_code[4];
EXEC SQL END DECLARE SECTION;

printf("RSETNULL Sample ESQL Program running.\n\n");
EXEC SQL connect to 'stores7'; /* connect to stores7 */
exp_chk("Connect to stores7", NOWARNNOTIFY);

printf("This program selects all rows for a given manufacturer\n");
printf("from the stock table and allows you to set the unit_price\n");
printf("\nTo begin, enter a manufacturer code - for example: 'HSK'\n");
printf("\nEnter Manufacturer code: ");
/* prompt for mfr. code */
gets(manu_code); /* get mfr. code */
EXEC SQL declare upcurs cursor for /* declare cursor */
select stock_num, description, unit_price from stock
where manu_code = :manu_code
for update of unit_price;
rupshift(manu_code); /* Make mfr code upper case */
EXEC SQL open upcurs; /* open select cursor */
if(exp_chk("Open cursor", WARNNOTIFY) == 1)
exit(1);

/*
* Display Column Headings

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 896 -
*/
printf("\nStock # \tDescription \t\tUnit Price");
while(1)
{
/* get a row */
EXEC SQL fetch upcurs into :stock_num, :description, :unit_price;
if ((ret = exp_chk("fetch", WARNNOTIFY)) == 100)
/* if end of rows */
break;
if(ret == 1)
exit(1);
if(risnull(CDECIMALTYPE, (char *) &unit_price))
/* unit_price NULL? */
continue; /* skip to next row */
rfmtdec(&unit_price, format, decdsply);
/* format unit_price */
/* display item */
printf("\n\t%d\t%15s\t%s", stock_num, description, decdsply);
ans = ' ';
/* Set unit_price to NULL? y(es) or n(o) */
while((ans = LCASE(ans)) != 'y' && ans != 'n')
{
printf("\n. . . Set unit_price to NULL ? (y/n) ");
scanf("%1s", &ans);
}
if (ans == 'y') /* if yes, NULL to unit_price */
{
rsetnull(CDECIMALTYPE, (char *) &unit_price);
EXEC SQL update stock set unit_price = :unit_price
where current of upcurs; /* and update current row */
if(exp_chk("UPDATE", WARNNOTIFY) == 1)
exit(1);
}

GBase 8s ESQL/C 编程指南
南大通用数据技术股份有限公司
- 897 -
}
printf("\nRSETNULL Sample Program over.\n\n");
}

/*
* The exp_chk() file contains the exception handling functions to check 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

输出
RSETNULL Sample ESQL Program running.

This program selects all rows for a given manufacturer
from the stock table and allows you to set the unit_price
to NULL.

To begin, enter a manufacturer code - for example: 'HSK'

Enter Manufacturer code: HSK

Stock # Description Unit Price
1 baseball gloves $800.00
. . . Set unit_price to NULL ? (y/n) n

3 baseball bat $240.00
. . . Set unit_price to NULL ? (y/n) y

4 football $960.00
. . . Set unit_price to NULL ? (y/n) n

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

110 helmet $600.00
. . . Set unit_price to NULL ? (y/n) y

RSETNULL Sample Program over.