xxxxxxxxxx
BEGIN TRANSACTION [Tran1]
BEGIN TRY
-- sql statements(update/insert )
COMMIT TRANSACTION [Tran1]
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION [Tran1]
SELECT ERROR_MESSAGE() AS ErrorMessage;
END CATCH
xxxxxxxxxx
BEGIN TRY
SELECT 1 / 0 AS Error;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
xxxxxxxxxx
# SQL Server
# Execute the query in try block
BEGIN TRY
DECLARE @result FLOAT;
SET @result = 1/0;
SELECT @result;
END TRY
# Specify what to return if an exception is occurred in the try block
BEGIN CATCH
SELECT 'Error occurred: ' + ERROR_MESSAGE();
END CATCH
# --------------------------------------------------------------------
# MySQL
BEGIN
DECLARE result FLOAT;
BEGIN
# Execute the query in try block
SET result = 1/0;
EXCEPTION
# Specify what to return if an exception is occurred in the try block
WHEN DIVISION_BY_ZERO THEN
SELECT 'Error occurred: Division by zero';
END;
END;
# --------------------------------------------------------------------
# PostgreSQL
DO $$
BEGIN
DECLARE result FLOAT;
BEGIN
# Execute the query in try block
result := 1/0;
EXCEPTION
# Specify what to return if an exception is occurred in the try block
WHEN division_by_zero THEN
RAISE EXCEPTION 'Error occurred: Division by zero';
END;
END $$;
xxxxxxxxxx
-- It is Transaction Control Language(TCL)
-- case1 (temporary)
BEGIN TRANSACTION
-- perform DML operation i.e. INSERT, UPDATE, DELETE
ROLLBACK -- goes to previous state
-- case2 (permanent)
BEGIN TRANSACTION
-- perform DML operation i.e. INSERT, UPDATE, DELETE
COMMIT -- Transaction is executed and hence change is done permanently