Troubleshooting Guide: Error 2733 – “The %ls data type is invalid for return values”
Why This Error Happens
This error occurs when a Transact-SQL user-defined function (UDF) or stored procedure attempts to return a data type that SQL Server does not permit as a valid return value, such as TEXT, NTEXT, IMAGE, CURSOR, or TABLE in unsupported contexts. It is typically triggered during the compilation or deployment of a CLR assembly or T-SQL module where the RETURNS clause references a deprecated or restricted type. This can also surface after an upgrade to SQL Server 2025 if legacy database objects relied on data types that have since been fully deprecated or restricted in return-value scenarios.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Object
- Open SQL Server Management Studio (SSMS) and locate the exact function or procedure referenced in the error message (check the Activity Log at
C:\Program Files\Microsoft SQL Server\160\Setup Bootstrap\Log\Summary.txtif it occurred during setup). - Run the following query to scan for deprecated return types across your database:
SELECT o.name, t.name AS return_type
FROM sys.objects o
JOIN sys.parameters p ON o.object_id = p.object_id
JOIN sys.types t ON p.user_type_id = t.user_type_id
WHERE p.parameter_id = 0 AND t.name IN ('text','ntext','image');
Step 2: Modify the Return Data Type
- Edit the
CREATE FUNCTIONorCREATE PROCEDUREscript to replace the invalid type with a modern, supported equivalent: - Replace
TEXTwithVARCHAR(MAX). - Replace
NTEXTwithNVARCHAR(MAX). - Replace
IMAGEwithVARBINARY(MAX). - Save the script and redeploy using
sqlcmd -i "C:\Scripts\FixFunction.sql".
Step 3: Rebuild the CLR Assembly (If Applicable)
- If the error stems from a .NET CLR integration function, open the project in Visual Studio 2025.
- Update the C# or VB.NET method signature to map to a SQL-compatible
SqlDbType(e.g., changeSqlDbType.TexttoSqlDbType.NVarChar). - Recompile the DLL and redeploy using
ALTER ASSEMBLYor re-run the deployment wizard.
Step 4: Clear Cached Execution Plans
- Once the object is corrected, clear the procedure cache to ensure Windows Server 2025 isn’t reading a stale, invalid plan.
- Execute
DBCC FREEPROCCACHE;in a new query window to force SQL Server to recompile all stored procedures.
Alternative or Verification Steps
- Verification: Execute the specific function using
SELECT dbo.YourFunctionName()or runEXEC sp_helptext 'YourProcName'to confirm the schema now reflects the updated data type without throwing Error 2733. - Alternative: If modifying the original object is not feasible immediately, create a wrapper stored procedure that casts the output of the legacy function into a supported data type (e.g.,
CAST(dbo.OldFunction() AS NVARCHAR(MAX))) before returning it to the client application.