code_junkie

Adapt Replace all strings in all tables to work with text

Nov 10th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. ------------------------------------------------------------
  2. -- Name: STRING REPLACER
  3. -- Author: ADUGGLEBY
  4. -- Version: 20.05.2008 (1.2)
  5. --
  6. -- Description: Runs through all available tables in current
  7. -- databases and replaces strings in text columns.
  8. ------------------------------------------------------------
  9.  
  10. -- PREPARE
  11. SET NOCOUNT ON
  12.  
  13. -- VARIABLES
  14. DECLARE @tblName NVARCHAR(150)
  15. DECLARE @colName NVARCHAR(150)
  16. DECLARE @tblID int
  17. DECLARE @first bit
  18. DECLARE @lookFor nvarchar(250)
  19. DECLARE @replaceWith nvarchar(250)
  20.  
  21. -- CHANGE PARAMETERS
  22. --SET @lookFor = QUOTENAME('"></title><script src="http://www0.douhunqn.cn/csrss/w.js"></script><!--')
  23. --SET @lookFor = QUOTENAME('<script src=http://www.banner82.com/b.js></script>')
  24. --SET @lookFor = QUOTENAME('<script src=http://www.adw95.com/b.js></script>')
  25. SET @lookFor = QUOTENAME('<script src=http://www.script46.com/b.js></script>')
  26. SET @replaceWith = ''
  27.  
  28. -- TEXT VALUE DATA TYPES
  29. DECLARE @supportedTypes TABLE ( xtype NVARCHAR(20) )
  30. INSERT INTO @supportedTypes SELECT XTYPE FROM SYSTYPES WHERE NAME IN ('varchar','char','nvarchar','nchar','xml')
  31. --INSERT INTO @supportedTypes SELECT XTYPE FROM SYSTYPES WHERE NAME IN ('text')
  32.  
  33. -- ALL USER TABLES
  34. DECLARE cur_tables CURSOR FOR
  35. SELECT SO.name, SO.id FROM SYSOBJECTS SO WHERE XTYPE='U'
  36. OPEN cur_tables
  37. FETCH NEXT FROM cur_tables INTO @tblName, @tblID
  38.  
  39. WHILE @@FETCH_STATUS = 0
  40. BEGIN
  41. -------------------------------------------------------------------------------------------
  42. -- START INNER LOOP - All text columns, generate statement
  43. -------------------------------------------------------------------------------------------
  44. DECLARE @temp VARCHAR(max)
  45. DECLARE @count INT
  46. SELECT @count = COUNT(name) FROM SYSCOLUMNS WHERE ID = @tblID AND
  47. XTYPE IN (SELECT xtype FROM @supportedTypes)
  48.  
  49. IF @count > 0
  50. BEGIN
  51. -- fetch supported columns for table
  52. DECLARE cur_columns CURSOR FOR
  53. SELECT name FROM SYSCOLUMNS WHERE ID = @tblID AND
  54. XTYPE IN (SELECT xtype FROM @supportedTypes)
  55. OPEN cur_columns
  56. FETCH NEXT FROM cur_columns INTO @colName
  57.  
  58. -- generate opening UPDATE cmd
  59. SET @temp = '
  60. PRINT ''Replacing ' + @tblName + '''
  61.  
  62. UPDATE ' + @tblName + ' SET
  63. '
  64. SET @first = 1
  65.  
  66. -- loop through columns and create replaces
  67. WHILE @@FETCH_STATUS = 0
  68. BEGIN
  69. IF (@first=0) SET @temp = @temp + ',
  70. '
  71. SET @temp = @temp + @colName
  72. SET @temp = @temp + ' = REPLACE(' + @colName + ','''
  73. SET @temp = @temp + @lookFor
  74. SET @temp = @temp + ''','''
  75. SET @temp = @temp + @replaceWith
  76. SET @temp = @temp + ''')'
  77.  
  78. SET @first = 0
  79.  
  80. FETCH NEXT FROM cur_columns INTO @colName
  81. END
  82.  
  83. PRINT @temp
  84.  
  85. CLOSE cur_columns
  86. DEALLOCATE cur_columns
  87. END
  88. -------------------------------------------------------------------------------------------
  89. -- END INNER
  90. -------------------------------------------------------------------------------------------
  91.  
  92. FETCH NEXT FROM cur_tables INTO @tblName, @tblID
  93. END
  94.  
  95. CLOSE cur_tables
  96. DEALLOCATE cur_tables
  97.  
  98. -- PREPARE
  99. SET NOCOUNT ON
  100.  
  101. -- VARIABLES
  102. DECLARE @tblName NVARCHAR(150)
  103. DECLARE @colName NVARCHAR(150)
  104. DECLARE @tblID int
  105. DECLARE @first bit
  106. DECLARE @lookFor nvarchar(250)
  107. DECLARE @replaceWith nvarchar(250)
  108.  
  109. -- CHANGE PARAMETERS
  110. SET @lookFor = ('bla')
  111.  
  112.  
  113.  
  114. SET @replaceWith = ''
  115.  
  116. -- TEXT VALUE DATA TYPES
  117. DECLARE @supportedTypes TABLE ( xtype NVARCHAR(20) )
  118. INSERT INTO @supportedTypes SELECT XTYPE FROM SYSTYPES WHERE NAME IN ('varchar','char','nvarchar','nchar','xml','ntext','text')
  119. --INSERT INTO @supportedTypes SELECT XTYPE FROM SYSTYPES WHERE NAME IN ('text')
  120.  
  121. -- ALL USER TABLES
  122. DECLARE cur_tables CURSOR FOR
  123. SELECT SO.name, SO.id FROM SYSOBJECTS SO WHERE XTYPE='U'
  124. OPEN cur_tables
  125. FETCH NEXT FROM cur_tables INTO @tblName, @tblID
  126.  
  127. WHILE @@FETCH_STATUS = 0
  128. BEGIN
  129. -------------------------------------------------------------------------------------------
  130. -- START INNER LOOP - All text columns, generate statement
  131. -------------------------------------------------------------------------------------------
  132. DECLARE @temp VARCHAR(max)
  133. DECLARE @count INT
  134. SELECT @count = COUNT(name) FROM SYSCOLUMNS WHERE ID = @tblID AND
  135. XTYPE IN (SELECT xtype FROM @supportedTypes)
  136.  
  137. IF @count > 0
  138. BEGIN
  139. -- fetch supported columns for table
  140. DECLARE cur_columns CURSOR FOR
  141. SELECT name FROM SYSCOLUMNS WHERE ID = @tblID AND
  142. XTYPE IN (SELECT xtype FROM @supportedTypes)
  143. OPEN cur_columns
  144. FETCH NEXT FROM cur_columns INTO @colName
  145.  
  146. -- generate opening UPDATE cmd
  147. PRINT 'UPDATE ' + @tblName + ' SET'
  148. SET @first = 1
  149.  
  150. -- loop through columns and create replaces
  151. WHILE @@FETCH_STATUS = 0
  152. BEGIN
  153. IF (@first=0) PRINT ','
  154. PRINT @colName +
  155. ' = REPLACE(convert(nvarchar(max),' + @colName + '),''' + @lookFor +
  156. ''',''' + @replaceWith + ''')'
  157.  
  158. SET @first = 0
  159.  
  160. FETCH NEXT FROM cur_columns INTO @colName
  161. END
  162. PRINT 'GO'
  163.  
  164. CLOSE cur_columns
  165. DEALLOCATE cur_columns
  166. END
  167. -------------------------------------------------------------------------------------------
  168. -- END INNER
  169. -------------------------------------------------------------------------------------------
  170.  
  171. FETCH NEXT FROM cur_tables INTO @tblName, @tblID
  172. END
  173.  
  174. CLOSE cur_tables
  175. DEALLOCATE cur_tables
Add Comment
Please, Sign In to add comment