Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 searching throug all the DB for a certain string

Author  Topic 

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2006-08-23 : 11:32:28
how can i search all tha tables and columns iof the DB to find a certain string?
thnaks in advance
peleg

Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:)

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-08-23 : 11:43:34
use information_schema.columns to search for string in column names and information_schema.tables to search for string in table names :)

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

shebert
Yak Posting Veteran

85 Posts

Posted - 2006-08-23 : 14:08:21
declare @Tablelist table (tableid int identity, table_name varchar(255))
insert into @tablelist
SELECT table_name FROM INFORMATION_SCHEMA.COLUMNS where column_name like '%userID%'
select * from @TableList

this code finds any column names with Userid
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-08-23 : 15:00:14
Try this script. Change the parameter settings to modify the functionality:
set nocount on

--Script parameters
declare @Value as VARCHAR(50)
declare @SearchStrings char(1)
declare @SearchNumbers char(1)
declare @SearchDates char(1)

----------------------------------------------------------------------------------------------------
--Script: ValueSearcher
--blindman, 9/19/2005
--Searches columns in user tables for a specified value.
--Returns the location where the value is found, and the number of records containing that value.
--Enter the value to be found in the Script parameter settings section below.
--Column types and comparison methods can be defined using the @Search parameters.
----------------------------------------------------------------------------------------------------
--Script parameter settings
set @Value = 'JEFF' --Enter the value to search for as a quoted string.
set @SearchStrings = 'L' --E=Exact string search, L=Search using Like operator, N=Do not search.
set @SearchNumbers = 'N' --Y=Search for numbers, N=Do not search.
set @SearchDates = 'N' --E=Exact datetime search, D=Search whole date parts only, N=Do not search.
----------------------------------------------------------------------------------------------------

--Processing variables
create table #Results (TableName sysname, ColumnName sysname, RecordCount bigint)
declare @SQLString varchar(4000)

--check validity of parameters
if IsNumeric(@Value) = 0 set @SearchNumbers = 'N'
if IsDate(@Value) = 0 set @SearchDates = 'N'

--Create SQL statements to search the database
declare SQLCursor cursor for
--exact string columns
select 'insert into #Results (TableName, ColumnName, RecordCount) select ''' + sysobjects.name + ''', ''' + syscolumns.name + ''', count(*) from ' + sysobjects.name + ' where ' + syscolumns.name + ' = ''' + @Value + ''' having count(*) > 0'
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
inner join systypes on syscolumns.xtype = systypes.xtype
where sysobjects.type = 'U'
and systypes.name in ('char', 'nchar', 'nvarchar', 'sysname', 'uniqueidentifer', 'varchar')
and @SearchStrings = 'E'
UNION
--like string columns
select 'insert into #Results (TableName, ColumnName, RecordCount) select ''' + sysobjects.name + ''', ''' + syscolumns.name + ''', count(*) from ' + sysobjects.name + ' where ' + syscolumns.name + ' like ''%' + @Value + '%'' having count(*) > 0'
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
inner join systypes on syscolumns.xtype = systypes.xtype
where sysobjects.type = 'U'
and systypes.name in ('char', 'nchar', 'nvarchar', 'sysname', 'uniqueidentifer', 'varchar')
and @SearchStrings = 'L'
UNION
--numeric columns
select 'insert into #Results (TableName, ColumnName, RecordCount) select ''' + sysobjects.name + ''', ''' + syscolumns.name + ''', count(*) from ' + sysobjects.name + ' where ' + syscolumns.name + ' = ' + @Value + ' having count(*) > 0'
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
inner join systypes on syscolumns.xtype = systypes.xtype
where sysobjects.type = 'U'
and systypes.name in ('bigint', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smallint', 'smallmoney', 'tinyint')
and @SearchNumbers = 'Y'
UNION
--Exact datetime columns
select 'insert into #Results (TableName, ColumnName, RecordCount) select ''' + sysobjects.name + ''', ''' + syscolumns.name + ''', count(*) from ' + sysobjects.name + ' where ' + syscolumns.name + ' = ''' + @Value + ''' having count(*) > 0'
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
inner join systypes on syscolumns.xtype = systypes.xtype
where sysobjects.type = 'U'
and systypes.name in ('datetime', 'smalldatetime')
and @SearchDates = 'E'
UNION
--dateonly datetime columns
select 'insert into #Results (TableName, ColumnName, RecordCount) select ''' + sysobjects.name + ''', ''' + syscolumns.name + ''', count(*) from ' + sysobjects.name + ' where convert(char(10), ' + syscolumns.name + ', 120) = convert(char(10), convert(datetime, ''' + @Value + '''), 120) having count(*) > 0'
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
inner join systypes on syscolumns.xtype = systypes.xtype
where sysobjects.type = 'U'
and systypes.name in ('datetime', 'smalldatetime')
and @SearchDates = 'D'

--Run the SQL Statements
Open SQLCursor
Fetch next from SQLCursor into @SQLString
while @@FETCH_STATUS = 0
begin
exec (@SQLString)
fetch next from SQLCursor into @SQLString
end
Close SQLCursor
Deallocate SQLCursor

--Display the results
select cast(TableName + '.' + ColumnName as char(60)) Location,
RecordCount
from #Results
order by TableName,
ColumnName

--Clean up
drop table #Results
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-26 : 05:04:28

http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -