joseph
joseph

Reputation: 21

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"

Upvotes: 2

Views: 2055

Answers (2)

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?

Upvotes: 1

Yuck
Yuck

Reputation: 50855

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;

Upvotes: 2

Related Questions