SoFunction
Updated on 2025-03-09

Error handling in SQL SERVER (try catch)

BEGIN TRY
……
END TRY
BEGIN CATCH
……
END CATCH。
In addition, if there are other SQL statements in front of the WITH statement, you should add a semicolon ";" at the end of the previous SQL statement. For example, in this TRY CATCH, you should add a ";" in front of it, as follows:
BEGIN TRY

WITH w AS(
SELECT f1,f2,f3
,ROW_NUMBER() OVER(ORDER BY Id DESC) AS Row
FROM [t1]
WHERE Code=@Code
)
INSERT INTO [t2](
f1,f2,f3
SELECT f1,f2,f3
FROM w WHERE Row>100;
END TRY
BEGIN CATCH
END CATCH;