Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Apr 16

Written by: Steve Gray
4/16/2010 11:17 AM  RssIcon

 

This code will dump the text of any object that you want to a text file. I use it as a backup, and to easily search a code base for something.

First, enable ‘Automation Procedures’ using this script:

 

Code Snippet
  1. sp_configure 'show advanced options', 1;
  2. GO
  3. RECONFIGURE;
  4. GO
  5. sp_configure 'Ole Automation Procedures', 1;
  6. GO
  7. RECONFIGURE;
  8. GO

 

This grant permissions:

Code Snippet
  1. grant exec on sp_oacreate to myUser
  2. grant exec on sp_oaMethod to myUser
  3. grant exec on sp_oaDestroy to myUser

Then run this script to dump your objects. You’ll need to configure the line that set the name and location of the output file, and the query that decides what objects to grab.

 

Code Snippet
  1. --runs all the stored procs and triggers in the current db to a text file
  2. DECLARE
  3.     @vchrFile VARCHAR(1000)
  4. --initialize
  5. DECLARE @db varchar(15)
  6. DECLARE @dt varchar(20)
  7. set @db = upper(db_name())
  8. set @dt = convert(varchar(10),getdate(),112)
  9. SET @vchrFile = 'h:\NGB-SQL-02.' + @db + ' ' + @dt + '.txt'
  10. DECLARE
  11.     @vchrFileID INT ,
  12.     @FS INT ,
  13.     @RC INT ,
  14.     @vchrStoredProcName varchar(8000),
  15.     @vchrChar varchar(1),-- holds the current character that we are evaluating
  16.     @vchrLine varchar(8000),-- holds the line that we are about to print
  17.     @intPos int,
  18.     @vchrPrevChar varchar(1),
  19.     @intAscii int,
  20.     @intPrevAscii int,
  21.     @vchrSysCommentText varchar(8000)
  22. --=======================================================================
  23. -- open the output file
  24. --=======================================================================
  25. EXEC @RC = sp_OACreate 'Scripting.FileSystemObject', @FS OUT
  26. IF @RC <> 0
  27.     PRINT 'Error:  Creating the file system object'
  28. -- Opens the file specified by the @vchrFile input parameter
  29. EXEC @RC = sp_OAMethod @FS , 'OpenTextFile' , @vchrFileID OUT ,
  30.     @vchrFile , 8 , 1
  31. -- Prints error if non 0 return code during sp_OAMethod OpenTextFile execution
  32. IF @RC <> 0 begin
  33.     PRINT 'Error:  Opening the specified text file'
  34.     return
  35. end
  36. --======================================================================
  37. --gather data on stored procedure into table _dbText
  38. --======================================================================
  39. DECLARE curStoredProcs CURSOR KEYSET FOR
  40.     SELECT Name from sysobjects
  41.         where
  42.             (name like '_4%'
  43.             or name like 'ep%'
  44.             or name like 'f%'
  45.             or name like 'fp%'
  46.             or name like 'sp%'
  47.             or name like 't_4%'
  48.             or name like 'vw%')
  49.         order by 1
  50. --select top 100 * from sysobjects
  51. OPEN curStoredProcs
  52. FETCH NEXT FROM curStoredProcs INTO @vchrStoredProcName
  53. WHILE (@@fetch_status = 0) BEGIN
  54.     set @vchrLine = '#####################################################'
  55.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  56.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  57.     set @vchrLine = ''
  58.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  59.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  60.     set @vchrLine = @vchrStoredProcName
  61.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  62.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  63.     set @vchrLine = ''
  64.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  65.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  66.     set @vchrLine = '#########################################################'
  67.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  68.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  69.     
  70.     --initialize
  71.     select
  72.         @vchrLine = '',
  73.         @vchrPrevChar = ''
  74.     -- =============================================
  75.     -- loop through one stored proc
  76.     -- =============================================
  77.     DECLARE curComments  CURSOR LOCAL FOR
  78.         SELECT [text] FROM syscomments
  79.             WHERE id = OBJECT_ID(@vchrStoredProcName) and encrypted = 0
  80.             ORDER BY number, colid
  81.         FOR READ ONLY
  82.     
  83.     OPEN curComments
  84.     
  85.     FETCH NEXT FROM curComments into @vchrSysCommentText
  86.     
  87.     --loop through the lines in the syscomments table.
  88.     --there can be one or many for the stored proc,
  89.     --many stored proc lines can be on one syscomments line
  90.     WHILE @@fetch_status >= 0
  91.     BEGIN
  92.         --initialize
  93.         select
  94.             @intPos = 1
  95.     
  96.         WHILE @intPos  <> len(@vchrSysCommentText) BEGIN
  97.             select @vchrChar = substring(@vchrSysCommentText,@intPos,1)
  98.             select @intAscii = ascii(@vchrChar)
  99.             if not (@intAscii = 13 or @intAscii = 10)
  100.                 select @vchrLine = @vchrLine + @vchrChar
  101.     
  102.             --if we encounter a line feed...
  103.             if @intAscii in (10,13) and @intPrevAscii in (10,13) begin
  104.                 --output a line and clear the line buffer
  105.                -- Appends the string value line to the file specified by the
  106.                 --@vchrFile input parameter
  107.                 EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  108.             
  109.                -- Prints error if non 0 return code during
  110.                 --sp_OAMethod WriteLine execution
  111.                 IF @RC <> 0
  112.                         PRINT 'Error:  Writing string data to file'
  113.     
  114.                 select @vchrLine = ''
  115.             end
  116.     
  117.     
  118.             select @intPos = @intPos + 1,
  119.                 @vchrPrevChar = @vchrChar,
  120.                 @intPrevAscii = @intAscii
  121.     
  122.         END
  123.     
  124.         FETCH NEXT FROM curComments into @vchrSysCommentText
  125.     END
  126.     CLOSE curComments
  127.     DEALLOCATE curComments
  128.     set @vchrLine = ''
  129.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  130.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  131.     set @vchrLine = ''
  132.     EXEC @RC = sp_OAMethod @vchrFileID, 'WriteLine', Null , @vchrLine
  133.     IF @RC <> 0 PRINT 'Error:  Writing string data to file'
  134.     
  135.     FETCH NEXT FROM curStoredProcs INTO @vchrStoredProcName
  136. END
  137. CLOSE curStoredProcs
  138. DEALLOCATE curStoredProcs
  139. EXECUTE @RC = sp_OADestroy @vchrFileID
  140. EXECUTE @RC = sp_OADestroy @FS

Tags:
Categories:
Location: Blogs Parent Separator DEVSHED Blogs Child Separator SQL
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us