1

質問: 次の SQL スクリプトがあります。

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() --<ZO_BETE_UID, uniqueidentifier,>
            ,@in_user --<ZO_BETE_BE_ID, int,>
            ,@in_TE_UID --<ZO_BETE_TE_UID, uniqueidentifier,>
            ,@in_ZO_BETE_Sort --<ZO_BETE_Sort, int,>
            ,1 --<ZO_BETE_Status, int,>
        )
        ;
    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 > 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

1回実行すると問題なく動作します。

その後、私が

SELECT * FROM T_FMS_ZO_Benutzer_TeaserOrder

その後、タイムアウトします。

追加するwith (nolock)と動作します。

SSMS のプロセスを見ると、ロックされた選択がLCK_M_S.

コードで同じコマンドを実行すると、常に次のエラーが発生します。

EXECUTE 後のトランザクション数は、BEGIN ステートメントと COMMIT ステートメントの数が一致していないことを示しています。前のカウント = 0、現在のカウント = 1

本当に、このコードの何が問題なのですか?

4

2 に答える 2

5

COMMITSQL のどこにもa を入れていません。おそらくそれはの直前に行くべきですEND TRY

于 2012-08-10T15:42:02.517 に答える
0
-- =============================================              
-- Author:  <Ashish Jaishwal>              
-- Create date: <14 Jan 2016>              
-- Description: <create a bulk registeration of student and Login and alloted Batch>              
-- =============================================              
alter proc [dbo].[SPC_Bulk_Student_Register]          
(                 
 @BatchName varchar(50),          
 @CourseID int,          
 @START_DATE Datetime,          
 @END_DATE Datetime,          
 @MAX_LIMIT int,          
 @EmpID int,          
 @ExistBatchFlag int          
)          
as          
Begin          
DECLARE @STUDENT_ID VARCHAR(15)          
DECLARE @BATCHID INT          
DECLARE @COURSE_ID VARCHAR(6)                    
--DECLARE @EMPID INT          
--Note: 0 Batch is not Exist and 1 Batch is Exist  
--insert into TempExcelData select * from @BulkData;                
IF @ExistBatchFlag =0          
BEGIN          
  exec tc_insert_batch @BatchName,@CourseID,@START_DATE,@END_DATE,@MAX_LIMIT,@EmpID          
  set @BATCHID=(select batch_id from BATCH_MASTER where batch_name=@BatchName)          
END          
ELSE          
BEGIN          
 set @BATCHID=(select batch_id from BATCH_MASTER where batch_name=@BatchName)          
END          
--BEGIN try          
 BEGIN TRANSACTION     

 WHILE EXISTS(select top 1 ID,FNAME,LNAME,DOB,EMAILID from TempExcelData)          
 BEGIN          
  DECLARE @id INT          
  DECLARE @FNAME VARCHAR(40)          
  DECLARE @LNAME VARCHAR(40)          
  DECLARE @DOB DATETIME          
  DECLARE @EMAILID VARCHAR(40)          
  DECLARE @STUDENTLOGIN VARCHAR(50)          
  SELECT TOP 1 @id = Id,@FNAME=Fname,@LNAME =Lname,@DOB=DOB,@EMAILID=EmailID FROM TempExcelData          
  --HERE REGISTER THE STUDENT AND RETURN THE STUDENT ID          
   EXEC Spc_excel_insert_student_register @FNAME,@LNAME,@DOB,@EMAILID,@STUDENT_ID out          
   EXEC Spc_Excel_LoginName_Generation @FNAME,@LNAME,@STUDENTLOGIN OUT      
   EXEC sp_InsertStudentLogonDetails @STUDENT_ID,@STUDENTLOGIN,'welcome123','Y'      
   --HERE ALLOTED TO BATCH AND COURSE          
    EXEC tc_insertStudentRegister4_new @STUDENT_ID,@BATCHID,'',@CourseID          
   delete TempExcelData where id=@id     
  -- COMMIT TRAN           
 END          
     --COMMIT TRAN      
    IF ( @@error <> 0 )                 
      BEGIN       
       BEGIN              
          ROLLBACK TRAN    
          END                 
      END              
     IF (@@trancount > 0)                 
  BEGIN      
    BEGIN               
      COMMIT TRAN           
      END          
  END            
end               

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 2, current count = 0.

I AM FACING THIS SUCH TYPE PROBLEM................!!!!
于 2016-04-11T11:31:49.093 に答える