Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

14 Experts available now in Live!

Get 1:1 Help Now