In SQL Server 2005 and earlier, it was bit difficult to pass numerous parameters (Table variable) to the T-SQL Statements or Functions or Stored Procedures. The approach we used to take is creating a Temporary Table on Demand and inserts the values to the Temporary Table and then calls it in requisite procedure. In SQL Server 2008 the Table-Valued Parameters have been introduced that helps us eliminating the cumbersome process. This is the ability to easily pass a table to a stored procedure from T-SQL code or from an application as a parameter.
User-Defined Table Type
When first take a look at the new table-valued parameters, I thought that using this feature is a bit complicated. There are several steps involved. The first thing to do is to define a table type. If you look at the Types section under Programmability in the 2008 Management Studio, you will see the new “User-Defined Table Types”
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgi0dM2GUlB9f0mz42Uf8vo4ZiysSl0S6uiGhBpKFxpELvI-N6nsbp1_Jw2xEjWINJkKOJ4ANWeIKk7hwyn7PFyy85-mOWyRT720ywlIz9yhpUZ2Xu-QUFxRbCnYlMeoHT5FGuDib4uJPo8/s320/26998img1.jpg)
Table-valued Parameters are the new parameter types in SQL Server 2008 and it could be declared by declaring the user defined table types. The Table-valued parameters could be used to send multiple rows of data to a T-SQL statement or routine. Table-valued parameters are like the parameter arrays in OLE DB and ODBC but with better flexibility. It could be used to participate in the set-based operations. Permissions for the Table-valued parameters follow SQL Server Object Model so CREATE, GRANT, DENY, ALTER etc. could be used.
Table Valued Parameters help in the following:
1. They are Strongly Typed
2. We can specify the sort order and the unique key
3. Reduce Round trips to the server
4. We can have the table structure of different cardinality
5. It doesn't need or cause the statement to recompile
6. It doesn't need lock to initially populate the data from the client
7. Enables us to include complex business logic in a single routine
8. It provides a very simple programming model.
Lets start with an example:
Syantax:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZY8wyM6b5dy3gO_wY5WJn0QjVQy_ptgKYMOa9GGkwWWu6xXlyiuy50B0bnNTj7dDeLvV9OwR_bChdgtdpe_utu_UeCepxniKOV_6aePc7hmGZ_bc0Ca0P1A_LPmAs-qTA_bDh4-G3VeM1/s320/778868280.jpg)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbomv2pnyq4NPIsIvYuoGA4V0asy_F6evtwbs-T_MBjkALrV455MZRrPaHXx6TAlbQocIootsDgjkVZlcFLDYhLhtEGtgwTkGYy1zPgyvrtbJHVtSGmF8a_RaytcEi1MI3DuAmNhFQi3pU/s320/778868281.jpg)
After running the above code, the object definition is created and viewable in the “User-Defined Table Type”. You can view the properties there, but not modify them. To modify the type, you will have to drop it and then create it again with the modified definition.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxDMH0f7wK01EKJIkZh-6bvIbtQ7IGYpTR07xEfMm-UApzzpluJmbzPsblk1bQLJgZK8uhThqX2l-uoTnCeR7arYkasW99T3tqK8gbc55FW5C1ALidFBnd6NElw3auiwF8sP4oi55wGENv/s320/778868282.jpg)
Using the User-Defined Table Type
So far, it seems like we have done quite a bit of work, but we only have the type definition created. Like other programmable objects, it will stick around unless it is dropped. To use it in T-SQL code, you must create a variable of the new type and then populate it as you would any other table. Once it is populated, you can use it in other T-SQL statements. Because it is a variable, it goes out of scope automatically when the batch is completed. Notice in the code below that the name of the data type is the same as the type we just created.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFgF0oYvJJ8Q3h1cGhyphenhyphennyyPnSjHQOQtRFwrU5_QnL_8n2GD4IRawsdksOuYv27qQhVfLLvtuc7k750wgeEfM7Tk3KO4lwMviEBaPmGUg_UrVnF4z6pEECkaRsriYP8dYGSA21H5uN73zBd/s320/778868283.jpg)
As long as the variable does not go out of scope, you can do just about anything with it that you can do with a regular table variable, such as join another table or be used to populate another table. Like a table variable, you can not modify the table definition.
As I mentioned, the variable is gone once it goes out of scope. For example if you have a T-SQL script that is composed of more than one batch, the variable is only valid within the batch where it was created.
Using the Variable as a Parameter
So far, we haven’t seen anything that is not possible with a regular table variable. The benefit is being able to pass the variable of the new type to a stored procedure. A stored procedure must be created that uses the new type. Here is an example of that along with the code to create a regular table that we will be populating.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiou6X12MVFInNoiwhoWq_yIMpn4lfHx5ETiY2GdkVmCW4oOo1vjcKcl3je3IvtwmJpZHnyqu3yVPndGsyHmhsutOEA8dWman7HhrezF3SHUTJb1Hmv2myQ-dw9ShcgXXZZ2kvnEDSa1V47/s320/778868284.jpg)
Notice the READONLY qualifier after the table parameter. This is required because a copy of the user-defined table variable is not passed to the procedure. To be more efficient, a pointer is passed to the proc. Therefore, to eliminate changes to the variable inside the proc that would affect the original, no changes are allowed.
Finally, let’s put it all together and call the stored procedure. In the next code snippet, code from the previous section is used to create and populate the variable.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlJpHfvu5T9JA_QU0amNvLN8WQJZPVYyU4EpWX7OZyeiGeQLwKUKl-F-x1nhgFSFoHz8IGeK0C92cCl2Zg66vUgOQw6SNqO70HOWjYZuDPyHUKY3ayXN4bvPSP-2bSfxCAwzoLBaZV6dUW/s320/778868285.jpg)
In order for a user to use the User-Defined Table Type, EXECUTE or CONTROL permission must be granted. This is the command to grant permission to a user:
GRANT EXECUTE ON TYPE::dbo.MyType TO TestUser;
Calling from a .Net Application
The coolest way to use the table-valued parameter is from a .Net application. To do so, you need to have .NET 3.5 installed and make sure you are using the System.Data.SQLClient namespace. This gives you a new SQL data type called Structured that you will use when creating the parameter.
First create a local DataTable and populate it. Be sure that the DataTable that you create matches the user-defined table type’s column count and data types.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEifORgcRqYoRt_d9pLDCOst_OZ6Ai3Ia6DFiJKjgrxdgTjxJ02jOJBenn4fnfdPbtn6H8aUNFdbAGNk1arKZOAsHPw8K8KNIrQvQa7HaokhCbCgGM8RGYHJqyTz9ccaD1B2ICI_g3j5JJCf/s320/778868286.jpg)
Since we will be working with a stored proc, create a command object and add the two parameters. This code assumes that you have an open connection to your test database.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhrl4zn65Q29qOKndy79yn3B3FqZrqVcLOM6HC4zHYs2f0AVCI3EyJu3n7lKoOUt9dks3epuUJe92G-AqQy2_7bbt2boKt73jA82tBXjv3dVk5_FxdFMrB3mlTelueChemYtTz_gvS9Qo1f/s320/778868286.jpg)
Notice the data type of the @MyTableParam parameter. This is the new type added with .Net 3.5 to work with this new functionality. Finally, assign the local table to the parameter and execute the command.
Conclusion
The table-valued parameter feature introduced with SQL Server 2008 has been sorely needed. It will allow developers to write much better performing applications by decreasing round-trips to the server and by letting SQL Server work how it does best – on sets of data. It is not difficult to use once all the pieces are put into place.
Let me know if I missed anything...
No comments:
Post a Comment