Technical Article

ITX_SP_BAK

,

1) Change the Database Name below from YourDBName to the name of the target DB.

2) If XP_cmdshell is blocked, use the statement below to enable it.

--Use when XP_cmdshell gets blocked

-- To allow advanced options to be changed.

EXEC sp_configure 'show advanced options', 1

GO

-- To update the currently configured value for advanced options.

RECONFIGURE

GO

-- To enable the feature.

EXEC sp_configure 'xp_cmdshell', 1

GO

-- To update the currently configured value for this feature.

RECONFIGURE

GO

3) Run the statement on the Target DB.

4) Exec ITX_SP_BAK A file will be created on the target server's c drive.

@P INT, --Back up Procs 1=yes 0=no

@FN INT, --Back up Functions 1=yes 0=no

@V INT --Back up Views 1=yes 0=no

/*********************************************************************************    Name: SP_BAK_MIGRATE Author: Sean D. Brian, ITX Enterprises  Purpose: Creates a single sql install file for all stored procs in a database. Useful for migrating procs from one server to another.  ---------------------------------------------------------------------------- DISCLAIMER:  This code and information are provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the implied  warranties or merchantability and/or fitness for a particular purpose. ------------------------A---------------------------------------------------- LICENSE:  This script is free to download and use for personal, educational,  and internal corporate purposes, provided that this header is preserved.  Redistribution or sale of this index defrag script, in whole or in part, is  prohibited without the author's express written consent.    Notes:    1) Change the Database Name below from YourDBName to the name of the target DB.     2) If XP_cmdshell is blocked, use the statement below to enable it.         --Use when XP_cmdshell gets blocked        -- To allow advanced options to be changed.        EXEC sp_configure 'show advanced options', 1        GO        -- To update the currently configured value for advanced options.        RECONFIGURE        GO        -- To enable the feature.        EXEC sp_configure 'xp_cmdshell', 1        GO        -- To update the currently configured value for this feature.        RECONFIGURE        GO    3) Run the statement on the Target DB. A file will be created on the target server's c drive.*********************************************************************************/ CREATE PROC ITX_SP_BAK        @P INT,    @FN INT,    @V INTASBEGINSET NOCOUNT ON;DECLARE @t TABLE (Test INT)declare c cursor local for     (        SELECT             name         FROM Sys.objects         WHERE             (            @FN=1 AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')            OR            @P=1 AND type IN (N'P', N'PC')            OR             @V=1 AND type IN (N'V')            )            AND name NOT LIKE '%sp_MSdel%' --exclude replication procs            AND name NOT LIKE '%sp_MSins%'            AND name NOT LIKE '%sp_MSupd%'    )Declare @ID NVARCHAR(MAX)CREATE TABLE Proc_Def (Def TEXT)Open cfetch next from c into @IDwhile @@fetch_status = 0     BEGIN        INSERT Proc_Def        SELECT definition        FROM sys.sql_modules sm WITH ( NOLOCK ) LEFT JOIN sys.objects so ON so.object_id = sm.object_id        WHERE so.name=@ID         --EXEC sys.sp_helptext @objname = @ID        INSERT Proc_Def        SELECT    'GO'        --Print OBJECT_DEFINITION(@ID))+' GO'        --PRINT @IDfetch next from c into @ID    ENDclose cEXEC XP_cmdshell 'BCP "SELECT * FROM YourDBName..Proc_Def" queryout "c:\SP_Script.SQL" -c -T'DROP TABLE Proc_DefSET NOCOUNT OFF;END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating