Home Forums SQL Server 2008 T-SQL (SS2K8) How to implement try catch block for this proceudre RE: How to implement try catch block for this proceudre

  • Yeah,Yes.
    It will check for If exists if there is no record it will not go inside right?
    I changed the code like below - The issue is after changing I am able to see the records into dbo..Std table, But CATOI procedure is calling from another procedure where I am capturing rowcount; How many rows are entered into std table. That count is showing as 0 for me. but I see the records in the table. Not sure why the Rowcount is showing 0
    ------------------------------------------------------------------------------------
    EXEC dbo.CATOI  @ID
      
    SET @RowCount = @@ROWCOUNT;  
    SET @ErrorRows = @ErrorRows + @RowCount;

    ------------------------------------------------------------------------------------------------------------
    Alter procedure CATOI
    @ID nvarchar(10)
    AS
    BEGIN
        SET NOCOUNT ON
     BEGIN TRANSACTION
     BEGIN TRY

    IF EXISTS(SELECT 1 FROM dbo.Std WHERE ID = @ID)
     BEGIN
     DELETE FROM dbo.Std WHERE ID = @ID
     END

     INSERT INTO dbo..Std
     (
    ID
     )
     SELECT
    ID
     FROM stage.Std
     WHERE ID = @ID

     FINISH: 
    COMMIT TRANSACTION T1
        
            END TRY
            BEGIN CATCH
             ROLLBACK TRANSACTION T1
        
           Exec LogError 'CATOI', @ID

          END CATCH

    END