-2

Please help me how to solve it. why is it throwing this error as my script is inserting data into various tables extracted from an environment.

#376039 is the catch block where rollback and other raising error is happening

2
  • Try extracting, say five lines before and after the mentioned location from the script. The error says that line is 376 039, so the script should have more than 350 000 lines, right?
    – vonPryz
    Mar 8 at 6:54
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Mar 8 at 7:53

1 Answer 1

1

Look for a RAISERROR statement around line #376,039 of your script.

Most likely, you are providing invalid error number 515 in a variable:

DECLARE @error integer = 515;
RAISERROR (@error, 16, 1);
Msg 2732, Level 16, State 1, Line 26
Error number 515 is invalid. 
The number must be from 13000 through 2147483647 and it cannot be 50000.

You'll need to debug the script to determine how that error number is ending up in a RAISERROR statement.

Error 515 is a system error with the following template text:

Cannot insert the value NULL into column '%.*ls', table '%.*ls'; 
column does not allow nulls. %ls fails.

So, the root cause will be trying to insert NULL into a column that doesn't allow NULL. You could help localise the problem by printing the ERROR_MESSAGE() inside the CATCH before the RAISERROR. The ERROR_LINE() function might also be useful.

It is generally better to use the error message rather than the error number with RAISERROR. See Error and Transaction Handling in SQL Server by Erland Sommarskog for detailed information.

From that article:

Here is a stored procedure that showcases how you should work with errors and transactions.

CREATE PROCEDURE insert_data @a int, @b int AS 
  SET XACT_ABORT, NOCOUNT ON
  BEGIN TRY
     BEGIN TRANSACTION
     INSERT sometable(a, b) VALUES (@a, @b)
     INSERT sometable(a, b) VALUES (@b, @a)
     COMMIT TRANSACTION
  END TRY
  BEGIN CATCH
     IF @@trancount > 0 ROLLBACK TRANSACTION
     DECLARE @msg nvarchar(2048) = error_message()  
     RAISERROR (@msg, 16, 1)
     RETURN 55555
  END CATCH
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.