How do I disable errors on string truncation in SQL Server?

北慕城南 提交于 2020-06-22 11:12:08

问题


How can I tell SQL Server not to raise an error if I insert or update a string longer than the size of the field - I would like silent truncation in this instance.


回答1:


The thing you have to do is set ANSI WARNINGS to OFF You can do that by calling

set ANSI_WARNINGS  OFF

I have also written a practical example:

create table bla(id varchar(2))
go

insert bla values ('123') --fails


set ANSI_WARNINGS  OFF

insert bla values ('123') --succeeds

Do remember to turn the ANSI warnings back ON when you are done. You can do so by calling:

set ANSI_WARNINGS ON



回答2:


Try casting the variable to the exact type and length before inserting it. That might do the trick. Casting (and converting) are much more flexible. :)



来源:https://stackoverflow.com/questions/528579/how-do-i-disable-errors-on-string-truncation-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!