SQL Server Error 346 Fix: Parameter Cannot Be Readonly
In this article
You can fix SQL Server error 346 by removing the READONLY keyword from the parameter in your stored procedure or function definition—this error occurs because parameters in inline table-valued functions (ITVFs) cannot be marked as READONLY. Below is a step-by-step guide to diagnosing and resolving it.
What This Achieves
This guide helps you identify and correct the "parameter cannot be declared readonly" error (error 346), which typically arises when:
- You define a parameter in an inline table-valued function (ITVF) and mistakenly add
READONLYto it. - A stored procedure or function references an ITVF with a READONLY parameter, triggering the constraint.
- You’re upgrading or modifying legacy code that predates SQL Server’s stricter parameter rules.
What You'll Need
- A SQL Server database with the stored procedure or function causing error 346.
- Administrative or schema-ownership permissions to alter the problematic object (e.g., a user-defined function).
- Your original script or the definition of the stored procedure/function from
sys.objectsorINFORMATION_SCHEMAviews. - (Optional) A backup of the database or script, in case changes need to be reverted.
Step-by-Step
-
Confirm the error source:
Open the error details in SSMS or your SQL client. Note the object name (e.g., a stored procedure or inline table-valued function) and its line number. If the error references an ITVF, this is almost certainly the cause. For example:
Msg 346, Level 16, State 1, Line 20 Parameter '@ParamName' cannot be declared readonly because it is used in an OUTPUT clause or as the target of an assignment.Use
SELECT definition FROM sys.sql_modules WHERE object_id = OBJECT_ID('YourSchema.YourFunction')to locate the exact definition. -
Identify READONLY parameters:
Search the object’s definition for the
READONLYkeyword. Example of an incorrect ITVF:CREATE FUNCTION dbo.YourITVF (@InputParam INT READONLY) RETURNS TABLE AS RETURN ( SELECT * FROM SomeTable WHERE SomeColumn = @InputParam );The parameter
@InputParamis incorrectly marked asREADONLY. -
Remove READONLY from the parameter:
Alter the function to remove
READONLY. For inline table-valued functions, parameters should never be marked as READONLY because they cannot be. Use this corrected version:ALTER FUNCTION dbo.YourITVF (@InputParam INT) RETURNS TABLE AS RETURN ( SELECT * FROM SomeTable WHERE SomeColumn = @InputParam );If you’re modifying a stored procedure that calls this function, ensure it does not pass the parameter in a way that conflicts with the function’s logic.
-
Test the change:
Run the stored procedure or function again to verify the error is resolved. For example:
DECLARE @Result TABLE (ID INT); INSERT INTO @Result EXEC dbo.SomeProcedureThatCallsYourITVF 123; SELECT * FROM @Result;If the error persists, check for other READONLY parameters in dependent objects (e.g., views or procedures referencing the function).
-
(Optional) Re-evaluate parameter usage:
If your function was intended to be readonly (e.g., to enforce immutability), consider redesigning it as a multi-statement table-valued function (MSTVF) or a scalar UDF if appropriate. However, for most cases, simply removing
READONLYis sufficient.
Common Mistakes
-
Assuming all functions require READONLY:
Inline table-valued functions (ITVFs) cannot use
READONLYparameters. This is a SQL Server constraint, not a best practice. Scalar UDFs and MSTVFs can use READONLY, but ITVFs cannot. -
Not checking dependent objects:
After altering a function, errors may still occur if other objects (e.g., views or stored procedures) reference it incorrectly. Always test the entire workflow after making changes.
-
Using READONLY in OUTPUT parameters:
Parameters marked as
OUTPUTcannot beREADONLY. If you need to return values, useOUTPUTor a separate return table instead. -
Failing to back up before altering:
Always script your object definition (e.g.,
SELECT definition FROM sys.sql_modules) before making changes. This lets you revert if issues arise.
Tips
-
Use SSMS IntelliSense:
When writing T-SQL, SSMS’s IntelliSense will highlight invalid syntax, including unsupported parameter modifiers. Enable it with
Ctrl+Spaceduring script authoring. -
Leverage dynamic SQL for complex alterations:
If you’re altering many objects, generate scripts dynamically to avoid manual errors:
DECLARE @SQL NVARCHAR(MAX); SELECT @SQL = 'ALTER FUNCTION dbo.' + OBJECT_NAME(object_id) + ' (' + parameters + ') RETURNS TABLE AS RETURN (' + body + ');' FROM sys.sql_modules WHERE definition LIKE '%READONLY%' EXEC sp_executesql @SQL; -
Document parameter usage:
Add comments to your functions explaining whether parameters are input-only, output, or both. Example:
CREATE FUNCTION dbo.CalculateDiscount (@CustomerID INT) -- Input-only parameter RETURNS DECIMAL(10,2) AS RETURN ... -
Test in a staging environment:
If altering production objects, test changes in a non-production database first to avoid unintended consequences.
Troubleshooting
If the error persists after removing READONLY, follow this order:
-
Verify the object definition:
Double-check that the parameter was indeed removed. Run:
SELECT definition FROM sys.sql_modules WHERE object_id = OBJECT_ID('dbo.YourITVF');Look for any residual
READONLYkeywords. -
Check for cascading dependencies:
Use
sp_dependsto list objects referencing your function:EXEC sp_depends 'dbo.YourITVF';Alter or recreate dependent objects if necessary.
-
Review transaction isolation levels:
If the error appears intermittently, test with explicit transaction isolation levels (e.g.,
READ UNCOMMITTED) to rule out blocking issues. However, this is unlikely to fix error 346. -
Recreate the object:
If the function is corrupted, drop and recreate it:
DROP FUNCTION dbo.YourITVF; GO CREATE FUNCTION dbo.YourITVF (@Param INT) RETURNS TABLE AS RETURN (SELECT * FROM SomeTable WHERE SomeColumn = @Param); -
Restart SQL Server services:
As a last resort, restart the SQL Server service if the error persists. This rarely resolves syntax issues but can clear temporary state.
-
Check for schema changes:
If the function was created in a different schema, ensure you’re altering the correct object. Use
SCHEMA_NAME(object_id)to verify.
Frequently Asked Questions
Why does error 346 occur specifically for inline table-valued functions?
SQL Server enforces that inline table-valued functions (ITVFs) cannot use READONLY parameters because their primary purpose is to return query results, not modify data. The READONLY modifier would imply immutability, but ITVFs are fundamentally read-only in their execution (though they can reference modifiable tables). This is a design constraint to prevent ambiguity.
Can I use READONLY with scalar UDFs or multi-statement table-valued functions?
Yes. Only inline table-valued functions cannot use READONLY. Scalar UDFs and multi-statement table-valued functions (MSTVFs) support it. For example:
CREATE FUNCTION dbo.ScalarUDF (@InputParam INT READONLY)
RETURNS INT
AS
BEGIN
RETURN @InputParam * 2;
END;
Always check the function type in sys.objects to confirm compatibility.
What if I need to enforce immutability for a function?
If your goal is to prevent modifications to input parameters, use a TRY/CATCH block or application-layer validation instead of READONLY. For example:
CREATE FUNCTION dbo.ImmutableCalculation (@Value INT)
RETURNS INT
AS
BEGIN
BEGIN TRY
IF @Value IS NULL OR @Value < 0
THROW 50000, 'Parameter cannot be null or negative', 1;
RETURN @Value;
END TRY
BEGIN CATCH
RETURN 0;
END CATCH
END;
This approach is more flexible than READONLY and works across all function types.
Will removing READONLY break dependent stored procedures?
Only if the procedure explicitly relies on the parameter being readonly, which is rare. Most procedures treat parameters as input-only by default. However, test the procedure thoroughly after altering the function. If errors persist, review the procedure’s logic for assumptions about parameter immutability.
Can I use OUTPUT parameters instead of READONLY?
Yes, but they serve different purposes. OUTPUT parameters return values from the function, while READONLY (when allowed) signals that the parameter should not be modified. For ITVFs, use a table-valued return type instead. Example:
CREATE FUNCTION dbo.GetOutputValues (@Input INT OUTPUT)
RETURNS TABLE
AS
BEGIN
SET @Input = @Input + 1;
RETURN (SELECT @Input AS Result);
END;
However, this is unconventional. Prefer explicit return tables for ITVFs.
Does this error occur in SQL Server Always On Availability Groups?
No. Error 346 is a syntax constraint and does not depend on Availability Groups. However, altering objects in a synchronous replica may cause brief disruptions. Always test changes in a non-primary replica first if using Always On.