I need to create a function that can be used across all databases in my SQL Instance. Can I add a User Defined Function at the server level开发者_运维百科 in SQL Server 2008 r2?
I would create a small database for yourself called "Tools" or something like that to store these kinds of functions. Then reference them with a fully qualified name where you need them: Tools.dbo.MyUDF
.
Avoid the temptation to store things like this in the Master database. That database belongs to SQL Server and should not be used for such purposes.
Only by adding it to the master database. You might want to give it the sp_
prefix so that it is looked for in the master database automatically. Additionally dependent upon the purpose of the function you might need to mark it as a system object.
No, a UDF must be created in a particular DB.
A UDF in any DB can be used in any other DB as long as the SQL Server login for the relevant executing user(s) have sufficient permission to do so. One reason to create such UDFs in a separate, meaningfully-named DB would be to clearly document the purpose of the UDF (and any other objects you might create in that DB) for other users, administrators, support techs, etc.
精彩评论