DECLARE @previousstatus nvarchar(50)
DECLARE @presentstatus nvarchar(50)
DECLARE @Startdate nvarchar(50)
DECLARE @Enddate nvarchar(50)
SET @Startdate = getdate() - 7
SET @Enddate = getdate() - 2
CREATE TABLE #Temp(
[Id] [int] IDENTITY(1,1) NOT NULL,
[StartDte] [date] NULL,
[Enddate] [date] NULL,
[Statu] [nvarchar](50) NULL,
CONSTRAINT [PK_ssse] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Select @previousstatus = statu FROM #Temp where ID = (Select max(id) From #Temp)
IF @Enddate < cast(getdate() as date) or ISNULL(@previousstatus,'') = ''
SET @presentstatus = 'Inactive'
ELSE
SET @presentstatus = 'Active'
IF @previousstatus = 'Inactive' or ISNULL(@previousstatus,'') = ''
BEGIN
Insert INTO #Temp(StartDte,enddate,statu) VALUES(@Startdate,@Enddate,@presentstatus)
END
SELECT * FROM #Temp
Drop TABLE #Temp