A basic template for creating transactions in SQL Server.

--
-- Example 1
-- ----------------------------------------------------------------------------

begin try
    begin transaction;

    -- Code goes here

    commit transaction;
end try
begin catch
    if @@TRANCOUNT > 0
        begin
            rollback transaction
        end;

    declare @ERROR_SEVERITY int,
        @ERROR_STATE int,
        @ERROR_NUMBER int,
        @ERROR_LINE int,
        @ERROR_MESSAGE nvarchar(4000);

    select @ERROR_SEVERITY = ERROR_SEVERITY(),
        @ERROR_STATE = ERROR_STATE(),
        @ERROR_NUMBER = ERROR_NUMBER(),
        @ERROR_LINE = ERROR_LINE(),
        @ERROR_MESSAGE = ERROR_MESSAGE();

    raiserror('Msg %d, Line %d, :%s',
        @ERROR_SEVERITY,
        @ERROR_STATE,
        @ERROR_NUMBER,
        @ERROR_LINE,
        @ERROR_MESSAGE
    );
end catch;


--
-- Example 2
-- Source: https://github.com/readyroll/sql-server-samples/blob/master/samples/databases/wide-world-importers/readyroll/WideWorldImporters/Programmable%20Objects/Website/Stored%20Procedures/InsertCustomerOrders.sql
-- ----------------------------------------------------------------------------

CREATE PROCEDURE [Website].[InsertCustomerOrders]
    @OrdersCreatedByPersonID int,
    @SalespersonPersonID int
WITH EXECUTE AS OWNER
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    BEGIN TRY
        BEGIN TRAN;

        -- ... insert statement here...

        COMMIT;
    END TRY

    BEGIN CATCH
        IF XACT_STATE() <> 0 ROLLBACK TRAN;
        PRINT N'Unable to create the customer orders.';
        THROW;
        RETURN -1;
    END CATCH;

    RETURN 0;
END;
GO