首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么让sql自动截断字符串,不用提示异常

2012-01-14 
如何让sql自动截断字符串,不用提示错误?如何让sql自动截断字符串,不用提示错误?示例如下createtablet_test

如何让sql自动截断字符串,不用提示错误?
如何让sql自动截断字符串,不用提示错误?
示例如下
create   table   t_test(id   int   identity(1,1),item_code   varchar(10)   null,item_name   varchar(10)   null)
INSERT   INTO   t_test   (   item_code,   item_name   )   VALUES   (   '0123456789abc ',   'zdf '   )
出错信息如下:
服务器:   消息   8152,级别   16,状态   9,行   1
将截断字符串或二进制数据。
语句已终止。

是否可以做到让sql自动截断字符串,不用提示错误,字符超长时,自动截断字符串,如上,item_code   只记录0123456789即可


[解决办法]
请正确设置item_code 的长度
[解决办法]
create trigger tr_
for instead of insert
as
if exists (select * from inserted where len(xxxxx)> N)
insert tablename select xxx,xxxx,left(xxxxx,N) from inserted
else
insert tablename select xxx,xxxx,xxxxx from inserted
[解决办法]
好象不行,把长度加大.

或使用left截取左十位.

示例如下
create table t_test(id int identity(1,1),item_code varchar(10) null,item_name varchar(10) null)
INSERT INTO t_test ( item_code, item_name ) VALUES ( left( '0123456789abc ',10), 'zdf ' )

[解决办法]
lang8134(heaton) ,你的方法不行,也会提示的。
[解决办法]
原以为trigger可以,试了下,也不行。

热点排行