在Derby中自动将VARCHAR截断为特定长度 [英] Truncate a VARCHAR to specific length in Derby AUTOMATICALLY

查看:271
本文介绍了在Derby中自动将VARCHAR截断为特定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用SQL在Derby中自动将VARCHAR截断为表字段长度?

How can I truncate a VARCHAR to the table field length AUTOMATICALLY in Derby using SQL?

具体来说:

CREATE TABLE A ( B VARCHAR(2) );
INSERT INTO A B VALUES ('1234');

将抛出SQLException:

would throw a SQLException:

A truncation error was encountered trying to shrink VARCHAR '123' to length 2.

是否有一种简便的方法来抑制此异常?

Is there a easy way to suppress this exception?

推荐答案

否。您应在检查元数据后将其切掉。或者,如果您不想每次都检查元数据,则必须使代码和数据库保持同步。但这没什么大不了的,这是验证程序的常规做法。

No. You should chop it off after checking the meta-data. Or if you don't wanna check the meta-data everytime, then you must keep both your code and database in sync. But thats not a big deal, its a usual practice in validators.

这篇关于在Derby中自动将VARCHAR截断为特定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆