Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

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

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
Pizpot
Asked:
Pizpot
  • 3
2 Solutions
 
Chris MangusDatabase AdministratorCommented:
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
 
PizpotAuthor Commented:
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
 
pablo_munozCommented:
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
 
Chris MangusDatabase AdministratorCommented:
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
 
Chris MangusDatabase AdministratorCommented:
I think the poster is looking for actual data, not column or table names.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now