开发者

SQL - How to check overflow of data type

I would like to check for an overflow of a specific data type of column in SQL database. Here is the text that I need put into my SQL:

"overflow happened when the value is out of the interval of a column's data type"

开发者_StackOverflow中文版


Something like this would work for SQL Server...

DECLARE @userInput NVarChar(100) = '129387918279387987123123';
DECLARE @value Int;

BEGIN TRY
  SELECT @value = CONVERT(Int, @userInput);
END TRY
BEGIN CATCH
  PRINT 'Overflow happend when the value is out of interval of column''s data type';
END CATCH;


In a database application, there isn't really any good way to recover automatically (to recover in code) from overflow. You have to rollback the transaction.

So you're going to have to trap the overflow error the server raises, and then either rollback or raise that error again so some other module can rollback. I really don't see the point in doing that, but I might be missing something.

Am I missing something?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜