简体   繁体   English

是否可以在SQL Server 2008 r2的服务器级别添加用户定义函数?

[英]Can I add a User Defined Function at the server level in SQL Server 2008 r2?

I need to create a function that can be used across all databases in my SQL Instance. 我需要创建一个可以在我的SQL实例中的所有数据库中使用的函数。 Can I add a User Defined Function at the server level in SQL Server 2008 r2? 是否可以在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 . 然后在需要它们的地方使用完全限定的名称进行引用: Tools.dbo.MyUDF

Avoid the temptation to store things like this in the Master database. 避免将这样的事情存储在Master数据库中的诱惑。 That database belongs to SQL Server and should not be used for such purposes. 该数据库属于SQL Server,不应用于此目的。

Only by adding it to the master database. 仅通过将其添加到master数据库中。 You might want to give it the sp_ prefix so that it is looked for in the master database automatically. 您可能想给它sp_前缀,以便在master数据库中自动查找它。 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. 不可以,必须在特定的数据库中创建UDF。

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. 只要相关执行用户的SQL Server登录名具有足够的权限,任何数据库中的UDF都可以在任何其他数据库中使用。 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. 在一个单独的,有意义的数据库中创建此类UDF的一个原因是,要清楚地记录UDF(以及您可能在该DB中创建的任何其他对象)对其他用户,管理员,支持技术人员的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM