SP_BAK

  • Comments posted to this topic are about the item SP_BAK

  • Looks like an interesting proc, it would be great if you could post the formatted version.

    Here is SQL Server Central a forum to provide some assistance.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks, not sure why it didn't format correctly.

    /*********************************************************************************

    Name: SP_BAK_SVN

    Author: Sean D. Brian, ITX Enterprises

    Purpose: Creates a separate file for each sp on your server's c: drive. Useful for initial source control setup.

    ----------------------------------------------------------------------------

    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.

    ----------------------------------------------------------------------------

    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 script, in whole or in part, is

    prohibited without the author's express written consent.

    *********************************************************************************/

    IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Proc_Def]') AND type in ('U'))

    BEGIN

    DROP TABLE [dbo].[Proc_Def]

    END

    GO

    SET NOCOUNT ON;

    /*Notes:

    --1) Create a folder on the C: drive of the target server called SP_BAK

    --2) Change the Database Name below from YourDBName to the name of the target DB

    --3) Run the statement on the Target DB

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

    --5) The output should now show a separate output for each file created.

    --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

    */

    DECLARE @t TABLE (Test INT)

    declare c cursor local for

    (

    SELECT

    name

    FROM Sys.objects

    WHERE

    type in (N'P', N'PC',N'FN', N'IF', N'TF', N'FS', N'FT')

    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)

    DECLARE @PCOUNT INT

    SET @PCOUNT=0

    CREATE TABLE Proc_Def (P_Count INT,Def TEXT)

    Open c

    fetch next from c into @ID

    while @@fetch_status = 0

    BEGIN

    SET @PCOUNT=@PCOUNT+1

    INSERT Proc_Def

    SELECT @PCOUNT p, definition+' GO'

    FROM sys.sql_modules sm WITH ( NOLOCK )

    LEFT JOIN sys.objects so ON so.object_id = sm.object_id

    WHERE so.name=@ID

    DELETE FROM PROC_DEF

    WHERE P_Count!=@PCOUNT

    DECLARE @cmd VARCHAR(255)

    SET @cmd='BCP "SELECT Def FROM YourDBName..Proc_Def" queryout "c:\SP_BAK\'+@ID+'.SQL" -c -T'

    --PRINT @cmd

    EXEC XP_cmdshell @cmd

    fetch next from c into @ID

    END

    close c

  • Thanks for the script.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply