Initializing input variable.

  • Hello All,

    I want to know whether it is possible to initialize an input variable in a user-defined function. If so, then how it should be used in a select query?

    create function dbo.udfgetloc (@locid int, @typ int = 0)

    returns varchar(10) as

    begin

          declare @STR varchar(10)

           if @typ = 0

               select @STR = query

          else

              select @STR = query

    return @STR

    end

    Now, i want to use the above function in a select query.

    select name, dbo.udfgetloc(1) Location from <tablename> -- is it correct.

    i am getting error saying that insufficient number of arguments were supplied to the function.

    My question -- is it wise to initialize an input variable in a user-defined function?

    Thanks in advance.


    Lucky

  • You must specify the word default when using a default value in a user defined function e.g.

    select name, dbo.udfgetloc(1, DEFAULT) Location from

    SEE BOL EXCERPT

    A user-defined function takes zero or more input parameters and returns either a scalar value or a table. A function can have a maximum of 1024 input parameters. When a parameter of the function has a default value, the keyword DEFAULT must be specified when calling the function to get the default value. This behavior is different from parameters with default values in stored procedures in which omitting the parameter also implies the default value. User-defined functions do not support output parameters.

    Dave

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

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