
Creating and Using Table-Valued Parameters in Transact-SQL
Table-valued parameters have two primary components: a SQL Server type and a parameter that references that type. To create and use table-valued parameters, follow these steps:
-
Create a table type and define the table structure.
For information about how to create a SQL Server type, see User-Defined Table Types. For more information about how to define a table structure, see CREATE TABLE (Transact-SQL).
-
Declare a routine that has a parameter of the table type. For more information about SQL Server routines, see CREATE PROCEDURE (Transact-SQL) and CREATE FUNCTION (Transact-SQL).
-
Declare a variable of the table type, and reference the table type. For information about how to declare variables, see DECLARE @local_variable (Transact-SQL).
-
Fill the table variable by using an INSERT statement. For more information about how to insert data, see Adding Rows by Using INSERT and SELECT.
-
After the table variable is created and filled, you can pass the variable to a routine.
After the routine is out of scope, the table-valued parameter is no longer available. The type definition remains until it is dropped.
To use a table-valued parameter in the SQL Server Native Client, see Table-Valued Parameters (SQL Server Native Client).
To use a table-valued parameter in ADO.NET, see the ADO.NET documentation.
Benefits
Table-valued parameters offer more flexibility and in some cases better performance than temporary tables or other ways to pass a list of parameters. Table-valued parameters offer the following benefits:
-
Do not acquire locks for the initial population of data from a client.
-
Provide a simple programming model.
-
Enable you to include complex business logic in a single routine.
-
Reduce round trips to the server.
-
Can have a table structure of different cardinality.
-
Are strongly typed.
-
Enable the client to specify sort order and unique keys.
Restrictions
Table-valued parameters have the following restrictions:
-
SQL Server does not maintain statistics on columns of table-valued parameters.
-
Table-valued parameters must be passed as input READONLY parameters to Transact-SQL routines. You cannot perform DML operations such as UPDATE, DELETE, or INSERT on a table-valued parameter in the body of a routine.
-
You cannot use a table-valued parameter as target of a SELECT INTO or INSERT EXEC statement. A table-valued parameter can be in the FROM clause of SELECT INTO or in the INSERT EXEC string or stored-procedure.
Scope
A table-valued parameter is scoped to the stored procedure, function or dynamic Transact-SQL text, exactly like other parameters. Similarly, a variable of table type has scope like any other local variable that is created by using a DECLARE statement. You can declare table-valued variables within dynamic Transact-SQL statements and pass these variables as table-valued parameters to stored procedures and functions.
Security
Permissions for table-valued parameters follow the object security model for SQL Server, by using the Transact-SQL keywords: CREATE, GRANT, DENY, ALTER, CONTROL, TAKE OWNERSHIP, REFERENCES, EXECUTE, VIEW DEFINITION, and REVOKE.
Catalog Views