USE [MyCalender]

GO

/****** Object:  StoredProcedure [dbo].[SP_DayEvent]    Script Date: 10/6/2020 10:46:13 PM ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[SP_DayEvent]

@DayEventId varchar(50)

,@Note varchar(MAX)

,@EventDate datetime

,@OperationType int


AS

BEGIN TRAN

IF(@OperationType = 1) --Insert

BEGIN

SET @DayEventId = (SELECT MAX(DayEventId) FROM DayEvent) + 1


INSERT INTO DayEvent  (DayEventId, [Note], EventDate)

           VALUES(@DayEventId, @Note, @EventDate)


SELECT * FROM DayEvent WHERE DayEventId=@DayEventId


END

ELSE IF(@OperationType = 2) --Update

BEGIN

IF (@DayEventId = 0)

BEGIN

ROLLBACK

RAISERROR (N'Invalid DayEvent !!!~',16,1);

RETURN

END


UPDATE DayEvent SET [Note]=@Note

   ,EventDate=@EventDate

WHERE DayEventId=@DayEventId


SELECT * FROM DayEvent WHERE DayEventId=@DayEventId

END

ELSE IF(@OperationType = 3) --Delete

BEGIN

IF (@DayEventId = 0)

BEGIN

ROLLBACK

RAISERROR (N'Invalid DayEvent !!!~',16,1);

RETURN

END


DELETE FROM DayEvent WHERE DayEventId=@DayEventId

END

COMMIT TRAN