Solved

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

Posted on 2007-11-30
5
824 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

773 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