Multiple parameter function !

  • I want to have Multiple parameter function like concat :

    MY_FUN( string1, string2, ... string_n )

    whats syntax look like?

  • Will be exactly how you call the CONCAT function and how you have already discribed it

    CREATE FUNCTION dbo.my_func (@string1 varchar(10), @string2 varchar(10),............, @stringn varchar(10)

    ...

    ...

    ...

    dbo.my_func ('string1','string2',.....,'stringn')

  • http://bfy.tw/3Nxw

    https://msdn.microsoft.com/en-GB/library/ms186755.aspx

    --Transact-SQL Scalar Function Syntax

    CREATE FUNCTION [ schema_name. ] function_name

    ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type

    [ = default ] [ READONLY ] }

    [ ,...n ]

    ]

    )

    RETURNS return_data_type

    [ WITH <function_option> [ ,...n ] ]

    [ AS ]

    BEGIN

    function_body

    RETURN scalar_expression

    END

    [ ; ]

    --Transact-SQL Inline Table-Valued Function Syntax

    CREATE FUNCTION [ schema_name. ] function_name

    ( [ { @parameter_name [ AS ] [ type_schema_name. ] parameter_data_type

    [ = default ] [ READONLY ] }

    [ ,...n ]

    ]

    )

    RETURNS TABLE

    [ WITH <function_option> [ ,...n ] ]

    [ AS ]

    RETURN [ ( ] select_stmt [ ) ]

    [ ; ]

    Catch-all queries done right [/url]
    Gail Shaw's Performance Blog[/url]

  • If you mean a function that takes an undisclosed number of parameters, you can't create such a function. You have to define the parameters exactly when the function is created and specify them all when the function is called (I don't think that functions have optional parameters that can be left out of the call)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • If your goal is to pass a list of values to your function and then treat it as a table to perform a query, you could pass a delimited list in a single parameter, then split it and use it as a table in the function. See http://www.sqlservercentral.com/articles/Tally+Table/72993/ for a good string splitter ITVF that returns a table.

    If each string has a different purpose, then I think you're out of luck.

  • GilaMonster (12/21/2015)


    I don't think that functions have optional parameters that can be left out of the call

    That's correct, neither optional parameters nor default values can be defined for User Defined Functions.

    😎

    Note that default value parameter syntax doesn't cause syntax error which can be misleading as it does not work.

  • Ed Wagner (12/21/2015)


    If your goal is to pass a list of values to your function and then treat it as a table to perform a query, you could pass a delimited list in a single parameter, then split it and use it as a table in the function. See http://www.sqlservercentral.com/articles/Tally+Table/72993/ for a good string splitter ITVF that returns a table.

    If each string has a different purpose, then I think you're out of luck.

    Or pass a table valued parameter instead of a delimited list of values you have to parse. 🙂

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Eirikur Eiriksson (12/21/2015)


    GilaMonster (12/21/2015)


    I don't think that functions have optional parameters that can be left out of the call

    That's correct, neither optional parameters nor default values can be defined for User Defined Functions.

    😎

    Note that default value parameter syntax doesn't cause syntax error which can be misleading as it does not work.

    Agree that UDFs do not support optional parameters that can be left out of the call which is why the OP cannot implement their idea however UDFs support default values as far as how I utilize them. What did you mean by "does not work"?

    use tempdb;

    go

    if object_id(N'dbo.test_default_parameters') is not null

    drop function dbo.test_default_parameters;

    go

    create function dbo.test_default_parameters(@one int,

    @two int = 0 -- default value

    )

    returns table

    as

    return (select @one as one, @two as two);

    go

    select *

    from dbo.test_default_parameters(1,default); -- default keyword

    go

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Sean Lange (12/21/2015)


    Ed Wagner (12/21/2015)


    If your goal is to pass a list of values to your function and then treat it as a table to perform a query, you could pass a delimited list in a single parameter, then split it and use it as a table in the function. See http://www.sqlservercentral.com/articles/Tally+Table/72993/ for a good string splitter ITVF that returns a table.

    If each string has a different purpose, then I think you're out of luck.

    Or pass a table valued parameter instead of a delimited list of values you have to parse. 🙂

    +1

    There are some functional use cases where splitting strings is still necessary, e.g. when asking SSRS to send the value of a multi-select parameter to a stored procedure input parameter, but in general the need to pass delimited strings into a database API has been greatly reduced since TVPs were introduced in 2008.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Orlando Colamatteo (1/1/2016)


    Eirikur Eiriksson (12/21/2015)


    GilaMonster (12/21/2015)


    I don't think that functions have optional parameters that can be left out of the call

    That's correct, neither optional parameters nor default values can be defined for User Defined Functions.

    😎

    Note that default value parameter syntax doesn't cause syntax error which can be misleading as it does not work.

    Agree that UDFs do not support optional parameters that can be left out of the call which is why the OP cannot implement their idea however UDFs support default values as far as how I utilize them. What did you mean by "does not work"?

    use tempdb;

    go

    if object_id(N'dbo.test_default_parameters') is not null

    drop function dbo.test_default_parameters;

    go

    create function dbo.test_default_parameters(@one int,

    @two int = 0 -- default value

    )

    returns table

    as

    return (select @one as one, @two as two);

    go

    select *

    from dbo.test_default_parameters(1,default); -- default keyword

    go

    Thanks for the correction Orlando, didn't word this properly. With "does not work" I meant as a definition of an optional parameter, that is as optional parameter definition work in a stored procedure.

    😎

  • .

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Eirikur Eiriksson (1/2/2016)


    Orlando Colamatteo (1/1/2016)


    Eirikur Eiriksson (12/21/2015)


    GilaMonster (12/21/2015)


    I don't think that functions have optional parameters that can be left out of the call

    That's correct, neither optional parameters nor default values can be defined for User Defined Functions.

    😎

    Note that default value parameter syntax doesn't cause syntax error which can be misleading as it does not work.

    Agree that UDFs do not support optional parameters that can be left out of the call which is why the OP cannot implement their idea however UDFs support default values as far as how I utilize them. What did you mean by "does not work"?

    use tempdb;

    go

    if object_id(N'dbo.test_default_parameters') is not null

    drop function dbo.test_default_parameters;

    go

    create function dbo.test_default_parameters(@one int,

    @two int = 0 -- default value

    )

    returns table

    as

    return (select @one as one, @two as two);

    go

    select *

    from dbo.test_default_parameters(1,default); -- default keyword

    go

    Thanks for the correction Orlando, didn't word this properly. With "does not work" I meant as a definition of an optional parameter, that is as optional parameter definition work in a stored procedure.

    😎

    Figured you knew about DEFAULT so was wondering if you knew something I was missing about the API options. Thanks for confirming.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 12 posts - 1 through 11 (of 11 total)

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