0
votes

I'm getting:

Msg 8115 Arithmetic overflow error converting expression to data type int error

For this query:

DECLARE @x  int     
IF EXISTS ( SELECT * FROM Y)     
  BEGIN          
    SET @x = (SELECT max(Id )  from Y)+1    
  END    
ELSE 
  BEGIN         
    SET @x = 1   
  END 

I tried to cast Id to Big int and I also tried by changing x as Big int, but still getting same error.

2
Can you try declare @x bigint - TheGameiswar
mysql or sql server? - Sankar
I tried with @x bigint but no use - antara
@aks Did you try using the IDENTITY(1,1) for auto increment of ID column? - Techie
Is there possibility the max(Id) is the maximum value for the bigint type? - owczarek

2 Answers

1
votes

I have tried your code nothing is wrong with it, please refer this LINK it may help you.

0
votes

Check the ranges of values that can be stored within the types:

https://msdn.microsoft.com/en-us/library/ms187746(v=SQL.90).aspx

https://msdn.microsoft.com/en-us/library/ms187745.aspx

(and also check this: SQL Server BIGINT or DECIMAL(18,0) for primary key)

The ID column of the decimal type may cause problem in your case.