DATABASE

SQL Server : Reusing T-SQL Code - How Reusing Code Improves its Robustness

3/23/2013 9:40:55 PM

Rather than repeat the same logic in multiple places, we need to refactor the common functionality out of our two stored procedures. We can implement the definition of "sales for a given month" in an inline UDF, as shown in Listing 1.

Listing 1. Implementing the definition of "sales for a given month" in an inline UDF.

This new inline UDF can then be used in both stored procedures.

Listing 2. Utilizing the new inline function in our two stored procedures.

Can we reuse the definition of the reporting period in other queries against other tables? We can at least try to go one step further and have one module define our reporting period and do nothing else. As usual, we should verify that the performance is still acceptable. The code in Listing 3 shows how to implement the definition of reporting period as an inline UDF.

Listing 3. An inline UDF that implements the definition of a reporting period.

We can utilize this inline UDF when we implement the "sales for a given month" functionality.

Listing 4. Altering SalesPerStateForMonth to utilize the new MonthReportingPeriodStart function.


Alternatively, we can use a scalar UDF to implement the definition of reporting period, as shown in Listing 5.

Listing 5. Scalar UDF which implements the definition of reporting period.

We also have to change our SalesForMonth function, so that it utilizes our new scalar UDF, as shown in Listing 6.

Listing 6. Altering SalesForMonth to utilize the new scalar UDF MonthReportingPeriodStart.

Note that the new implementation of SalesForMonth is simpler than the previous one): instead of using the CROSS APPLY clause to utilize the inline UDF, we can just invoke the scalar UDF directly in the WHERE clause.

The basic fact remains, however, that implementing the same logic in multiple places increases the possibility of bugs when our requirements change. Instead, we should aim for sensible code reuse wherever possible, and UDFs are just one of the means to achieve this.

Over the coming sections, we'll discuss other ways in which we can reuse T-SQL code, as dictated by the given circumstances. Overall, reusing code is a very important component of defensive programming, and I cannot emphasize strongly enough how much it can improve the robustness of our code.

Other  
  •  SQL Server : Reusing T-SQL Code - The Dangers of Copy-and-Paste
  •  SQL Server 2005 Native XML Web Services : Example Native XML Web Services Project (part 3) - Creating the Client Application
  •  SQL Server 2005 Native XML Web Services : Example Native XML Web Services Project (part 2) - Exposing the Endpoints
  •  SQL Server 2005 Native XML Web Services : Example Native XML Web Services Project (part 1) - Creating the SQL Server Functionality
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 2) - Calling Native XML Web Service Endpoints from Client Applications
  •  SQL Server 2005 Native XML Web Services : Exposing SQL Programmability as Web Services (part 1)
  •  Western Digital Black 4TB Hard Drive - 4TB Storage Goes Mainstream
  •  Intel Solid State Drive 335 Series - Better Under The Hood
  •  HDD, SSD and Hybrid Hard Drive Competition
  •  SQL Server 2008 : Index analysis (part 3) - Identifying index fragmentation
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone