Solved

How find value in any column in any table with SYSCOLUMNS?

Posted on 2007-11-30
5
821 Views
Last Modified: 2008-02-01
USE CompanyA SELECT * FROM EccUser WHERE Administrator='Y'

The above lets me search the EccUser table in the CompanyA database for rows where the Administrator column is = 'Y'

How do I look in any column in any table for 'Y'? Do I use SYSCOLUMNS? How? (I am on ms sql 2000 if I need to use ps_MSforEachTable or ...EachDB by the way.)
0
Comment
Question by:Pizpot
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 250 total points
ID: 20385473
I use the attached code to search for a value in every column of every table.
Declare @TN as varchar(200), @CN as varchar(200), @myValue varchar(30), @SQL as nvarchar(1000)

, @SN as varchar(200), @Exact_Match bit
 

Create Table #myTable (Table_Name varchar(200), Column_Name varchar(200), Number_Of_Rows int)
 

-- Replace @myValue with the value you're searching for in the database

Set @myValue = 'searchvalue' 

-- 0 for LIKE match, 1 for exact match

Set @Exact_Match = 0    
 

Declare myCursor Cursor For

Select T.Table_Name, C.Column_Name, T.Table_Schema

From INFORMATION_SCHEMA.TABLES T Inner Join INFORMATION_SCHEMA.COLUMNS C 

On T.Table_Schema = C.Table_Schema And T.Table_Name = C.Table_Name

Where T.Table_Name <> 'dtproperties' And Table_Type = 'Base Table'

And C.Data_Type In ('varchar','char','nvarchar','nchar','sql_variant')

--And C.Data_Type In ('text','ntext')

--And C.Data_Type In ('tinyint','int','bigint','numeric','decimal','money','float','smallint','real','smallmoney')

--And C.Data_Type In ('datetime','dmalldatetime')

-- Fields not searched: image, uniqueidentifier, bit, varbinary, binary, timestamp

Open myCursor

Fetch Next From myCursor Into @TN, @CN, @SN

While @@Fetch_Status <> -1

Begin

	If @Exact_Match = 0

		Set @SQL = N'Insert Into #myTable Select ''' + @SN + '.' + @TN + ''', ''' + @CN + ''', Count(*) From [' + @SN + '].[' + @TN + '] Where [' + @CN + '] Like ''%' + @myValue + '%'''

	    Else

		Set @SQL = N'Insert Into #myTable Select ''' + @SN + '.' + @TN + ''', ''' + @CN + ''', Count(*) From [' + @SN + '].[' + @TN + '] Where [' + @CN + '] = ''' + @myValue + ''''

	--Print @SQL

	Exec sp_executesql @SQL 

	Fetch Next From myCursor Into @TN, @CN, @SN

End

Close myCursor

Deallocate myCursor

Select * From #myTable Where Number_Of_Rows > 0 Order By Table_Name

Drop Table #myTable

Open in new window

0
 

Author Comment

by:Pizpot
ID: 20385533
Nice... I see that I just edit

Set @myValue = 'searchvalue'

to the value that I am looking for. This won't run with only READ access to the db server right? (because it is creating a table) That is why I get nothing back right?
0
 
LVL 3

Assisted Solution

by:pablo_munoz
pablo_munoz earned 250 total points
ID: 20385546
I'm not sure if the above code worked for you, but I use this stored proc to look for any value in any table, you can pass it the table name and value you are looking for, or just the value.

regards.
set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[prFindTableOrColumn]

(

	@table_name VARCHAR(128) = NULL,

	@search_data VARCHAR(128) = NULL

)

AS

	

	DECLARE @search1 VARCHAR(128)

	SET @search1 = '%' + @table_name + '%'

	IF @search1 IS NULL

		SET @search1 = '%'

	

	DECLARE @search2 VARCHAR(128)

	SET @search2 = '%' + @search_data + '%'

	IF @search2 IS NULL

		SET @search2 = '%'

	

	SELECT

		c.table_name,

		c.column_name,

		c.data_type,

		c.character_maximum_length,

		c.numeric_precision,

		c.numeric_scale

	FROM	

		information_schema.columns c

		INNER JOIN information_schema.tables t 

			ON c.table_name = t.table_name 

	WHERE

		c.table_name LIKE @search1

		AND c.column_name LIKE @search2

		AND t.table_type = 'BASE TABLE'

	ORDER BY 

		c.table_name,

		c.column_name

Open in new window

0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20385554
Likely yes, because it will need to create a temporary table to hold the data.  

And yes, you just edit @myValue.  You also have the option of 'exact' or 'like' matches.  Plus, you can uncomment some of the lines to search through numeric or text values.
0
 
LVL 17

Expert Comment

by:Chris Mangus
ID: 20385596
I think the poster is looking for actual data, not column or table names.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now