SQL Server Error 455: Fix It Step by Step
In this article
SQL Server error 455 (“The last statement included within a function must be a return statement”) means a scalar or inline table-valued function ends with something other than a RETURN. SQL Server requires the final statement in certain functions to hand a value back to the caller, and it does not care whether your logic “obviously” returns something in every branch — it reads the last statement literally. The fix is almost always to end the function body with an explicit RETURN (or to restructure multi-statement logic so exactly one final RETURN exists). You need nothing but SSMS, ADS or Azure Data Studio and permission to alter the object.
What You’ll Need
- SQL Server Management Studio, Azure Data Studio, or any client that can run T-SQL.
- ALTER permission on the function (db_owner, or membership in db_ddladmin/db_owner) and access to the database that contains it.
- The full
CREATE FUNCTIONdefinition — you will re-issue it asALTER FUNCTION. Retrieve it withsp_helptext,OBJECT_DEFINITION(OBJECT_ID('dbo.YourFunction')), or by scripting the object as CREATE in SSMS. - A test query that calls the function, so you can prove the fix works.
Step-by-Step
- Read the error’s object and line details. The message box shows the object name and, in many cases, the line number where the parser failed. Note them — the line is often the line after the last real statement because SQL Server is pointing at the missing
RETURN. - Decide which type of function you have. Run
SELECT OBJECT_NAME(object_id) AS fn, type_desc FROM sys.objects WHERE name = 'YourFunction';or checksys.sql_modules. The value tells you a lot:SQL_INLINE_TABLE_VALUED_FUNCTION,SQL_TABLE_VALUED_FUNCTION(multi-statement), or a scalar function. Scalar and multi-statement table-valued functions need aRETURN; inline table-valued functions do not useRETURNat all — they end with aSELECT. - Script the body. In SSMS, expand the database, then Programmability, then Functions, then Scalar-valued or Table-valued functions. Right-click the function and choose Script as CREATE to a new query window. This avoids retyping and gives you the exact original text.
- Look at the final statement. If it is
SELECT @result;,IF ... BEGIN ... ENDwithout a closing return, or a bareSELECTin a scalar function, that is your culprit. A scalar function must end withRETURN @result(orRETURN expression). - Convert CREATE to ALTER. Change the first line from
CREATE FUNCTIONtoALTER FUNCTIONand keep the rest identical. - Add a single exit point. Declare a variable to hold the answer, assign it inside branches, and place
RETURN @result;as the very last statement. For example:DECLARE @r INT; IF ... SET @r = 1; ELSE SET @r = 0; RETURN @r;. - Remove stray trailing code. Comments after the return are fine, but a
SELECT,PRINT,SET NOCOUNT ON, orBEGIN...ENDblock after the last return will re-trigger error 455. - Handle all branches. Make sure every
IFpath sets the variable or returns. A branch that falls through leaves a NULL or an unassigned value, and SQL Server may still parse fine but return unexpected results. - Run the ALTER in the correct database. Put
USE YourDatabase;at the top or select the database in the toolbar. Creating the function in master by accident produces “cannot find object” errors later. - Test the compiled function. Execute it with representative inputs, including NULL and edge values:
SELECT dbo.YourFunction(NULL), dbo.YourFunction(0), dbo.YourFunction(99);. - Check dependent objects. Query
sys.sql_expression_dependenciesto see what references the function, then run a refresher such assp_refreshsqlmoduleon calling views and procedures if their metadata is stale. - Re-script the function to confirm the final statement. The last executable line should now be the single
RETURN.
Common Mistakes
- Using
SELECTinstead ofRETURNin a scalar function.SELECT @x = 1assigns; it does not return. OnlyRETURN @xsatisfies the rule. - Adding
RETURNto an inline table-valued function. Inline TVFs end with aSELECTand cannot containRETURN— adding one produces a different parse error. If you need procedural logic, convert to a multi-statement TVF or a stored procedure. - Leaving a statement after the return. A trailing
PRINT, logging call, or testSELECTat the bottom re-creates error 455 even though the return is present. - Returning a table from a scalar function. Scalar functions return a single value; a
SELECTof a column set belongs in a TVF. - Wrong convert. Editing
CREATE OR ALTERcarelessly, or changing the function name/schema unintentionally, can break callers or create a duplicate object. - Assuming the message is a runtime error. Error 455 is a compile/parse-time problem; it fires when the batch is parsed, not when a row is processed, so wrapping the call in TRY/CATCH does not help.
Tips
- Keep scalar functions to a single
RETURNat the end — it reads clearly and avoids this error class entirely. - Prefer inline table-valued functions over multi-statement ones where possible; they perform better and need no
RETURN. - Use
CREATE OR ALTER FUNCTIONon supported SQL Server versions, so you can run the same script whether the object exists or not. - Keep the original definition in source control before altering production code.
- Run the whole function body as a single batch — splitting it across markers can confuse the parser.
Troubleshooting
If error 455 persists after adding a return, check line numbers carefully: an unterminated BEGIN...END block or a missing AS before the body can push the parser’s failure line to the end of the script. If you get “Cannot use a RETURN statement in a function”, you are editing an inline TVF — remove the return and end with SELECT instead. If the ALTER succeeds but callers fail with “invalid object name”, verify the schema prefix (dbo.) and the database context. If the function returns NULL unexpectedly, an IF branch was not covered. If dependent views report metadata errors, run sp_refreshsqlmodule on them. If the message names a different object than you edited, another function in the same database still has the flaw — search sys.sql_modules for functions ending without RETURN.
Frequently Asked Questions
What exactly causes SQL Server error 455?
The parser requires the final statement of a scalar or multi-statement table-valued function to be a RETURN. If the last statement is a SELECT, PRINT, IF, or block, the error fires.
Can I put the RETURN inside an IF block?
You can, but SQL Server still requires the last statement overall to be a return. It is safer to have one return at the end, after the branches.
Does error 455 apply to stored procedures?
No. Procedures can end with any statement. The rule applies to functions.
Why did the error appear after a small edit?
Any edit causing the final statement to no longer be a return — such as deleting the return or adding a trailing statement — repeats the error. Rerun the ALTER after fixing the end of the body.
What is the quickest fix?
Add RETURN @result; as the last statement of a scalar function, or end a multi-statement TVF with the single RETURN after populating its table variable.