Skip to main content
added a link and clarification
Source Link
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311
  1. is it a good candidate for parameter sniffing?

  2. how could I add option(recompile) within the code?

    ALTER PROCEDURE [dbo].[usp_upd_activity] @activityId INT, @title VARCHAR(100), @description VARCHAR(MAX), @inclusions VARCHAR(MAX), @locationId INT, @imageUriMain VARCHAR(255), @uploadToBucket VARCHAR(200), @path VARCHAR(200) AS BEGIN

     SET NOCOUNT ON;
    
     BEGIN TRAN 
     BEGIN TRY
    
         DECLARE @documentId INT
    
         IF NOT EXISTS (SELECT 1
                          FROM document 
                         WHERE activityId    = @activityId)
         BEGIN
             INSERT INTO  document 
                         (   [uploadToBucket], [path], [activityId])
             VALUES      (@uploadToBucket, @path, @activityId)
    
             SET @documentId = SCOPE_IDENTITY();
    
    
         END
         ELSE
         BEGIN
             UPDATE  document
             SET     uploadToBucket  = @uploadToBucket,
                     [path]          = @path,
                     activityId      = @activityId
             WHERE   activityId      = @activityId    
    
             SET @documentId = 
                 (SELECT documentId 
                    FROM document 
                    WHERE activityId = @activityId)
         END
    
         UPDATE          activity
         SET             title           =       @title,
                         description     =       @description,
                         inclusions      =       @inclusions,
                         locationId      =       @locationId,
                         imageUriMain    =       IsNull(@imageUriMain, imageUriMain),
                         documentId      =       @documentId
         WHERE           activityId      =       @activityId                 
    
         COMMIT
    
    
     END TRY
     BEGIN CATCH
         DECLARE     @ErrorMessage   nvarchar(max), 
                     @ErrorSeverity  int, 
                     @ErrorState     int;
         SELECT      @ErrorMessage = ERROR_MESSAGE() + ' Line ' + 
                                     cast(ERROR_LINE() as nvarchar(5)), 
                                     @ErrorSeverity = ERROR_SEVERITY(), 
                                     @ErrorState = ERROR_STATE();
    
         IF @@TRANCOUNT > 0
             ROLLBACK;
    
         RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
     END CATCH       
    

    END

There are differences between adding option(recompile) within the code and creating a stored procedure with recompile.

I would be more keen on the option(recompile) if that is possible.

ALTER PROCEDURE [dbo].[usp_upd_activity]
                    @activityId             INT,
                    @title                  VARCHAR(100), 
                    @description            VARCHAR(MAX),
                    @inclusions             VARCHAR(MAX),
                    @locationId             INT,
                    @imageUriMain           VARCHAR(255),
                    @uploadToBucket         VARCHAR(200),
                    @path                   VARCHAR(200)
AS
BEGIN

    SET NOCOUNT ON;
    
    BEGIN TRAN 
    BEGIN TRY

        DECLARE @documentId INT
        
        IF NOT EXISTS (SELECT 1
                         FROM document 
                        WHERE activityId    = @activityId)
        BEGIN
            INSERT INTO  document 
                        (   [uploadToBucket], [path], [activityId])
            VALUES      (@uploadToBucket, @path, @activityId)

            SET @documentId = SCOPE_IDENTITY();
            
             
        END
        ELSE
        BEGIN
            UPDATE  document
            SET     uploadToBucket  = @uploadToBucket,
                    [path]          = @path,
                    activityId      = @activityId
            WHERE   activityId      = @activityId    

            SET @documentId = 
                (SELECT documentId 
                   FROM document 
                   WHERE activityId = @activityId)
        END

        UPDATE          activity
        SET             title           =       @title,
                        description     =       @description,
                        inclusions      =       @inclusions,
                        locationId      =       @locationId,
                        imageUriMain    =       IsNull(@imageUriMain, imageUriMain),
                        documentId      =       @documentId
        WHERE           activityId      =       @activityId                 
             
        COMMIT


    END TRY
    BEGIN CATCH
        DECLARE     @ErrorMessage   nvarchar(max), 
                    @ErrorSeverity  int, 
                    @ErrorState     int;
        SELECT      @ErrorMessage = ERROR_MESSAGE() + ' Line ' + 
                                    cast(ERROR_LINE() as nvarchar(5)), 
                                    @ErrorSeverity = ERROR_SEVERITY(), 
                                    @ErrorState = ERROR_STATE();

        IF @@TRANCOUNT > 0
            ROLLBACK;

        RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
    END CATCH       
       
    
END
  1. is it a good candidate for parameter sniffing?

  2. how could I add option(recompile) within the code?

    ALTER PROCEDURE [dbo].[usp_upd_activity] @activityId INT, @title VARCHAR(100), @description VARCHAR(MAX), @inclusions VARCHAR(MAX), @locationId INT, @imageUriMain VARCHAR(255), @uploadToBucket VARCHAR(200), @path VARCHAR(200) AS BEGIN

     SET NOCOUNT ON;
    
     BEGIN TRAN 
     BEGIN TRY
    
         DECLARE @documentId INT
    
         IF NOT EXISTS (SELECT 1
                          FROM document 
                         WHERE activityId    = @activityId)
         BEGIN
             INSERT INTO  document 
                         (   [uploadToBucket], [path], [activityId])
             VALUES      (@uploadToBucket, @path, @activityId)
    
             SET @documentId = SCOPE_IDENTITY();
    
    
         END
         ELSE
         BEGIN
             UPDATE  document
             SET     uploadToBucket  = @uploadToBucket,
                     [path]          = @path,
                     activityId      = @activityId
             WHERE   activityId      = @activityId    
    
             SET @documentId = 
                 (SELECT documentId 
                    FROM document 
                    WHERE activityId = @activityId)
         END
    
         UPDATE          activity
         SET             title           =       @title,
                         description     =       @description,
                         inclusions      =       @inclusions,
                         locationId      =       @locationId,
                         imageUriMain    =       IsNull(@imageUriMain, imageUriMain),
                         documentId      =       @documentId
         WHERE           activityId      =       @activityId                 
    
         COMMIT
    
    
     END TRY
     BEGIN CATCH
         DECLARE     @ErrorMessage   nvarchar(max), 
                     @ErrorSeverity  int, 
                     @ErrorState     int;
         SELECT      @ErrorMessage = ERROR_MESSAGE() + ' Line ' + 
                                     cast(ERROR_LINE() as nvarchar(5)), 
                                     @ErrorSeverity = ERROR_SEVERITY(), 
                                     @ErrorState = ERROR_STATE();
    
         IF @@TRANCOUNT > 0
             ROLLBACK;
    
         RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
     END CATCH       
    

    END

  1. is it a good candidate for parameter sniffing?

  2. how could I add option(recompile) within the code?

There are differences between adding option(recompile) within the code and creating a stored procedure with recompile.

I would be more keen on the option(recompile) if that is possible.

ALTER PROCEDURE [dbo].[usp_upd_activity]
                    @activityId             INT,
                    @title                  VARCHAR(100), 
                    @description            VARCHAR(MAX),
                    @inclusions             VARCHAR(MAX),
                    @locationId             INT,
                    @imageUriMain           VARCHAR(255),
                    @uploadToBucket         VARCHAR(200),
                    @path                   VARCHAR(200)
AS
BEGIN

    SET NOCOUNT ON;
    
    BEGIN TRAN 
    BEGIN TRY

        DECLARE @documentId INT
        
        IF NOT EXISTS (SELECT 1
                         FROM document 
                        WHERE activityId    = @activityId)
        BEGIN
            INSERT INTO  document 
                        (   [uploadToBucket], [path], [activityId])
            VALUES      (@uploadToBucket, @path, @activityId)

            SET @documentId = SCOPE_IDENTITY();
            
             
        END
        ELSE
        BEGIN
            UPDATE  document
            SET     uploadToBucket  = @uploadToBucket,
                    [path]          = @path,
                    activityId      = @activityId
            WHERE   activityId      = @activityId    

            SET @documentId = 
                (SELECT documentId 
                   FROM document 
                   WHERE activityId = @activityId)
        END

        UPDATE          activity
        SET             title           =       @title,
                        description     =       @description,
                        inclusions      =       @inclusions,
                        locationId      =       @locationId,
                        imageUriMain    =       IsNull(@imageUriMain, imageUriMain),
                        documentId      =       @documentId
        WHERE           activityId      =       @activityId                 
             
        COMMIT


    END TRY
    BEGIN CATCH
        DECLARE     @ErrorMessage   nvarchar(max), 
                    @ErrorSeverity  int, 
                    @ErrorState     int;
        SELECT      @ErrorMessage = ERROR_MESSAGE() + ' Line ' + 
                                    cast(ERROR_LINE() as nvarchar(5)), 
                                    @ErrorSeverity = ERROR_SEVERITY(), 
                                    @ErrorState = ERROR_STATE();

        IF @@TRANCOUNT > 0
            ROLLBACK;

        RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
    END CATCH       
       
    
END
Source Link
Marcello Miorelli
  • 16.6k
  • 53
  • 170
  • 311

how to add option(recompile) within this stored procedure?

In my current environment, I have many instances of stored procedures, just like the one shown below, where a bunch of parameters are passed to the procedure, and then within the procedure a select exists is run and based on the result, different logic paths are run within the stored procedure.

I have a couple of questions regarding the procedure below:

  1. is it a good candidate for parameter sniffing?

  2. how could I add option(recompile) within the code?

    ALTER PROCEDURE [dbo].[usp_upd_activity] @activityId INT, @title VARCHAR(100), @description VARCHAR(MAX), @inclusions VARCHAR(MAX), @locationId INT, @imageUriMain VARCHAR(255), @uploadToBucket VARCHAR(200), @path VARCHAR(200) AS BEGIN

     SET NOCOUNT ON;
    
     BEGIN TRAN 
     BEGIN TRY
    
         DECLARE @documentId INT
    
         IF NOT EXISTS (SELECT 1
                          FROM document 
                         WHERE activityId    = @activityId)
         BEGIN
             INSERT INTO  document 
                         (   [uploadToBucket], [path], [activityId])
             VALUES      (@uploadToBucket, @path, @activityId)
    
             SET @documentId = SCOPE_IDENTITY();
    
    
         END
         ELSE
         BEGIN
             UPDATE  document
             SET     uploadToBucket  = @uploadToBucket,
                     [path]          = @path,
                     activityId      = @activityId
             WHERE   activityId      = @activityId    
    
             SET @documentId = 
                 (SELECT documentId 
                    FROM document 
                    WHERE activityId = @activityId)
         END
    
         UPDATE          activity
         SET             title           =       @title,
                         description     =       @description,
                         inclusions      =       @inclusions,
                         locationId      =       @locationId,
                         imageUriMain    =       IsNull(@imageUriMain, imageUriMain),
                         documentId      =       @documentId
         WHERE           activityId      =       @activityId                 
    
         COMMIT
    
    
     END TRY
     BEGIN CATCH
         DECLARE     @ErrorMessage   nvarchar(max), 
                     @ErrorSeverity  int, 
                     @ErrorState     int;
         SELECT      @ErrorMessage = ERROR_MESSAGE() + ' Line ' + 
                                     cast(ERROR_LINE() as nvarchar(5)), 
                                     @ErrorSeverity = ERROR_SEVERITY(), 
                                     @ErrorState = ERROR_STATE();
    
         IF @@TRANCOUNT > 0
             ROLLBACK;
    
         RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
     END CATCH       
    

    END