Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-30
5
Medium Priority
?
834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
Chris Mangus earned 750 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 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 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