可以创建用于所有数据库的 SQL 存储过程

发布于 2024-12-03 10:31:04 字数 121 浏览 0 评论 0原文

我的 SQL Server 中的一个数据库中有一个存储过程,它可以同时为该特定数据库的所有存储过程设置权限。有没有办法创建这个存储过程,让我可以从 SQL Server 中的任何数据库轻松调用它,如果是的话,我该如何做这样的事情

I have a stored procedure, in one database within my SQL server, that sets permissions to all stored procedures at once for that particulat database. Is there a way to create this stored procedure in a way were I can call it easily from any database within the SQL server and if so how do I go about doing such a thing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

糖果控 2024-12-10 10:31:05

虽然授予所有过程执行这一特定问题的最佳解决方案是 marc_s 提供的解决方案,但实际问题是有一种方法可以创建单个存储过程并使其可用于所有数据库。

执行此操作的方法记录在 https://nickstips.wordpress.com/2010/10/18/sql-making-a-stored-procedure-available-to-all-databases/

  1. 中创建存储过程>master 数据库。
  2. 它必须以sp_ 开头,例如sp_MyCustomProcedure
  3. 执行sys.sp_MS_marksystemobject 传递过程的名称,例如EXEC sys。 sp_MS_marksystemobject sp_MyCustomProcedure

这是一个简单的示例,仅选择当前数据库的名称:

use master
go

create procedure dbo.sp_SelectCurrentDatabaseName as begin
    select db_name()
end
go

execute sys.sp_MS_marksystemobject sp_SelectCurrentDatabaseName
go

调用 exec然后,任何数据库上的 dbo.sp_SelectCurrentDatabaseName 都可以工作。

为了将该过程标记为不是系统对象,在 https://social.msdn.microsoft.com/Forums/sqlserver/en-US/793d0add-6fd9-43ea-88aa-c0b3b89b8d70/how-do-i-undo-spmsmarksystemobject?forum=sqltools但最安全、最简单的方法是删除并重新创建该过程。

警告

当然,创建这样的系统过程违反了不要将自己的过程命名为 sp_xxx 的通用规则,因为它们将来可能与内置过程发生冲突SQL Server 的版本。因此,这应该小心地完成,而不仅仅是创建一堆您认为有用的随机命名的过程。

避免这种情况的常见简单方法是将您自己的公司/个人前缀添加到 Microsoft 不太可能使用的过程中,例如 sp_MyCompany_MyCustomProcedure

While the best solution to this specific question of granting execute to all procedures is the one provided by marc_s, the actual question was is there a way to create a single stored procedure and make it available to all databases.

The way to do this is documented at https://nickstips.wordpress.com/2010/10/18/sql-making-a-stored-procedure-available-to-all-databases/:

  1. Create the stored procedure in the master database.
  2. It must be named to start with sp_, e.g. sp_MyCustomProcedure
  3. Execute sys.sp_MS_marksystemobject passing the name of the procedure, e.g. EXEC sys.sp_MS_marksystemobject sp_MyCustomProcedure

Here is a simple example which just selects the name of the current database:

use master
go

create procedure dbo.sp_SelectCurrentDatabaseName as begin
    select db_name()
end
go

execute sys.sp_MS_marksystemobject sp_SelectCurrentDatabaseName
go

Calling exec dbo.sp_SelectCurrentDatabaseName on any database will then work.

To mark the procedure as not a system object, there a some nasty hacks suggested at https://social.msdn.microsoft.com/Forums/sqlserver/en-US/793d0add-6fd9-43ea-88aa-c0b3b89b8d70/how-do-i-undo-spmsmarksystemobject?forum=sqltools but it is safest and easiest to just drop and re-create the procedure.

Caveat

Of course creating system procedures like this is breaking the common rule of not naming your own procedures as sp_xxx, due to the possibility of them conflicting with built-in procedures in future versions of SQL Server. Therefore this should be done with care and not just create a load of randomly named procedures which you find useful.

A common simple way to avoid this is to add your own company/personal prefix to the procedure which Microsoft is unlikely to use, e.g. sp_MyCompany_MyCustomProcedure.

超可爱的懒熊 2024-12-10 10:31:05

我的 SQL 服务器内的一个数据库中有一个存储过程,
立即为该特定的所有存储过程设置权限
数据库。

您可以更轻松地归档相同的结果:

  • 创建一个新角色,例如db_executor

    创建角色 db_executor
    
  • 授予该角色执行权限而不指定任何对象:

    将执行授予 db_executor
    

该角色现在拥有所有存储过程和函数的执行权限 - 它甚至会获得相同的权限您添加到数据库中的任何未来存储过程!

只需将此角色分配给您需要的用户即可完成......

I have a stored procedure, in one database within my SQL server, that
sets permissions to all stored procedures at once for that particular
database.

You could archive the same result much easier:

  • create a new role, e.g. db_executor

    CREATE ROLE db_executor
    
  • grant that role execute permissions without specifying any objects:

    GRANT EXECUTE TO db_executor
    

This role now has execute permissions on all stored procedures and functions - and it will even get the same permissions for any future stored procedure that you add to your database!

Just assign this role to the users you need and you're done....

心的位置 2024-12-10 10:31:05

您尝试过由 3 部分或 4 部分组成的名称吗?

InstanceName.DatabaseName.dbo.usp_Name

该过程又可以使用相同的约定引用其他数据库中的对象。因此,您可以参数化要操作的数据库名称,并使用动态 SQL 生成 4 个部分名称来引用系统表等对象。

Have you tried a 3 or 4 part name?

InstanceName.DatabaseName.dbo.usp_Name

That procedure could in turn reference objects in other databases using the same conventions. So you could parameterize the name of the database to be operated on and use dynamic SQL to generate 4 part names to reference objects such as system tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文