Function Issues

  • MattNorman88

    SSC Journeyman

    Points: 77

    I have created the following function which is to be used for a check constraint.

    For some reason it is always returning 0 even if the condition is met.

    I have tested it in a normal query and it returns 1 as expected.

    Can anyone see any issues with the function?

    CREATE FUNCTION IsCoverTypeValid(@CoverType NVARCHAR) RETURNS BIT
    AS BEGIN
    DECLARE @Result BIT = 0
    IF EXISTS (SELECT 1 FROM PolicyCoverTypes WHERE CoverType =@CoverType)
    SET @Result = 1
    RETURN @Result
    END
    GO
  • Sue_H

    SSC Guru

    Points: 90011

    Try setting a size for the @CoverType parameter. When you don't specify the size, it defaults to 1

    Sue

  • MattNorman88

    SSC Journeyman

    Points: 77

    Thank you Sue.

     

    That was indeed the issue

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply