Is there a way or how to make a global UDF like it can be accessed everywhere (as long as it is inside the scope of your server) in SQL Server?
2 Answers
Assuming the person calling the UDF has appropriate permissions, you should be able to create the function in any database (including the system databases, though I'd steer clear of that). You can call the function by prefacing the function with the database name. i.e.
SELECT * FROM [database].[dbo].[function_name]

- 2,601
- 1
- 20
- 30
-
Yeah I see the point though the function is still owned by the db where it was created.I am actually looking for something like we can say that db1, db2, db3 ... owns this function or this function is shared by all of them. Good point though. – JanLeeYu Feb 18 '16 at 03:19
Be aware that UDFs, as a general rule, perform poorly in SQL Server as the optimizer has no means of properly costing them.
You can lessen the suck, if the function does not access tables, by creating it as schemabound
On to the question at hand - how do you get a global function? You can't. You can get global stored procedures by putting them in the master database but functions themselves are treated differently (or procs are treated differently -> half-full or half-empty)
I'd probably go the synonym route myself
- create system function
- Can I add a User Defined Function at the server level in SQL Server 2008 r2?
- https://social.msdn.microsoft.com:443/Forums/sqlserver/en-US/d49ff86b-c518-458e-a6e1-a74d7865c8d9/sqlserver-2008-adding-function-to-master-database-and-use-call-it-from-other-database?forum=transactsql