简体   繁体   English

在存储过程中更改数据库名称

[英]Changing Database Name In Stored Procedures

I need to change the database name in SQL SERVER 2008 and use it in another project. 我需要在SQL SERVER 2008中更改数据库名称,并在另一个项目中使用它。 However it consist hundreds of stored procedures and the name of the database should be changed in the stored procedures as well. 但是,它由数百个存储过程组成,并且数据库名称也应在存储过程中更改。 Is there any way to do this? 有什么办法吗?

If you right mouse click a Database and choose Tasks->Generate Scripts. 如果右键单击数据库,然后选择任务->生成脚本。 Go through the Wizard and it will create a SQL script for you. 通过向导,它将为您创建一个SQL脚本。 Make sure that you select all of the necessary options eg Create Database, Stored Procedures etc. Once finished, you'll have a big script. 确保选择所有必需的选项,例如“创建数据库”,“存储过程”等。完成后,您将拥有一个大型脚本。 Find and replace the database name. 查找并替换数据库名称。

I would suggest that you export all your stored procedures as sql-files and then take a nice texteditor (like Notepad++) and make a file-search&replace-action to change all the names referenced inside the sql-files. 我建议您将所有存储过程导出为sql文件,然后使用一个不错的texteditor(如Notepad ++)并进行file-search&replace-action来更改sql文件中引用的所有名称。

There is no other way around as far as I can say :-( 据我所知,没有其他方法可以解决:-(

接下来,我将做一件事:我将通过Management Studio为数据库生成所有脚本( Right Click on DB -> Tasks -> Generate scripts ),然后替换数据库名称( Ctrl + H )。

Right click on the database, Tasks -> Generate scripts, select stored procedure, and open the scripts in sql editor. 右键单击数据库,然后单击任务->生成脚本,选择存储过程,然后在sql编辑器中打开脚本。

Search and replace the database or string names. 搜索并替换数据库或字符串名称。 replace Create procedure with Alter procedure. 将“创建过程”替换为“更改过程”。 Compile and done. 编译完成。

There is no way of doing this automatically. 无法自动执行此操作。 You would have to manually change every stored procedure. 您将必须手动更改每个存储过程。

Do you really need to change the name? 您真的需要更改名称吗?

Wouldn't it be better to keep everything the same and use a different server or server instance? 将所有内容保持不变并使用其他服务器或服务器实例会更好吗? Are you going to have to maintain database changes through these two datbases with the different names? 您是否需要通过这两个具有不同名称的数据库维护数据库更改?

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

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