title: Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT?
body: <p>Question: I have this SQL script:</p>
  
  <pre><code>DECLARE @in_TE_UID varchar(36) 
  DECLARE @in_ZO_BETE_Sort int 
  DECLARE @in_user int 
  
  
  SET @in_TE_UID = '9f510440-8828-44ce-bbea-6bc866902262'
  SET @in_ZO_BETE_Sort = 0 
  SET @in_user = 12435
  
  -- http://stackoverflow.com/questions/884334/tsql-try-catch-transaction-in-trigger
  BEGIN TRANSACTION BeforeUpdate;
  BEGIN TRY
      IF NOT EXISTS 
      (
          SELECT 1 FROM T_FMS_ZO_Benutzer_TeaserOrder 
          WHERE (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_Status = 1) 
          AND (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_BE_ID = @in_user) 
          AND 
          (
              T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID = @in_TE_UID 
              OR 
              (
                  @in_TE_UID IS NULL 
                  AND 
                  T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID IS NULL 
              ) 
          )    
      )
      BEGIN
          INSERT INTO T_FMS_ZO_Benutzer_TeaserOrder 
          (
               ZO_BETE_UID
              ,ZO_BETE_BE_ID
              ,ZO_BETE_TE_UID
              ,ZO_BETE_Sort
              ,ZO_BETE_Status
          )
          VALUES
          (
               NEWID() --&lt;ZO_BETE_UID, uniqueidentifier,&gt;
              ,@in_user --&lt;ZO_BETE_BE_ID, int,&gt;
              ,@in_TE_UID --&lt;ZO_BETE_TE_UID, uniqueidentifier,&gt;
              ,@in_ZO_BETE_Sort --&lt;ZO_BETE_Sort, int,&gt;
              ,1 --&lt;ZO_BETE_Status, int,&gt;
          )
          ;
      END
      ELSE
      BEGIN
  
          UPDATE T_FMS_ZO_Benutzer_TeaserOrder  
              SET  ZO_BETE_Sort = @in_ZO_BETE_Sort 
  
          WHERE (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_Status = 1) 
          AND (T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_BE_ID = @in_user) 
          AND 
          (
              T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID = @in_TE_UID 
              OR 
              (
                  @in_TE_UID IS NULL 
                  AND 
                  T_FMS_ZO_Benutzer_TeaserOrder.ZO_BETE_TE_UID IS NULL 
              ) 
          ) 
          ;
  
      END
  
  END TRY
  BEGIN CATCH
      IF @@TRANCOUNT &gt; 0
          ROLLBACK TRANSACTION BeforeUpdate --RollBack in case of Error
  
      --RETURN
  
  
      DECLARE @ErrorMessage NVARCHAR(4000);
      DECLARE @ErrorSeverity INT;
      DECLARE @ErrorState INT;
  
      SELECT 
          @ErrorMessage = ERROR_MESSAGE(),
          @ErrorSeverity = ERROR_SEVERITY(),
          @ErrorState = ERROR_STATE();
  
      -- Use RAISERROR inside the CATCH block to return error
      -- information about the original error that caused
      -- execution to jump to the CATCH block.
      RAISERROR (@ErrorMessage, -- Message text.
                 @ErrorSeverity, -- Severity.
                 @ErrorState -- State.
      );
  END CATCH
  </code></pre>
  
  <p>It runs fine when I run it once.</p>
  
  <p>If afterwards I run a </p>
  
  <pre><code>SELECT * FROM T_FMS_ZO_Benutzer_TeaserOrder
  </code></pre>
  
  <p>then it timeouts.</p>
  
  <p>If I add <code>with (nolock)</code> then it works.</p>
  
  <p>If I look at the processes in SSMS, I see a locked select with <code>LCK_M_S</code>.</p>
  
  <p>If I execute the same command in code, I always get this error:</p>
  
  <blockquote>
    <p><em>Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1</em></p>
  </blockquote>
  
  <p>So really, what's wrong with this code ?</p>