Skip to content

SQL Server create function procedure

Tako Lee edited this page Feb 18, 2014 · 7 revisions
  • Parameters
    • Fit into one line if possible

      • At the same line as CREATE keyword

        CREATE FUNCTION dbo.Fn_gettoporders(@custid AS INT,@n AS INT, @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
      • In new line

        CREATE FUNCTION dbo.Fn_gettoporders(
          @custid AS INT,@n AS INT, @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
      • Left parenthesis in new line

        CREATE FUNCTION dbo.Fn_gettoporders
          (@custid AS INT,@n AS INT, @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
    • Stacked parameters

      • Left/Right parenthesis in new line

        CREATE FUNCTION dbo.Fn_gettoporders
          (
             @custid AS INT,
             @n AS INT, 
             @test AS CHAR
          )  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
      • First parameter at the same line as left parenthesis

        • Comma at the end of line
        CREATE FUNCTION dbo.Fn_gettoporders(@custid AS INT,
                                            @n AS INT, 
                                            @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line
        CREATE FUNCTION dbo.Fn_gettoporders(@custid AS INT
                                            ,@n AS INT 
                                            ,@test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line, align parameters.
        CREATE FUNCTION dbo.Fn_gettoporders(@custid AS INT
                                           ,@n AS INT 
                                           ,@test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line, align parameters, space between comma and space is 1 or n
        CREATE FUNCTION dbo.Fn_gettoporders( @custid AS INT
                                           , @n AS INT 
                                           , @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
      • First parameter at new line, indent by 0 or n

        • Comma at the end of line
        CREATE FUNCTION dbo.Fn_gettoporders(
          @custid AS INT,
          @n AS INT, 
          @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line
        CREATE FUNCTION dbo.Fn_gettoporders(
          @custid AS INT
          ,@n AS INT 
          ,@test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line, align parameters.
        CREATE FUNCTION dbo.Fn_gettoporders(
           @custid AS INT
          ,@n AS INT 
          ,@test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
        • Comma at the begin of line, align parameters, space between comma and space is 1 or n
        CREATE FUNCTION dbo.Fn_gettoporders(
            @custid AS INT
          , @n AS INT 
          , @test AS CHAR)  
        RETURNS TABLE  
        AS  
          RETURN  
            SELECT TOP(@n) *  
            FROM   sales.salesorderheader  
            WHERE  customerid = @custid  
            ORDER  BY totaldue DESC  
        
        GO        
Clone this wiki locally