CREATE FUNCTION [dbo].[INTEGER_GREATEST_3PARAM]
(@val1 INT, @val2 INT,@val3 INT)
returns INT
AS
BEGIN
    Declare @result INT
    set @result = case when @val1 > @val2 then
               case when @val1 > @val3 then
                @val1
               else
                @val3
               end
        when @val2 > @val3 then
                @val2
        else
                 @val3
    end
    return @result
END