title: How do I fix mismatching number of BEGIN and COMMIT statements?
body: <p>I am getting this error:</p>
  
  <blockquote>
    <p>Transaction count after EXECUTE indicates a mismatching number of
    BEGIN and COMMIT statements. Previous count = 2, current count = 3.</p>
  </blockquote>
  
  <p>But I don't know enough about SQL Server to stop the error.</p>
  
  <p>Here is my <code>DROP PROCEDURE</code> command:</p>
  
  <pre><code>--Specify database in which to uninstall procedure
  USE SalesLogix_Dev
  GO
  
  IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'usp_matt_db_tasks')
              AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
  DROP PROCEDURE usp_matt_db_tasks
  GO
  </code></pre>
  
  <p>And here is the <code>CREATE PROCEDURE</code>: </p>
  
  <pre><code>--Specify database in which to install procedure
  USE SalesLogix_Dev
  GO
  
  --Drop existing objects in order to guanrantee error-free install
  IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'usp_matt_db_tasks')
              AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
  DROP PROCEDURE usp_matt_db_tasks
  GO
  
  
  CREATE PROCEDURE usp_matt_db_tasks
      -- Add the parameters for the stored procedure here
  
  AS
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
      BEGIN TRANSACTION
  INSERT INTO [SalesLogix_Dev].[sysdba].[LEAD] (
      CREATEUSER,
      CREATEDATE,
      MODIFYUSER,
      MODIFYDATE,
      FIRSTNAME,
      ACCOUNTMANAGERID,
      ASSIGNDATE,
      COMPANY,
      COMPANY_UC,
      EMAIL,
      DONOTSOLICIT,
      ISPRIMARY,
      LEADSOURCEID,
      SECCODEID,
      STATUS,
      LASTNAME,
      LASTNAME_UC,
      INDUSTRY,
      NOTES,
      HOMEPHONE) 
  SELECT 
         ,'something'
         ,CURRENT_TIMESTAMP
         ,'something'    
         ,CURRENT_TIMESTAMP
         ,replace(firstname, '"', '')
         ,'something'
         ,CURRENT_TIMESTAMP
         ,replace(company, '"', '')
         ,replace(UPPER(company), '"', '')
         ,replace(email, '"', '')
         ,'1'
         ,'T'
         ,''
         ,'SYST00000001'
         ,'New'
         ,replace(lastname, '"', '')
         ,replace(UPPER(lastname), '"', '')
         ,replace(department, '"', '')
         ,replace(comments, '"', '')
         ,replace(phone, '"', '')
  
    FROM [SalesLogix_Dev].[sysdba].[CSVTemp]
  
  update  [SalesLogix_Dev].[sysdba].[LEAD] set LEAD_ADDRESSID = 'Q' + LEADID where DONOTSOLICIT = 1
  
  INSERT INTO [SalesLogix_Dev].[sysdba].[LEAD_ADDRESS] (
      LEAD_ADDRESSID,
      LEADID,
      CREATEUSER,
      CREATEDATE,
      MODIFYUSER,
      MODIFYDATE,
      ISMAILING,
      ISPRIMARY) 
  SELECT 
        LEAD_ADDRESSID
       ,LEADID
       ,'something'
       ,CURRENT_TIMESTAMP
       ,'something'      
       ,CURRENT_TIMESTAMP
       ,'T'
       ,'T'
  
    FROM [SalesLogix_Dev].[sysdba].[LEAD] where DONOTSOLICIT = 1
  
    update  [SalesLogix_Dev].[sysdba].[LEAD] set DONOTSOLICIT = 0 where DONOTSOLICIT = 1
    DROP TABLE [SalesLogix_Dev].[sysdba].[CSVTemp]
    ROLLBACK  TRANSACTION
  COMMIT TRANSACTION
  
  END
  </code></pre>
  
  <p>And finally I execute as follows:</p>
  
  <pre><code>USE SalesLogix_Dev
  GO
  
  EXEC usp_matt_db_tasks;
  </code></pre>