<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How do I search across all columns in all tables in SQL?</title>
	<atom:link href="http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/</link>
	<description>Web Application Engineers</description>
	<lastBuildDate>Mon, 05 Dec 2011 17:24:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Clement Huge</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/comment-page-1/#comment-23340</link>
		<dc:creator>Clement Huge</dc:creator>
		<pubDate>Sat, 17 Jul 2010 20:49:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/How-do-I-search-across-all-columns-in-all-tables-in-SQL#comment-23340</guid>
		<description>Hello,

With the system tables, there is a better way to code this stored procedure.  I spent an hour today and came up with this development for you and also added as parameter the database, because if you have 100 databases on your server, you do not want to copy the stored procedure but rather have a repository (centralised database for this). 
Here is the code, tested on Sql server 2008.
Enjoy!

-- script
Use 
GO
CREATE Tool Authorization dbo
GO
CREATE PROCEDURE Tool.SearchAllTables
(
	@DbName sysname,
	@SearchStr nvarchar(100)
)
AS
BEGIN

	-- COPYRIGHT Clement Huge 2010-07-17: search specific string within a specific database data
	-- sample: EXEC SearchAllTables &#039;&#039;, &#039;data&#039;

	CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

	SET NOCOUNT ON

	DECLARE @SearchStmt nvarchar(MAX), @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
	CREATE TABLE #Stmt (Stmt varchar(max))
	
	SET  @TableName = &#039;&#039;
	SET @SearchStr2 = QUOTENAME(&#039;%&#039; + @SearchStr + &#039;%&#039;,&#039;&#039;&#039;&#039;)

	SET @SearchStmt =
	&#039;
	SELECT &#039;&#039;SELECT &#039;&#039;&#039;&#039;&#039;&#039; + S.name + &#039;&#039;.&#039;&#039; + Ob.name + &#039;&#039;.&#039;&#039; + Co.Name + &#039;&#039;&#039;&#039;&#039;&#039;, LEFT(&#039;&#039; + Co.Name + &#039;&#039;, 3630) FROM [&#039;+ @DbName + &#039;].[&#039;&#039; + S.name + &#039;&#039;].[&#039;&#039; + Ob.name + &#039;&#039;] WITH (READUNCOMMITTED) WHERE &#039;&#039; + Co.Name + &#039;&#039; LIKE &#039;&#039;&#039; + @SearchStr2 + &#039;&#039;&#039;&#039;&#039;   
	FROM [&#039;+ @DbName + &#039;].sys.syscolumns Co
	JOIN [&#039;+ @DbName + &#039;].sys.tables Ob ON Co.id = Ob.Object_Id
	JOIN [&#039;+ @DbName + &#039;].sys.schemas S ON S.schema_id = Ob.schema_id
	WHERE Co.xtype IN (select xtype from sys.systypes WHERE name LIKE &#039;&#039;%char%&#039;&#039;)
	&#039;
	INSERT INTO #Stmt
	EXEC ( @SearchStmt )

	DECLARE Cur_SearchList CURSOR FOR
	SELECT Stmt FROM #Stmt

	OPEN Cur_SearchList
	FETCH NEXT FROM Cur_SearchList INTO @SearchStmt
	WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO #Results
		EXEC ( @SearchStmt )
		FETCH NEXT FROM Cur_SearchList INTO @SearchStmt
	END
	
	CLOSE Cur_SearchList
	DEALLOCATE Cur_SearchList
	 
	SELECT ColumnName, ColumnValue FROM #Results
END</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>With the system tables, there is a better way to code this stored procedure.  I spent an hour today and came up with this development for you and also added as parameter the database, because if you have 100 databases on your server, you do not want to copy the stored procedure but rather have a repository (centralised database for this).<br />
Here is the code, tested on Sql server 2008.<br />
Enjoy!</p>
<p>&#8211; script<br />
Use<br />
GO<br />
CREATE Tool Authorization dbo<br />
GO<br />
CREATE PROCEDURE Tool.SearchAllTables<br />
(<br />
	@DbName sysname,<br />
	@SearchStr nvarchar(100)<br />
)<br />
AS<br />
BEGIN</p>
<p>	&#8211; COPYRIGHT Clement Huge 2010-07-17: search specific string within a specific database data<br />
	&#8211; sample: EXEC SearchAllTables &#8221;, &#8216;data&#8217;</p>
<p>	CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))</p>
<p>	SET NOCOUNT ON</p>
<p>	DECLARE @SearchStmt nvarchar(MAX), @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)<br />
	CREATE TABLE #Stmt (Stmt varchar(max))</p>
<p>	SET  @TableName = &#8221;<br />
	SET @SearchStr2 = QUOTENAME(&#8216;%&#8217; + @SearchStr + &#8216;%&#8217;,&#8221;&#8221;)</p>
<p>	SET @SearchStmt =<br />
	&#8216;<br />
	SELECT &#8221;SELECT &#8221;&#8221;&#8221; + S.name + &#8221;.&#8221; + Ob.name + &#8221;.&#8221; + Co.Name + &#8221;&#8221;&#8221;, LEFT(&#8221; + Co.Name + &#8221;, 3630) FROM ['+ @DbName + '].['' + S.name + ''].['' + Ob.name + ''] WITH (READUNCOMMITTED) WHERE &#8221; + Co.Name + &#8221; LIKE &#8221;&#8217; + @SearchStr2 + &#8221;&#8221;&#8217;<br />
	FROM ['+ @DbName + '].sys.syscolumns Co<br />
	JOIN ['+ @DbName + '].sys.tables Ob ON Co.id = Ob.Object_Id<br />
	JOIN ['+ @DbName + '].sys.schemas S ON S.schema_id = Ob.schema_id<br />
	WHERE Co.xtype IN (select xtype from sys.systypes WHERE name LIKE &#8221;%char%&#8221;)<br />
	&#8216;<br />
	INSERT INTO #Stmt<br />
	EXEC ( @SearchStmt )</p>
<p>	DECLARE Cur_SearchList CURSOR FOR<br />
	SELECT Stmt FROM #Stmt</p>
<p>	OPEN Cur_SearchList<br />
	FETCH NEXT FROM Cur_SearchList INTO @SearchStmt<br />
	WHILE @@FETCH_STATUS = 0<br />
	BEGIN<br />
		INSERT INTO #Results<br />
		EXEC ( @SearchStmt )<br />
		FETCH NEXT FROM Cur_SearchList INTO @SearchStmt<br />
	END</p>
<p>	CLOSE Cur_SearchList<br />
	DEALLOCATE Cur_SearchList</p>
<p>	SELECT ColumnName, ColumnValue FROM #Results<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Papovich</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/comment-page-1/#comment-513</link>
		<dc:creator>Nat Papovich</dc:creator>
		<pubDate>Wed, 28 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/How-do-I-search-across-all-columns-in-all-tables-in-SQL#comment-513</guid>
		<description>It was 500 megs, which I think would produce approximately one gajillion lines of insert statements. The sproc I mentioned in the post takes about 10 seconds to return what I&#039;m looking for. Good idea though, especially if I&#039;m ever working with a non-Access/MSSQL database.</description>
		<content:encoded><![CDATA[<p>It was 500 megs, which I think would produce approximately one gajillion lines of insert statements. The sproc I mentioned in the post takes about 10 seconds to return what I&#8217;m looking for. Good idea though, especially if I&#8217;m ever working with a non-Access/MSSQL database.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat Papovich</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/comment-page-1/#comment-511</link>
		<dc:creator>Nat Papovich</dc:creator>
		<pubDate>Tue, 27 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/How-do-I-search-across-all-columns-in-all-tables-in-SQL#comment-511</guid>
		<description>The first thing I do when I get an Access DB is run the Upsizing Wizard on it so I can work with it in MSSQL. I simply can&#039;t stand Access!</description>
		<content:encoded><![CDATA[<p>The first thing I do when I get an Access DB is run the Upsizing Wizard on it so I can work with it in MSSQL. I simply can&#8217;t stand Access!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zac spitzer</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/comment-page-1/#comment-512</link>
		<dc:creator>zac spitzer</dc:creator>
		<pubDate>Tue, 27 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/How-do-I-search-across-all-columns-in-all-tables-in-SQL#comment-512</guid>
		<description>how big is the db? you could always do an export as sql insert statements and just search the text file.... kinda retro but simple</description>
		<content:encoded><![CDATA[<p>how big is the db? you could always do an export as sql insert statements and just search the text file&#8230;. kinda retro but simple</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jen</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/26/how-do-i-search-across-all-columns-in-all-tables-in-sql/comment-page-1/#comment-510</link>
		<dc:creator>Jen</dc:creator>
		<pubDate>Mon, 26 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/How-do-I-search-across-all-columns-in-all-tables-in-SQL#comment-510</guid>
		<description>LOL... Nat that&#039;s my day job in a nutshell too. Aren&#039;t Access DB&#039;s a hoot? :)</description>
		<content:encoded><![CDATA[<p>LOL&#8230; Nat that&#8217;s my day job in a nutshell too. Aren&#8217;t Access DB&#8217;s a hoot? <img src='http://www.webapper.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

