SQL Server Error 352: Table-Valued Parameter Fix
In this article
SQL Server error 352 means you tried to pass a table-valued parameter (TVP) to a stored procedure or function, but the parameter was not declared as a table type in the database. The fix is to create a user-defined table type with CREATE TYPE ... AS TABLE, declare the procedure parameter using that type with READONLY, and then pass a matching DataTable or structured variable from your client code. This guide walks through the exact steps, the common mistakes that trigger the error, and what to check when it still fails.
What You'll Need
- SQL Server Management Studio (SSMS) or Azure Data Studio connected to your database with permission to create types and alter procedures.
- The name of the stored procedure or function that is throwing error 352.
- Knowledge of the columns and data types you need to pass as a table.
- Your client code (C#/ADO.NET, Python/pyodbc, etc.) if you are calling the procedure from an application.
Step-by-Step
- Identify the failing call. Look at the error message text: it usually names the parameter, for example The table-valued parameter "@Items" must be declared with a valid type name. Note the procedure name and parameter name. If you are in SSMS, run the procedure with a sample call to reproduce the error.
- Create a user-defined table type. In a new query window, run a statement like:
CREATE TYPE dbo.ItemList AS TABLE (ItemId INT NOT NULL, ItemName NVARCHAR(100) NULL);
The columns must match exactly the shape of the data you intend to pass. Table types are scoped to the database, so create it in the same database as the procedure. - Declare the procedure parameter with that type. Alter or create the procedure so the parameter uses the type name and includes
READONLY:CREATE PROCEDURE dbo.usp_InsertItems @Items dbo.ItemList READONLY AS BEGIN INSERT INTO dbo.Items (ItemId, ItemName) SELECT ItemId, ItemName FROM @Items; END
WithoutREADONLY, SQL Server raises a different error, but omitting the type name is what causes 352. - Grant permissions if needed. The caller needs
EXECUTEon the procedure andREFERENCESon the table type. For a quick test in development, you can grantREFERENCESon the type to the user or role. In production, follow least-privilege practice. - Call the procedure correctly from T-SQL. Declare a variable of the table type, insert rows, and pass it:
DECLARE @t dbo.ItemList; INSERT INTO @t VALUES (1, 'Widget'), (2, 'Gadget'); EXEC dbo.usp_InsertItems @Items = @t;
This confirms the server-side declaration works before you touch application code. - Update your client code to send a structured parameter. In ADO.NET, create a
DataTablewith columns matching the type, setSqlParameter.TypeName = "dbo.ItemList", and setSqlDbType = SqlDbType.Structured. For example:var p = cmd.Parameters.AddWithValue("@Items", dt); p.SqlDbType = SqlDbType.Structured; p.TypeName = "dbo.ItemList";
In Python with pyodbc, usecursor.executewith a list of tuples and theTVPsupport if available, or call the procedure with a table variable built via a batch. Check your driver's documentation for structured parameter support. - Test end to end. Run the application call and verify rows are inserted. If you still see error 352, double-check the type name spelling, schema (dbo vs. custom schema), and that the type exists in the same database as the procedure.
Common Mistakes
- Forgetting
READONLY. SQL Server requires table-valued parameters to be read-only. Omitting it causes a different error, but it is often confused with 352. - Using a table variable type without creating the type. Declaring
@Items TABLE (...)inside the procedure body is not a parameter; the parameter must reference a user-defined table type. - Type name mismatch. The type name in the procedure must match exactly, including schema. If you created
dbo.ItemListbut referenceItemListwithout schema, it may resolve differently depending on default schema. - Creating the type in the wrong database. Table types are database-scoped. A type created in
masteris not visible in your application database. - Client parameter not marked as structured. In ADO.NET, if you add a parameter without setting
SqlDbType.StructuredandTypeName, the provider sends a scalar value and SQL Server cannot bind it to the TVP. - Column order or data type mismatch. The DataTable columns must match the table type's columns in name, order, and type. A mismatch can cause conversion errors or silent truncation.
Tips
- Keep table types simple and stable. Changing a table type requires dropping and recreating it, which means dropping dependent procedures first. Use
sp_renameor a migration script to manage changes. - Name your table types clearly, e.g.,
dbo.ItemListordbo.OrderDetailType, to avoid confusion with regular tables. - Use
SELECT * FROM sys.table_typesto list existing table types and verify the schema and name. - In SSMS, you can script the table type and procedure together to keep definitions in sync.
- When performance matters, TVPs are best for moderate row counts (hundreds to a few thousand). For very large sets, consider bulk insert or table-valued parameters with streaming, but test carefully.
Troubleshooting
If error 352 persists after following the steps, work through these checks in order:
- Verify the type exists and is in the right database. Run
SELECT name, schema_id FROM sys.table_types WHERE name = 'ItemList';in the target database. If it returns nothing, create it there. - Check the procedure definition. Use
sp_helptext 'dbo.usp_InsertItems'or SSMS Object Explorer to confirm the parameter is declared as@Items dbo.ItemList READONLY. If it shows a plainTABLEtype or no type, alter the procedure. - Confirm permissions. The executing user needs
REFERENCESon the type. Grant it withGRANT REFERENCES ON TYPE::dbo.ItemList TO [user];. - Test with a minimal T-SQL call. If the T-SQL call works but the application fails, the issue is in the client parameter setup. Review the code that sets
TypeNameandSqlDbType. - Check for schema binding or dependencies. If the procedure is schema-bound, you may need to drop and recreate it after creating the type.
- Look for case-sensitivity issues. If the database uses a case-sensitive collation, type names must match case exactly.
If none of these resolve the error, capture the exact error message and the procedure definition, and compare the parameter declaration line by line with a working example.
Frequently Asked Questions
What causes SQL Server error 352?
Error 352 occurs when a table-valued parameter is used in a procedure or function but is not declared with a valid user-defined table type. The parameter must reference a type created with CREATE TYPE ... AS TABLE and include READONLY.
Can I use a table variable as a parameter without creating a type?
No. SQL Server requires a user-defined table type for table-valued parameters. Table variables declared inside a procedure are local and cannot be passed as parameters.
Do I need to grant special permissions for TVPs?
Yes. The caller needs EXECUTE on the procedure and REFERENCES on the table type. Without REFERENCES, you may get a permission error rather than 352, but it is a common follow-up issue.
Why does my C# code still fail after creating the type?
Ensure the SqlParameter has SqlDbType = SqlDbType.Structured and TypeName = "dbo.ItemList" set. Also verify the DataTable columns match the type's columns in order and data type.
Can I alter a table type after it is used?
You cannot alter a table type directly. You must drop dependent procedures, drop the type, create the new type, and recreate the procedures. Plan this in a maintenance window.
Does error 352 affect Azure SQL Database?
The same rules apply in Azure SQL Database. Create the table type in the user database, declare the parameter with READONLY, and ensure the client sends a structured parameter.