Search
Database

Transact-SQL in SQL Server 2008 : Table-Valued Parameters

Published 4 min readBy Programming4us Editorial Team
In previous versions of SQL Server, it was not possible to share the contents of table variables between stored procedures. SQL Server 2008 changes that with the introduction of table-valued parameters, which allow you to pass table variables to stored procedures as input parameters. Table-valued parameters provide more flexibility and, in many cases, better performance than temporary tables as a means to pass result sets between stored procedures.

To create and use table-valued parameters, you must first create a user-defined table type as a TABLE data type and define the table structure. This is done using the CREATE TYPE command, as shown in Listing 1.

Listing 1. Defining a User-Defined Table Type

After creating the user-defined table data type, you can use it for declaring local table variables and for stored procedure parameters. To use the table-valued parameter in a procedure, you create a procedure to receive and access data through a table-valued parameter, as shown in Listing 2.

Listing 2. Defining a Stored Procedure with a Table-Valued Parameter

Then, when calling that stored procedure, you declare a local table variable using the table data type defined previously, populate the table variable with data, and then pass the table variable to the stored procedure (see Listing 3).

Listing 3. Executing a Stored Procedure with a Table-Valued Parameter

The scope of a table-valued parameter is limited to only the stored procedure to which it is passed. To access the contents of a table-valued parameter in a procedure called by another procedure that contains a table-valued parameter, you need to pass the table-valued parameter to the subprocedure. Listing 4 provides an example of a subprocedure and alters the procedure created in Listing 4 in this article to call the subprocedure.

Listing 4. Passing a Table-Valued Parameter to a Subprocedure

Table-Valued Parameters Versus Temporary Tables

Table-valued parameters offer more flexibility and in some cases better performance than temporary tables or other ways to pass a list of values to a stored procedure. One benefit is table-valued parameters do not acquire locks for the initial population of data from a client. Also, table-valued parameters are memory resident and do not incur physical I/O unless they grow too large to remain in cache memory.

However, table-valued parameters do have some restrictions:

  • SQL Server does not create or maintain statistics on columns of table-valued parameters.

  • Table-valued parameters can be passed only as READONLY input parameters to T-SQL routines. You cannot perform UPDATE, DELETE, or INSERT operations on a table-valued parameter within the body of the stored procedure to which it is passed.

  • Like table variables, a table-valued parameter cannot be specified as the target of a SELECT INTO or INSERT EXEC statement. They can only be populated using an INSERT statement.