Home Forums SQL Server 2008 T-SQL (SS2K8) How to use values keyword as parameter of a function RE: How to use values keyword as parameter of a function

  • _simon_ (11/28/2012)


    Yes, with stored procedures this is easily achievable, but I cannot use it in scenarios like:

    declare @t table (Id int, I1 int, I2 int, I3 int, I4 int, I5 int)

    -- insert ...

    select Id, dbo.AreEqual(I1, I3, I5), dbo.AreEqual(I1, I2), dbo.AreEqual(I4, I5)

    from @t

    -- or

    select *

    from @t

    where dbo.AreEqual(I1, I2) and dbo.AreEqual(I4, I5)

    Sure you can. Did you try turning Joe's code into a function?

    e.g.

    CREATE FUNCTION Equal (

    @p1 INT = NULL,

    @p2 INT = NULL,

    @p3 INT = NULL,

    @p4 INT = NULL,

    @p5 INT = NULL

    )

    RETURNS VARCHAR(5)

    AS

    BEGIN

    DECLARE @ReturnValue VARCHAR(5);

    SELECT @ReturnValue = CASE WHEN MIN(parm) = MAX(parm) THEN 'True' ELSE 'False' END

    FROM (SELECT parm

    FROM (VALUES (@p1),(@p2),(@p3),(@p4),(@p5)) AS X1(parm)

    WHERE parm IS NOT NULL

    ) AS X2(parm);

    RETURN @ReturnValue;

    END


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/