Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

But is there any automatic option to download all ?

like image 366
Neo Avatar asked Nov 16 '11 06:11

Neo


2 Answers

1) Open SQL Server Management Studio 2) Select your database in the Object Explorer 3) Right click > Tasks > Generate Scripts

enter image description here

4) Select only stored procedures to be scripted out

enter image description here

5) Following the wizard through the steps; on the next screen, pick option Single file per object and define a directory where to put those files:

enter image description here

With those options, you get one file per stored procedure, stored in the directory of your choice.

like image 93
marc_s Avatar answered Oct 08 '22 06:10

marc_s


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.

like image 42
Code Magician Avatar answered Oct 08 '22 07:10

Code Magician