日期:2014-05-18  浏览次数:20620 次

如何让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可以,试了下,也不行。