Smart Systems for Home

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

Sql Server Error 557 Only Functions and Some Extended Stored Procedures

· 5 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 557 occurs when you try to execute a stored procedure or a scalar function from inside a user-defined function. The full message is: "Only functions and some extended stored procedures can be executed from within a function." The fix is to move the procedure call outside the function, or replace it with a function that returns the needed value. Functions in SQL Server are deliberately restricted: they must be deterministic, cannot modify data, and cannot call procedures that might have side effects. This guide explains why the error appears and how to restructure your code so it runs correctly.

What You'll Need

  • Access to SQL Server Management Studio (SSMS) or Azure Data Studio, or another client that can run T-SQL.
  • Permission to view and alter the function and procedure definitions (typically ALTER on the schema or db_owner).
  • The name of the function and the procedure it is trying to call.
  • A way to test changes on a development or staging database before applying them to production.

Step-by-Step

  1. Identify the failing function. Run the code that triggers the error and note the function name in the error message or in the stack trace. If the error comes from a procedure that calls a function, the function is the one containing the illegal EXEC statement.
  2. Open the function definition. In SSMS, expand the database, then Programmability > Functions > Scalar-valued Functions (or Table-valued Functions), right-click the function, and choose Script as > ALTER To > New Query Editor Window. Menu labels can differ slightly by SSMS version.
  3. Locate the EXEC or procedure call. Look for lines such as EXEC dbo.MyProcedure or EXEC(@sql) inside the function body. Any call to a stored procedure is illegal inside a function, even if the procedure only reads data.
  4. Decide on the replacement. If the procedure returns a single value, rewrite it as a scalar function and call that function instead. If it returns a rowset, rewrite it as an inline table-valued function. If the procedure performs inserts, updates, deletes, or dynamic SQL, it cannot be called from a function at all; you must move the call outside the function.
  5. Create the replacement function. For a scalar value, use CREATE FUNCTION dbo.GetValue (@id INT) RETURNS INT AS BEGIN RETURN (SELECT ...); END. For a rowset, use CREATE FUNCTION dbo.GetRows (@id INT) RETURNS TABLE AS RETURN (SELECT ...);. Keep the logic deterministic and avoid side effects.
  6. Edit the original function. Replace the EXEC line with a call to the new function. For a scalar function, use SET @var = dbo.GetValue(@id);. For a table-valued function, use SELECT * FROM dbo.GetRows(@id); inside the function body. Save and execute the ALTER statement.
  7. Move data-changing logic out of the function. If the procedure inserts, updates, or deletes, remove that logic from the function entirely. Call the procedure from the outer stored procedure or application code after the function returns its result. Functions cannot modify data, so this separation is required.
  8. Test the change. Run the original query or procedure that triggered the error. Verify that the results match expectations and that no error 557 appears. Test with edge cases such as NULL inputs and empty result sets.
  9. Deploy to production. Script the changes and apply them during a maintenance window. Keep a rollback script that restores the previous function definition in case of issues.

Common Mistakes

  • Trying to call a procedure that only reads data. Even a read-only procedure cannot be called from a function. The restriction applies to all stored procedures, not just those that modify data.
  • Using dynamic SQL inside a function. EXEC(@sql) and sp_executesql are procedure calls and are not allowed. Rewrite the logic without dynamic SQL, or move it outside the function.
  • Forgetting that functions must be deterministic. Calling GETDATE() or NEWID() inside a function can cause errors or prevent indexing. Pass such values as parameters instead.
  • Assuming table-valued functions can call procedures. They cannot. The same restriction applies to scalar and table-valued functions.
  • Overlooking nested calls. If function A calls function B, and B calls a procedure, the error still occurs. Check the entire call chain.

Tips

  • Keep functions small and focused on computation. Put data modification in stored procedures.
  • Use inline table-valued functions instead of multi-statement ones when possible; they often perform better and are easier to optimize.
  • When you need to log or audit from within a function, return the data to the caller and let the caller write the log.
  • Use SET NOCOUNT ON in procedures to reduce network traffic, but remember it does not make a procedure callable from a function.
  • Review code for EXEC statements before deploying functions to catch this error early.

Troubleshooting

If the error persists after editing, check for nested function calls that still contain EXEC statements. Use sp_helptext 'dbo.YourFunction' to view the current definition and confirm the change was applied. If you cannot alter the function because it is schema-bound, you may need to drop and recreate it, which requires dropping dependent objects first. In that case, script out dependencies before making changes. If the function is used in an indexed view or computed column, altering it may require dropping the index or column, so plan for that downtime. When the logic truly requires a procedure call, refactor the design: move the procedure call to the outer layer and pass the result into the function as a parameter. Always test in a non-production environment first.

Frequently Asked Questions

Can a function call a stored procedure that only reads data?

No. SQL Server does not allow any stored procedure call from inside a function, regardless of whether the procedure modifies data.

What is the difference between a function and a stored procedure?

A function returns a value or table and must be deterministic and side-effect free. A stored procedure can modify data, call other procedures, and use dynamic SQL.

Can I use EXEC inside a table-valued function?

No. The same restriction applies to scalar and table-valued functions. Use a SELECT statement or call another function instead.

How do I return multiple rows from a function?

Use an inline table-valued function with a RETURN (SELECT ...) statement, or a multi-statement table-valued function with a table variable.

Why does my function work in one database but not another?

Check for schema differences or nested procedure calls that exist in one database but not the other. Also verify that the function is not schema-bound in a way that prevents changes.

Can I call a function from a stored procedure?

Yes. Stored procedures can call functions freely. The restriction only applies in the other direction.