Practical .NET

Speed Up Stored Procedures with Table Valued Parameters

Peter revisits an earlier tip on how to use stored procedures to speed up your code. This version makes your stored procedures simpler … though you may have to write a little more code to make the call.

In an earlier column, I suggested that one way to speed up your application was to reduce the trips you make to your database, specifically by avoiding calling a stored procedure multiple times. To enable that, I showed how to pass a stored procedure multiple parameter values in a single call and then, inside the stored procedure, load the parameters into a table where they could be integrated with other SQL statements.

As several readers pointed out, I used very old-fashioned technology to implement this tactic: I concatenated all of the parameters into a string and then, in the stored procedure, broke the string into parts before loading those parts into a table. While my code worked in every version of SQL Server, it was hardly "hip and happening." But, if you're using any version of SQL Server later than SQL Server 2005, you have an alternative: table-valued parameters.

Table-valued parameters let you pass a collection of table rows as a parameter to your stored procedure. The benefit of passing table rows is you can short circuit some of the stored procedure code from my previous column: It's no longer necessary to break up the string (I used a set of T-SQL code I "borrowed" from a client to do that) or insert the values into a table because your parameters are already in a table.

In the examples for this column, I use ADO.NET, but these techniques work equally as well if you're calling your stored procedures from an Entity Framework DbContext object (as I've discussed in a previous column).

Accepting Table Valued Parameters
To use table-valued parameters you first need, in SQL Server Manager, to define a table type to your database. This example defines a table type called JobSpecifications with two columns called JobName and AvailableDate:

CREATE TYPE JobSpecifications AS TABLE 
  (JobName VARCHAR(50), 
  AvailableDate Date );

With the type defined, you can now use it as a parameter in any stored procedure where it makes sense. This example uses the type to accept one or more rows of the JobSpecifications type in a parameter called @JobCriteria:

Create PROCEDURE dbo.GetJobs
  @JobCriteria JobSpecifications ReadOnly
As

In the body of the stored procedure, you can use the parameter like any other table. This example joins the parameter to another table in the actual database to find some matching rows and return the result:

  Select * 
    From JobOpenings jo
      Inner Join @JobCriteria jc
        On  jo.JobName = jc.Name
        And jo.StartDate > jc.AvailableDate
Return

Passing Table-Valued Parameters
But, while everything is better on the T-SQL side of the processing, the code to call the stored procedure can be a little uglier because, under the worst possible scenario, the values you're passing are just sitting around in variables in your application. In that scenario you need to create a DataTable with columns that match your table type and then load it with rows.

For example, code to create a DataTable that matches my JobSpecifications type would look like this:

Dim jobspecs As New DataTable
jobspecs.Columns.Add("JobName", GetType(String))
jobspecs.Columns.Add("AvailableDate", GetType(Date))

This code creates a row from the DataTable, populates the two columns in the row and then adds the row to the table:

Dim rw As DataRow
rw = jobspecs.NewRow()
rw("JobName") = "Administrator"
rw("AvailableDate") = DateTime.Now
jobspecs.Rows.Add(rw)

Finally, of course, I need to pass the DataTable to the stored procedure:

Dim cn As New SqlConnection("...connection string...")
Dim cmd As SqlCommand = cn.CreateCommand
cmd.CommandText = "GetJobs"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("JobCriteria", jobspecs))

If this looks a little intimidating, it is the worst-possible scenario -- many other scenarios are simpler. If, for example, your parameters are in a DataTable in a DataSet, you can just pass the DataTable as your parameter. This example passes a DataTable called Parms in the DataSet held in a variable called ds (this will also work with any method that returns a DataTable, though not with any method that returns some collection of DataRows):

cmd.Parameters.Add(New SqlParameter("JobCriteria", ds.Tables("Parms")))

If you're reading your parameters from a table through a DataReader, then you can pass the DataReader as your parameter. This example uses the AddWithValue method on the Parameters collection to pass a DataReader called rdrParms:

cmd.Parameters.AddWithValue("@JobCriteria", rdrParms)
cmd.Parameters(0).SqlDbType = SqlDbType.Structured

There doesn't, however, seem to be a simple way to pass the results of a LINQ query to a table-valued parameter.

Whichever mechanism you use, once you've got the parameter added you can call the stored procedure and process the results:

Dim rdr As SqlDataReader
cn.Open()
rdr = cmd.ExecuteReader
Do While rdr.Read
  ...do something with the returned rows...
Loop
cn.Close()

While this column has concentrated on one technology for passing multiple parameters to a stored procedure, that's really just a means to an end. The real goal is to make your application run faster by reducing trips to your database. However you do that will make your users say the best thing: "Hey, that was fast!"

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube