Where to declare a data type when a function's parameters are declared

  • For readability is it preferable to declare variables an their data type outside and before the function? Or is it more succinct and arguably clearer to declare the variable's data type in the parentheses of a statement like this CREATE FUNCTION dbo.functionname (example varchar(8) ) ?

  • What exactly do you mean?

    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
  • When writing SQL code, which would be preferable from a readability perspective given these two examples which I believe are identical in terms of performance:

    1.

    DECLARE example varchar(8)

    CREATE FUNCTION dbo.FunctionName (example)

    2.

    CREATE FUNCTION dbo.FunctionName (example varchar(8) )

    Which is considered more readable?

    FWIW I meant to say:

    For readability is it preferable to declare variables and their data type outside and before the function?

    (this question refers to #1 above)

    Or is it more succinct and arguably clearer to declare the variable's data type in the parentheses of a statement like this CREATE FUNCTION dbo.functionname (example varchar(8) ) ?

  • There are a lot of things wrong with your last post. First, variable names are declared with an 'at' sign (@). Executing this code:

    DECLARE example varchar(8)

    ...results in the following error message:

    Msg 155, Level 15, State 2, Line 1

    'varchar' is not a recognized CURSOR option.

    It's simply incorrect syntax. The second problem is that the following code will fail for many reasons:

    DECLARE example varchar(8)

    CREATE FUNCTION dbo.FunctionName (example)

    First off, besides the variable declaration bad syntax, the CREATE FUNCTION statement must be the first in a batch. Second, as per BOL, parameters must have a specificied parameter type.

    In other words, your first scenario is not possible as it is just not the way T-SQL works. Your second is (provided the parameter declaration contains the 'at' symbol). Your idea of Variable Scope is a bit off. So it is not a question of readability, but actual syntax and scope that will prevent snippet 1 from even being possible.



    Twitter: @SQLife
    Email: sqlsalt(at)outlook(dot)com

  • Golfer22 (7/8/2012)


    When writing SQL code, which would be preferable from a readability perspective given these two examples which I believe are identical in terms of performance:

    Nothing to do with performance. Most rewrites aren't

    1.

    DECLARE example varchar(8)

    CREATE FUNCTION dbo.FunctionName (example)

    2.

    CREATE FUNCTION dbo.FunctionName (example varchar(8) )

    Which is considered more readable?

    Considering that the first is a syntax error, readability is also not the question

    The syntax for CREATE FUNCTION is (per BoL)

    CREATE FUNCTION [ schema_name. ] function_name

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

    [ = default ] [ READONLY ] }

    [ ,...n ]

    ]

    )

    There is no provision within the CREATE FUNCTION syntax definition for specifying the arguments or their data types anywhere other than in the brackets following CREATE FUNCTION.

    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

Viewing 5 posts - 1 through 4 (of 4 total)

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