{"id":428,"date":"2019-05-31T08:55:13","date_gmt":"2019-05-31T12:55:13","guid":{"rendered":"http:\/\/techref.camellarry.com\/?p=428"},"modified":"2019-05-31T08:55:13","modified_gmt":"2019-05-31T12:55:13","slug":"sql-search-all-tables-for-text","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=428","title":{"rendered":"SQL Search all tables for text"},"content":{"rendered":"<p>I was looking for a string inside of a table in a massive database.\u00a0 I was attempting to do this by trial-and-error until I ran across this method which does the searching for me.\u00a0 This script searches all the text\/string type fields in every table in the selected database.\u00a0 Simply specify the &#8220;StringToFind&#8221;.<\/p>\n<blockquote><p>Declare @StringToFind VARCHAR(200), @Schema sysname, @Table sysname, @FullTable int, @NewMinID int, @NewMaxID int,<br \/>\n@SQLCommand VARCHAR(8000), @BaseSQLCommand varchar(8000), @Where VARCHAR(8000), @CountCheck varchar(8000) , @FieldTypes varchar(8000),<br \/>\n@cursor VARCHAR(8000), @columnName sysname, @SCn int, @SCm int<br \/>\nDeclare @TableList table (Id int identity(1,1) not null, tablename varchar(250))<br \/>\nDeclare @SQLCmds table (id int identity(1,1) not null, sqlcmd varchar(8000))<br \/>\nDeclare @DataFoundInTables table (id int identity(1,1) not null, sqlcmd varchar(8000))<\/p>\n<p>&#8212; Settings<\/p>\n<p>SET @StringToFind = &#8216;.NET Framework 4.7.2&#8217;<br \/>\nSET NOCOUNT ON<br \/>\nSET @StringToFind = &#8216;%&#8217;+@StringToFind+&#8217;%&#8217;<\/p>\n<p>&#8212; Gathering Info<\/p>\n<p>if ((select count(*) from sysobjects where name = &#8216;tempcount&#8217;) &gt; 0)<br \/>\ndrop table tempcount<\/p>\n<p>create table tempcount (rowsfound int)<br \/>\ninsert into tempcount select 0<\/p>\n<p>&#8212; This section here is to accomodate the user defined datatypes, if they have<br \/>\n&#8212; a SQL Collation then they are assumed to have text in them.<br \/>\nSET @FieldTypes = &#8221;<br \/>\nselect @FieldTypes = @FieldTypes + &#8221;&#8221; + rtrim(ltrim(name))+&#8221;&#8217;,&#8217; from systypes where collation is not null or xtype = 36<br \/>\nselect @FieldTypes = left(@FieldTypes,(len(@FieldTypes)-1))<\/p>\n<p>insert into @TableList (tablename)<br \/>\nselect name from sysobjects<br \/>\nwhere xtype = &#8216;U&#8217; and name not like &#8216;dtproperties&#8217;<br \/>\norder by name<\/p>\n<p>&#8212; Start Processing Table List<\/p>\n<p>select @NewMinID = min(id), @NewMaxID = max(id) from @TableList<\/p>\n<p>while(@NewMinID &lt;= @NewMaxID)<br \/>\nBegin<\/p>\n<p>SELECT @Table = tablename, @Schema=&#8217;dbo&#8217;, @Where = &#8221; from @TableList where id = @NewMinID<\/p>\n<p>SET @SQLCommand = &#8216;SELECT * FROM &#8216; + @Table + &#8216; WHERE&#8217;<br \/>\n&#8212; removed &#8216; + @Schema + &#8216;.<\/p>\n<p>SET @cursor = &#8216;DECLARE col_cursor CURSOR FOR SELECT COLUMN_NAME<br \/>\nFROM [&#8216; + DB_NAME() + &#8216;].INFORMATION_SCHEMA.COLUMNS<br \/>\nWHERE TABLE_SCHEMA = &#8221;&#8217; + @Schema + &#8221;&#8217;<br \/>\nAND TABLE_NAME = &#8221;&#8217; + @Table + &#8221;&#8217;<br \/>\nAND DATA_TYPE IN (&#8216;+@FieldTypes+&#8217;)&#8217;<br \/>\n&#8211;Original Check, however the above implements user defined data types &#8211;AND DATA_TYPE IN (&#8221;char&#8221;,&#8221;nchar&#8221;,&#8221;ntext&#8221;,&#8221;nvarchar&#8221;,&#8221;text&#8221;,&#8221;varchar&#8221;)&#8217;<\/p>\n<p>EXEC (@cursor)<\/p>\n<p>SET @FullTable = 0<br \/>\nDELETE FROM @SQLCmds<\/p>\n<p>OPEN col_cursor<br \/>\nFETCH NEXT FROM col_cursor INTO @columnName<\/p>\n<p>WHILE @@FETCH_STATUS = 0<br \/>\nBEGIN<\/p>\n<p>SET @Where = @Where + &#8216; [&#8216; + @columnName + &#8216;] LIKE &#8221;&#8217; + @StringToFind + &#8221;&#8221;<br \/>\nSET @Where = @Where + &#8216; OR&#8217;<\/p>\n<p>&#8211;PRINT @Table + &#8216;|&#8217;+ cast(len(isnull(@Where,&#8221;))+len(isnull(@SQLCommand,&#8221;)) as varchar(10))+&#8217;|&#8217;+@Where<\/p>\n<p>if (len(isnull(@Where,&#8221;))+len(isnull(@SQLCommand,&#8221;)) &gt; 3600)<br \/>\nBegin<br \/>\nSELECT @Where = substring(@Where,1,len(@Where)-3)<br \/>\ninsert into @SQLCmds (sqlcmd) select @Where<br \/>\nSET @Where = &#8221;<br \/>\nEnd<\/p>\n<p>FETCH NEXT FROM col_cursor INTO @columnName<br \/>\nEND<\/p>\n<p>CLOSE col_cursor<br \/>\nDEALLOCATE col_cursor<\/p>\n<p>if (@Where &lt;&gt; &#8221;)<br \/>\nBegin<br \/>\nSELECT @Where = substring(@Where,1,len(@Where)-3)<br \/>\ninsert into @SQLCmds (sqlcmd)<br \/>\nselect @Where &#8211;select @Table,count(*) from @SQLCmds<br \/>\nEnd<\/p>\n<p>SET @BaseSQLCommand = @SQLCommand<\/p>\n<p>select @SCn = min(id), @SCm = max(id) from @SQLCmds<br \/>\nwhile(@SCn &lt;= @SCm)<br \/>\nBegin<\/p>\n<p>select @Where = sqlcmd from @SQLCmds where ID = @SCn<\/p>\n<p>if (@Where &lt;&gt; &#8221;)<br \/>\nBegin<\/p>\n<p>SET @SQLCommand = @BaseSQLCommand + @Where<br \/>\nSELECT @CountCheck = &#8216;update tempcount set rowsfound = (select count(*) &#8216;+ substring(@SQLCommand,10,len(@SQLCommand)) + &#8216;)&#8217;<br \/>\nEXEC (@CountCheck)<\/p>\n<p>if ((select rowsfound from tempcount) &gt; 0)<br \/>\nBegin<br \/>\nPRINT &#8216;&#8212; [&#8216;+cast(@NewMinID as varchar(15))+&#8217;\/&#8217;+cast(@NewMaxID as varchar(15))+&#8217;] &#8216;+@Table + &#8216; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-[FOUND!]&#8217;<br \/>\n&#8211;PRINT &#8216;&#8212; [FOUND USING:] &#8216; +@SQLCommand<br \/>\ninsert into @DataFoundInTables (sqlcmd) select @SQLCommand<br \/>\nEXEC (@SQLCommand)<br \/>\nupdate tempcount set rowsfound = 0<br \/>\nEnd<br \/>\nelse<br \/>\nBegin<br \/>\nPRINT &#8216;&#8212; [&#8216;+cast(@NewMinID as varchar(15))+&#8217;\/&#8217;+cast(@NewMaxID as varchar(15))+&#8217;] &#8216;+@Table<br \/>\nEnd<br \/>\nEnd<\/p>\n<p>SET @SCn = @SCn + 1<br \/>\nEnd<\/p>\n<p>set @NewMinID = @NewMinID + 1<br \/>\nend<\/p>\n<p>if ((select count(*) from sysobjects where name = &#8216;tempcount&#8217;) &gt; 0)<br \/>\ndrop table tempcount<\/p>\n<p>\/*<\/p>\n<p>This will now return all the sql commands you need to use<\/p>\n<p>*\/<\/p>\n<p>select @NewMinID = min(id), @NewMaxID = max(id) from @DataFoundInTables<\/p>\n<p>if (@NewMaxID &gt; 0)<br \/>\nBegin<br \/>\nPRINT &#8216; &#8216;<br \/>\nPRINT &#8216; &#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8211; TABLES WITH DATA &#8212;&#8212;&#8212;&#8212;&#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8216;<br \/>\nPRINT &#8216; &#8216;<br \/>\nPRINT &#8216;We found &#8216; + cast(@NewMaxID as varchar(10)) + &#8216; table(s) with the string &#8216;+@StringToFind<br \/>\nPRINT &#8216; &#8216;<\/p>\n<p>while(@NewMinID &lt;= @NewMaxID)<br \/>\nBegin<\/p>\n<p>select @SQLCommand = sqlcmd from @DataFoundInTables where ID = @NewMinID<\/p>\n<p>PRINT @SQLCommand<\/p>\n<p>SET @NewMinID = @NewMinID + 1<br \/>\nEnd<\/p>\n<p>PRINT &#8216; &#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#8216;<\/p>\n<p>End<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>I was looking for a string inside of a table in a massive database.\u00a0 I was attempting to do this by trial-and-error until I ran across this method which does the searching for me.\u00a0 This script searches all the text\/string type fields in every table in the selected database.\u00a0 Simply specify the &#8220;StringToFind&#8221;. Declare @StringToFind [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8],"tags":[45,70],"class_list":["post-428","post","type-post","status-publish","format-standard","hentry","category-sql","tag-microsoft","tag-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-6U","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=428"}],"version-history":[{"count":0,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}