如何在所有数据库中创建sp。 [英] how to create sp in all database.

查看:92
本文介绍了如何在所有数据库中创建sp。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个sp我想在除系统数据库之外的所有数据库中部署它。如何创建这样的..请帮助我

解决方案

信息:如果您在master数据库中创建此存储过程,它将可用于所有数据库。



如果您需要在除master之外的所有数据库中创建存储过程请按照这些链接逐步说明这一点。

使用sp_MSForEachDB T-SQL示例在所有数据库上创建相同的存储过程 [ ^ ]



现在,如果(如果)你需要更多的系统数据库被排除,然后在脚本中进行相应的更改(如果条件)。



希望,它有帮助:)

我真的不建议在任何系统数据库中创建存储过程。在我看来,系统数据库应保持原样,它们只能由SQL Server(=系统)使用。



说完之后,你可以创建您想要的任何数据库中的过程,然后使用3部分表示法从其他数据库中调用它。



例如,将此过程创建到某个数据库中

 创建  procedure  proc1  as  
开始
print ' 我在这里');
end ;



和另一个数据库使用以下命令调用它。只需更改数据库的名称

 执行 databse_name..proc1 



当然,程序在它所在的数据库中运行,因此如果您需要来自调用数据库的数据,您需要使用足够的参数将其提供给过程,或者将连接编码到其中的另一个数据库中。程序。



增加:



如果要在多个数据库中创建过程(系统数据库除外)您可以使用游标查询数据库,切换到正确的数据库,然后在其中创建过程。



由于包含创建过程的SQL语句必须是批处理中的第一个语句,因此创建必须在USE语句之后动态完成。



请考虑以下示例:

  DECLARE   @ databasename   nvarchar  200 
DECLARE @ sql nvarchar (max)
DECLARE curDatabases CURSOR FOR
SELECT name
FROM sys.databases
WHERE owner_sid<> 1 ; - 使用相关条件仅包含所需数据库

OPEN curDatabases

FETCH NEXT FROM curDatabases INTO @ databasename
WHILE @@ FETCH_STATUS = 0
BEGIN
PRINT @ databasename
SET @sql = ' 使用' + @ databasename + '
exec(''创建程序dbo.proc1为
开始
print(''''我在这里'''');
end'')'

EXEC sp_executesql @ stmt = @sql

FETCH NEXT FROM curDatabases INTO @ databasename
END
CLOSE curDatabases;
DEALLOCATE curDatabases;





另一种方式是使用 sp_msforeachdb [ ^ ]和SQL语句中包含一个条件,如果数据库是系统数据库,则不运行创建过程。


I have one sp i want to deploy it in all database except system databases..How can create like that..please help me out

解决方案

Info: If you create this stored procedure in master database, it will be available for all the database.

If you need the stored procedure to be created in all databases except master then follow these link which describes step by step to do this.
Create Same Stored Procedure on All Databases using sp_MSForEachDB T-SQL Example[^]

Now, as (if) you need more system databases to be excluded then, make the changes accordingly in the script (in if condition).

Hope, it helps :)


I really don't advice to create a stored procedure in any of the system databases. In my opinion system databases should be left as-is and they should only be used by the SQL Server (=system).

Having that said, you can create the procedure in any database you want and then call it from other databases using 3-part notation.

For example create this procedure into some database

create procedure proc1 as
begin
   print('I am here');
end;


and from another database call it using the following command. Just change the name of the database

execute databse_name..proc1


Of course the procedure operates in the database it's located so if you need data from the calling database, you need either to provide it to the procedure using sufficient parameters or code the 'connection' to another database inside the procedure.

Addition:

If you want to create the procedure in multiple databases (except system databases) you can use a cursor to query the databases, switch to proper database and then create the procedure in it.

Since the SQL statement containing the procedure creation must be the first statement inside a batch, the creation must be done 'dynamically' after the USE statement.

Consider the following example:

DECLARE @databasename nvarchar(200)
DECLARE @sql nvarchar(max)
DECLARE curDatabases CURSOR FOR 
SELECT name
FROM sys.databases 
WHERE owner_sid <> 1; -- use relevant condition to include only desired databases

OPEN curDatabases

FETCH NEXT FROM curDatabases INTO @databasename
WHILE @@FETCH_STATUS = 0
BEGIN
    PRINT @databasename
	SET  @sql = 'use ' + @databasename + '
exec (''create procedure dbo.proc1 as
begin
   print(''''I am here'''');
end'')'
	EXEC sp_executesql @stmt = @sql

    FETCH NEXT FROM curDatabases INTO @databasename
END 
CLOSE curDatabases;
DEALLOCATE curDatabases;



Another way would be to use sp_msforeachdb[^] and in the SQL statement include a condition not to run the create procedure if the database is a system database.


这篇关于如何在所有数据库中创建sp。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆