SQL - safely downcast BIGINT to INT

前端 未结 4 961
栀梦
栀梦 2020-12-11 03:13

I have a CSV I\'m importing into our database. One of the \"columns\" contains data that should be an INT but some rows have numbers that only fall in the BIGINT ra

4条回答
  •  一生所求
    2020-12-11 03:48

    I'm not sure this is the best answer but it is one I came up with earlier on my own. It is possible to catch the exception/error and gracefully continue execution.

    Example:

    DECLARE @UserIDBigInt BIGINT = 9723021913;
    DECLARE @UserID INT;
    BEGIN TRY
        SET @UserID = @UserIDBigInt;
    END TRY BEGIN CATCH
    END CATCH
    
    IF @UserID IS NULL BEGIN
        SELECT 'Handle it as unreliable data'
        RETURN
    END
    
    SELECT 'Handle it as reliable data'
    

提交回复
热议问题