• I realize this is an old post, but it comes up on google pretty high when you ask how to "throw error in UDF" so lemme answer with a trick I use to accomplish this for the next person that might find a need for this.

    My trick, is to force an invalid cast exception (by trying to convert a VARCHAR to an INT for example). In the same spirit as "SELECT 1/0".

    The side effect, is the string value your trying to convert to an INT will get spit out to the message window, thus allowing you to send information when it happens.

    CREATE FUNCTION ThrowError

    RETURNS INT

    AS

    BEGIN

    DECLARE @result INT

    SELECT @result = 'We have a major problem here' -- This throws a conversion error

    RETURN 1

    END