Tuesday, 24 April 2018

How to alter a stored procedure in SQL Server

USE [SampleDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Sp_UpdateEmployee]
AS
BEGIN
  DECLARE @CurrentRowCount int

  BEGIN TRY
    BEGIN TRANSACTION

      SELECT
        @CurrentRowCount = (SELECT
          COUNT(*)
        FROM Employee
        WHERE status = 0
        AND updated_on < DATEADD(dd, -90,
        GETDATE()))

      WHILE (@CurrentRowCount > 0)
      BEGIN
        UPDATE Employee
        SET status = 4
        WHERE updated_on < DATEADD(dd, -90, GETDATE())
      END

    COMMIT TRANSACTION
  END TRY

  BEGIN CATCH
    ROLLBACK TRANSACTION
  END CATCH
END


GO


No comments:

Post a Comment