is it a good candidate for parameter sniffing?
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
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