且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在所有数据库中创建sp。

更新时间:2023-12-01 10:43:52

信息:如果您在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.