Technical Article

Script to export Stored Procedures

,

This procedure when executed with the correct parameters, exports all stored procedures from a database into different .SQL files in the path specified.

Usage  :

exec USP_PRINT_PROCS username, password, servername, databasename, directory path

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

IF EXISTS (SELECT name 
   FROM   sysobjects 
   WHERE  name = N'USP_PRINT_PROCS' 
   AND   type = 'P')
    DROP PROCEDURE USP_PRINT_PROCS
GO

CREATE PROCEDURE USP_PRINT_PROCS @UNAME VARCHAR(100), @PWD 

VARCHAR(100), @SERVERNAME VARCHAR(100), @DBNAME VARCHAR(100),  @PATH 

VARCHAR(100)
AS
DECLARE

@HELPTEXT_STMT VARCHAR(8000),
@HELPTEXT VARCHAR(8000),
@PROCNAME VARCHAR(8000),
@ONE_FILE_STMT VARCHAR(8000),
@CONVERTED_DATE VARCHAR(10)


SET @HELPTEXT_STMT = ''
SET @HELPTEXT = ''
SET @PROCNAME = ''
SET @ONE_FILE_STMT = ''
SET @CONVERTED_DATE = ''


CREATE TABLE #INT_TBL
(SPHELP_STMT VARCHAR(800), PROC_NAME VARCHAR(800))

INSERT INTO #INT_TBL(SPHELP_STMT, PROC_NAME)
SELECT 'sp_helptext ' + name  , NAME
FROM SYSOBJECTS
WHERE TYPE = 'P' AND
NAME LIKE 'USP%'


DECLARE SPHELP_CURSOR CURSOR FOR 
SELECT * FROM #INT_TBL

OPEN SPHELP_CURSOR

FETCH NEXT FROM SPHELP_CURSOR INTO @HELPTEXT, @PROCNAME
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @HELPTEXT_STMT = 'MASTER..XP_CMDSHELL ' + '''OSQL.EXE -Q"' + 

@HELPTEXT + '"  -U' + @UNAME + ' -P' + @PWD + '  -d' + @DBNAME + '   

-S' + @SERVERNAME + ' -h-1 > ' + @PATH + @PROCNAME + '.SQL'''
    EXEC(@HELPTEXT_STMT)
    FETCH NEXT FROM SPHELP_CURSOR INTO @HELPTEXT, @PROCNAME
END

   SELECT @CONVERTED_DATE = CONVERT(VARCHAR(10),GETDATE(),112)

    SET @ONE_FILE_STMT = 'MASTER..XP_CMDSHELL '' TYPE  ' + @PATH + 

'*.SQL >> ' + @PATH + @DBNAME + '_' + @CONVERTED_DATE + 

'.SQL'',NO_OUTPUT'
    EXEC(@ONE_FILE_STMT)

CLOSE SPHELP_CURSOR
DEALLOCATE SPHELP_CURSOR


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating