SQL Server Error 1074: Temporary Functions Fix
In this article
Error 1074 in SQL Server means you tried to create a temporary function (a function whose name starts with a single #) and SQL Server does not allow that. SQL Server supports temporary tables and temporary stored procedures, but not temporary functions. The direct fix is to create a permanent function in a schema (for example dbo) and, if you need per-session isolation, use a unique name or a table-valued variable instead. This guide walks through the cause, the correct alternatives, and how to adjust code that was written for another database platform.
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 create objects in the target database (CREATE FUNCTION permission in the schema, or db_ddladmin/db_owner).
- The original script or stored procedure that contains the temporary function definition.
- A test database where you can safely create and drop objects.
Step-by-Step
- Confirm the error source. Run the failing statement in SSMS. The message “Creation of temporary functions is not allowed” appears when the function name begins with a single number sign, such as
CREATE FUNCTION #MyFunc(). Double-check that the name really starts with # and not with a normal character. - Understand what SQL Server does and does not support. Temporary tables (#temp) and temporary stored procedures (#proc) are allowed. Temporary functions are not. There is no server setting, trace flag, or compatibility level that enables them, so do not spend time looking for a toggle.
- Replace the temporary function with a permanent scalar or table-valued function. Create it in a schema, typically dbo, with a normal name:
CREATE FUNCTION dbo.MyFunc (@id int) RETURNS int AS BEGIN RETURN @id * 2 END;Then change every call from#MyFunctodbo.MyFunc. If the function is only used inside one stored procedure, you can also inline the logic directly into the procedure body. - If you need per-session isolation, use a unique name. Generate a name that includes a session or job identifier, create the function with dynamic SQL, and drop it when done. This is more complex and requires care with permissions and concurrency, but it avoids collisions between sessions. Example pattern: build a name like
dbo.MyFunc_plus a value from@@SPID, create it withsp_executesql, call it, then drop it in a TRY/CATCH block. - If the function is only used for a single query, consider a table variable or a CTE. Many scripts that use temporary functions are really trying to reuse a calculation. A common table expression (CTE) or a derived table often replaces the function entirely and avoids object creation. For row-by-row logic, a temporary table plus a JOIN can be simpler than a function.
- Update all references. Search the database for the old name using
sys.sql_modulesor a text search in SSMS (Ctrl+Shift+F). Change every call site, including dynamic SQL strings, views, and other stored procedures. If you renamed the function, also check for schema-qualified calls. - Test in a non-production database. Run the updated script, confirm the function returns the expected results, and check that permissions are correct. If the function is used by an application, test the application path that calls it.
- Clean up any leftover objects. If you created test functions or temporary tables during troubleshooting, drop them. Use
DROP FUNCTION dbo.MyFunc;and verify withSELECT * FROM sys.objects WHERE type IN ('FN','IF','TF');.
Common Mistakes
- Assuming a trace flag or compatibility level will help. No configuration option enables temporary functions in SQL Server. The restriction is by design.
- Using a double number sign (##). Global temporary objects also do not apply to functions. The same error appears.
- Forgetting to schema-qualify the new function. Calls to
MyFuncwithoutdbo.may resolve to a different schema or fail depending on the user's default schema. Always usedbo.MyFunc. - Leaving dynamic SQL strings unchanged. If the function name is built inside a string, a simple search may miss it. Check all code paths that construct SQL text.
- Creating the permanent function in the wrong database. Functions are database-scoped. If the calling code runs in another database, create the function there or use a three-part name.
- Not handling permissions. A permanent function requires EXECUTE permission for callers. Temporary objects inherit some session behavior, but permanent functions do not, so grant access explicitly.
Tips
- Prefer inline table-valued functions over multi-statement table-valued functions when performance matters; they often optimize better.
- If you are porting code from PostgreSQL, note that PostgreSQL allows temporary functions but SQL Server does not. Rewrite those functions as permanent objects or inline the logic.
- Use
CREATE OR ALTER FUNCTION(available in SQL Server 2016 SP1 and later) to simplify deployments and avoid drop-and-create scripts. - Keep function logic simple. Scalar functions that run per row can slow large queries; consider set-based alternatives.
- Document any dynamically created function names so that cleanup jobs can find and drop them.
Troubleshooting
If you still see error 1074 after changes, search the entire codebase for a remaining # prefix on a function name. Check dynamic SQL, SQL Agent job steps, and application connection strings that might run ad-hoc batches. If the error appears only for one login, verify that login's default schema and permissions; a missing EXECUTE grant on the permanent function can produce a different error, but permission issues sometimes surface as creation failures when the code attempts to create objects at runtime. If you are using a unique-name pattern, confirm that the generated name does not exceed the 128-character identifier limit and that the drop statement runs even when the call fails (use TRY/CATCH). Finally, if the script was generated by an ORM or migration tool, check the tool's documentation for a setting that controls temporary object naming; some tools emit # prefixes for certain databases and need to be configured for SQL Server.
Frequently Asked Questions
Can I enable temporary functions with a trace flag?
No. SQL Server does not support temporary functions, and no trace flag or server setting changes that.
What is the difference between a temporary table and a temporary function?
Temporary tables (names starting with #) are session-scoped objects that SQL Server supports. Temporary functions are not supported at all.
Can I use a temporary stored procedure instead?
Yes, temporary stored procedures (#proc) are allowed. If your logic can be expressed as a procedure rather than a function, that is a valid alternative.
Will a table variable work as a replacement?
Often yes. If the function returns a small set of rows, a table variable or CTE can replace it without creating a permanent object.
Does Azure SQL Database allow temporary functions?
No. Azure SQL Database follows the same rule as SQL Server: temporary functions are not allowed.
How do I find all references to the old function name?
Use SSMS's Find in Files (Ctrl+Shift+F) across your script folder, or query sys.sql_modules with a LIKE filter on the definition column.