对数百个相同的数据库使用一组存储过程

发布于 2024-12-27 14:16:32 字数 592 浏览 0 评论 0原文

我继承了一个项目,其中每个“客户”都有自己的数据库。有数百个数据库。当前未使用存储过程。

此处使用数据的最佳实践是什么?我是否将存储过程保留在“主”数据库中并使用动态 SQL 来处理数据?看来应该有更好的方法。我不想运行一个作业来将存储过程推送到数百个数据库以保持所有存储过程同步。

这个动态 SQL 有效,但我想要更好的方法。

CREATE PROCEDURE [Users_SELECT] 
@DataBase nvarchar(20),
@UserID uniqueidentifier
AS
BEGIN
DECLARE @sql nvarchar(max)
SET @sql = ''
SET @sql += 'SELECT * FROM ' + @DataBase + '.dbo.Users u '
SET @sql += 'WHERE u.UserID=@UserID '
EXEC sp_executesql @sql, N'@UserID uniqueidentifier', @UserID
END

我尝试了 EXEC sp_executesql 'USE ' + @DataBase + '; GO' 然后运行 ​​SELECT 但我无法让它工作。

I inherited a project where each 'customer' has its own database. There are hundreds of databases. Stored procedures are currently not being used.

What are the best practices for consuming data here? Do I keep my stored procedures in the "master" database and use dynamic SQL to muck with data? It seems like there should be a much better way. I don't want to have a job running to push stored procedures around hundreds of DBs to keep all the stored procedures in sync.

This dynamic SQL is working, but I want a better way.

CREATE PROCEDURE [Users_SELECT] 
@DataBase nvarchar(20),
@UserID uniqueidentifier
AS
BEGIN
DECLARE @sql nvarchar(max)
SET @sql = ''
SET @sql += 'SELECT * FROM ' + @DataBase + '.dbo.Users u '
SET @sql += 'WHERE u.UserID=@UserID '
EXEC sp_executesql @sql, N'@UserID uniqueidentifier', @UserID
END

I tried EXEC sp_executesql 'USE ' + @DataBase + '; GO' then running a SELECT but I couldn't get that working.

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

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

发布评论

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

评论(2

魔法少女 2025-01-03 14:16:32

我不明白将存储过程部署到每个数据库有什么问题。

您还必须部署对架构的更改,因此您希望拥有适当的基础设施来自动执行此操作。如果没有,那么是时候构建该基础设施了。

I don't see whats wrong with deploying the stored procedures to each and every database.

You have to deploy changes to the schema as well, so you hopefully have infrastructure in place to do it automatically. If not it is time build that infrastructure.

少女的英雄梦 2025-01-03 14:16:32

我想通了。这可能会让 SQL 专家非常生气,但我对这个解决方案很满意。

我在“master”中创建了我的存储过程。我知道这通常是禁忌。然后我将

USE master
EXEC sys.sp_MS_marksystemobject [sp_Users_SELECT]
GO

每个存储过程标记为系统对象。这样它们就可以在不同的上下文下被调用。然后我可以这样做:

USE DataBase200
EXEC sp_Users_SELECT

它将在 DataBase200 数据库的上下文中运行,而无需实际将存储过程部署到该数据库。到目前为止效果很好。请记住,您必须在存储过程名称前添加 sp_ 前缀,以便将它们识别为系统对象。

我希望这对某人有帮助。

I figured it out. This will probably make SQL experts extremely mad, but I'm happy with the solution.

I created my stored procedures in 'master'. I know this is usually a no-no. I then used

USE master
EXEC sys.sp_MS_marksystemobject [sp_Users_SELECT]
GO

to mark each sproc as a system object. This way they can be called while under a different context. I can then do this:

USE DataBase200
EXEC sp_Users_SELECT

and it will run in the context of the DataBase200 db, without having to actually deploy the sproc to that database. It is working great so far. Remember that you have to prefix your stored procedures names with sp_ in order for them to be recognized as system objects.

I hope this helps someone.

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