Sei sulla pagina 1di 2

USE [Week_Report]

GO
/****** Object: StoredProcedure [dbo].[sp_InsertSchedule_Management] Script
Date: 05/05/2019 11:42:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[sp_InsertSchedule_Management]
(
@Model VARCHAR(200),
@Buyer INT,
@CycleName VARCHAR(200),
@StartDate DATETIME,
@EndDate DATETIME,
@IsLgInternal Bit,
@TargetDate DATETIME,
@Result int output
)

AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRY
BEGIN TRANSACTION
SET @Result =0
DECLARE @ID int
--- Check duplicate record
IF Not EXISTS(SELECT 'True' FROM Schedule_Management
WHERE Model = @Model AND Buyer = @Buyer AND CycleName =
@CycleName AND
StartDate = @StartDate AND EndDate = @EndDate AND
IsLgInternal = @IsLgInternal AND TargetDate = @TargetDate)
begin
SET @Result =2
end

---Check date range


IF Not EXISTS(SELECT 'True' FROM Schedule_Management
WHERE Model = @Model AND Buyer = @Buyer AND CycleName =
@CycleName
AND IsLgInternal = @IsLgInternal AND TargetDate =
@TargetDate
and (@StartDate <= EndDate)
AND (@EndDate >= StartDate)
)
begin
SET @Result =3
end
else

BEGIN
INSERT INTO [dbo].[Schedule_Management]
(
[Model]
,[Buyer]
,[CycleName]
,[StartDate]
,[EndDate]
,[IsLgInternal]
,[TargetDate]
)
VALUES
(
@Model
,@Buyer
,@CycleName
,@StartDate
,@EndDate
,@IsLgInternal
,@TargetDate
)
SET @Result =1
SET @ID = SCOPE_IDENTITY()
commit;
END

END TRY
BEGIN CATCH
ROLLBACK

SET @Result =0
EXEC [dbo].[SP_ErrorLog] --To log Stored procedure errors

END CATCH;
End
end
;

Potrebbero piacerti anche