简体   繁体   English

如何从特定数据库下载所有存储过程

[英]How can I download all stored procedures from a specific database

I have to download all stored procedures from a specific database. 我必须从特定数据库下载所有存储过程。

There are around 130 stored procedures, and I can do it manually like by doing save as a file each one. 大约有130个存储过程,我可以手动完成,就像每个文件一样保存。

But is there any automatic option to download all ? 但有没有自动选项下载所有?

1) Open SQL Server Management Studio 2) Select your database in the Object Explorer 3) Right click > Tasks > Generate Scripts 1)打开SQL Server Management Studio 2)在对象资源管理器中选择数据库3)右键单击>任务>生成脚本

在此输入图像描述

4) Select only stored procedures to be scripted out 4)仅选择要编写的存储过程

在此输入图像描述

5) Following the wizard through the steps; 5)按照向导完成步骤; on the next screen, pick option Single file per object and define a directory where to put those files: 在下一个屏幕上,选择Single file per object选项Single file per object并定义放置这些文件的目录:

在此输入图像描述

With those options, you get one file per stored procedure , stored in the directory of your choice. 使用这些选项, 每个存储过程可以获得一个文件 ,存储在您选择的目录中。

You can do this in management studio - Right click the database you want and select tasks -> Generate scripts -> go through the wizard. 您可以在管理工作室中执行此操作 - 右键单击​​所需的数据库并选择任务 - >生成脚本 - >完成向导。 You can then specify just stored procedures etc. 然后,您可以指定存储过程等。

You can also use a script like this: 你也可以使用这样的脚本:

SET NOCOUNT ON
DECLARE @Test TABLE (Id INT IDENTITY(1,1), Code VARCHAR(MAX))

INSERT INTO @Test (Code)
SELECT 'IF object_ID(N''[' + schema_name(schema_id) + '].[' + Name + ']'') IS NOT NULL
          DROP PROCEDURE ['+ schema_name(schema_id) +' ].[' + Name + ']' + CHAR(13) + CHAR(10) + 'GO' + CHAR(13) +CHAR(10) +
           OBJECT_DEFINITION(OBJECT_ID) + CHAR(13) +CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
            FROM sys.procedures
            WHERE is_ms_shipped = 0

DECLARE @lnCurrent INT, @lnMax INT
DECLARE @LongName VARCHAR(MAX)

SELECT @lnMax = MAX(Id) FROM @Test
SET @lnCurrent = 1
WHILE @lnCurrent <= @lnMax
      BEGIN
            SELECT @LongName = Code FROM @Test WHERE Id = @lnCurrent
            WHILE @LongName <> ''
               BEGIN
                   PRINT LEFT(@LongName,8000)
                   SET @LongName = SUBSTRING(@LongName, 8001, LEN(@LongName))
               END
            SET @lnCurrent = @lnCurrent + 1
      END

You can also shift+click to select all the stored procedures and you can then right-click and script them to a file. 您也可以按住Shift键并单击以选择所有存储过程,然后右键单击并将其编写为文件。

You can use DB pro (Visual Studio tools for Database) to do this too. 您也可以使用DB pro(Visual Studio数据库工具)来执行此操作。 For more information check this - https://www.mssqltips.com/sqlservertip/2971/creating-a-visual-studio-database-project-for-an-existing-sql-server-database/ 有关详细信息,请查看此信息 - https://www.mssqltips.com/sqlservertip/2971/creating-a-visual-studio-database-project-for-an-existing-sql-server-database/

Edit: Updated the stale link. 编辑:更新陈旧链接。

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

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