CREATE PROC [dbo].[SupplementaryInsurance_CheckDuplicate]
    @Name NVARCHAR(50),
    @Code VARCHAR(3),
    @IsInsert BIT    
AS
DECLARE @Result VARCHAR(100)
IF @IsInsert = 1
BEGIN
    IF EXISTS (SELECT Code FROM SupplementaryInsurance WHERE Code = @Code)
        SET @Result = 'DuplicateCode';
    ELSE IF EXISTS (SELECT [Name] FROM SupplementaryInsurance WHERE Name = @Name)
        SET @Result = 'DuplicateName';
    ELSE
        SET @Result = 'NoDuplicate';
    SELECT Result = @Result;
END;
ELSE
BEGIN
    IF EXISTS
    (
        SELECT [Name]
        FROM SupplementaryInsurance
        WHERE [Name] = @Name
              AND Code <> @Code
    )
        SET @Result = 'DuplicateName';
    ELSE
        SET @Result = 'NoDuplicate';
    SELECT Result = @Result;
END;
