Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 455: Fix It Step by Step

· 6 min read · Home Tech

In this article
  1. What You’ll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 FUNCTION definition — you will re-issue it as ALTER FUNCTION. Retrieve it with sp_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

  1. 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.
  2. Decide which type of function you have. Run SELECT OBJECT_NAME(object_id) AS fn, type_desc FROM sys.objects WHERE name = 'YourFunction'; or check sys.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 a RETURN; inline table-valued functions do not use RETURN at all — they end with a SELECT.
  3. 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.
  4. Look at the final statement. If it is SELECT @result;, IF ... BEGIN ... END without a closing return, or a bare SELECT in a scalar function, that is your culprit. A scalar function must end with RETURN @result (or RETURN expression).
  5. Convert CREATE to ALTER. Change the first line from CREATE FUNCTION to ALTER FUNCTION and keep the rest identical.
  6. 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;.
  7. Remove stray trailing code. Comments after the return are fine, but a SELECT, PRINT, SET NOCOUNT ON, or BEGIN...END block after the last return will re-trigger error 455.
  8. Handle all branches. Make sure every IF path 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.
  9. 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.
  10. Test the compiled function. Execute it with representative inputs, including NULL and edge values: SELECT dbo.YourFunction(NULL), dbo.YourFunction(0), dbo.YourFunction(99);.
  11. Check dependent objects. Query sys.sql_expression_dependencies to see what references the function, then run a refresher such as sp_refreshsqlmodule on calling views and procedures if their metadata is stale.
  12. Re-script the function to confirm the final statement. The last executable line should now be the single RETURN.

Common Mistakes

  • Using SELECT instead of RETURN in a scalar function. SELECT @x = 1 assigns; it does not return. Only RETURN @x satisfies the rule.
  • Adding RETURN to an inline table-valued function. Inline TVFs end with a SELECT and cannot contain RETURN — 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 test SELECT at 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 SELECT of a column set belongs in a TVF.
  • Wrong convert. Editing CREATE OR ALTER carelessly, 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 RETURN at 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 FUNCTION on 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.