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 2008 Forums
 Transact-SQL (2008)
 search database

Author  Topic 

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-15 : 09:57:24
how do i search the database for a specific value in a field
dont know what table it would be stored in, or the field.

thanks

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-04-15 : 10:09:05
Do you want to search database name
then
use master
select * from sys.databases

Raghu' S
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-15 : 10:25:59
no i want to search the entire database for a specific value in a field, such as a date, dont know the table or field it is stored in.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-15 : 10:36:45
While I think this request is somewhat odd... silly even, it was kinda fun to mock up a sample:

WARNING: this could be dangerous to run if you are working with a large Db. (large in table counts, column counts, or row counts)

P.S. - this only provides the tables and columns that provide matches. More work will need to be done from there if more than 1 result returns.


Declare @findData varchar(100),
@isDate bit,
@isNumeric bit,
@isInt bit,
@isBit bit,
@isDecimal bit

Set @findData = 'Search for this string!'
--Set @findData = '4561347' -- or this number
--Set @findData = '4/23/1980' -- or this Date


Set @isDate = isdate(@findData)
Set @isNumeric = isNumeric(@findData)
if (@isNumeric=1)
Begin
Set @isInt = case when convert(decimal(18,9),@findData) - convert(int,convert(decimal(18,9),@findData)) = 0 then 1 else 0 end
Set @IsDecimal = 1
End
Else
Begin
Set @isInt = 0
Set @IsDecimal = 0
End

Set @isBit = case when @findData in ('0','1','true','false') then 1 else 0 end


Declare @t table (
sqlStr varchar(500)
)

Insert Into @t
Select
'Select tableName=''' + A.Name + ''', ColumnName=''' + B.Name + ''', Value=convert(varchar,'+ B.Name + '), Cnt=Count(*) From ' + A.Name + ' Where ' + B.Name + ' = convert(' + C.Name + ',''' + @findData + ''') Group By convert(varchar,'+ B.Name + ')'
--,A.Name, C.Name, B.*
From sysobjects A
Inner Join syscolumns B
On A.id = B.id
Inner Join systypes C
On B.xType = C.xType
Where A.type = 'U'
and (
(C.Name in ('int') and @isInt = 1)
or
(C.Name in ('decimal','float','money') and @isDecimal = 1)
or
(C.Name in ('datetime','smalldatetime') and @isDate = 1)
or
(C.Name in ('bit') and @isBit = 1)
or
C.Name in ('char','varchar','nvarchar','nchar')
--or
--C.Name not in ('int','datetime','bit','char','varchar','money','nchar','nvarchar','image','text','sysname','binary','varbinary','xml','smalldatetime','decimal')
)


--Select * From @t

Declare @results table (
TableName varchar(100),
ColumnName varchar(100),
Value varchar(100),
Cnt int
)

Declare @sqlStr varchar(500)

While exists(Select * From @t)
Begin
Set @sqlStr = (Select top 1 sqlStr From @t)
Delete From @t Where sqlStr = @sqlStr

Insert Into @results
Exec(@sqlStr)
End

Select * From @results


Corey

I Has Returned!!
Go to Top of Page

NeilG
Aged Yak Warrior

530 Posts

Posted - 2011-04-15 : 10:45:02
Why? surely it can't be that hard to figure out what table is likely to hold that value
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-15 : 10:50:55
but she 'wants to search the entire database'

Corey

I Has Returned!!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-15 : 11:02:03
I come across this on a smaller scaler all the time, when you get the "can't convert ` to a numeric" type answer.
I use select * from INFORMATION_SCHEMA.COLUMNS WHERE< whatever>
to get all teh table names and column names and the cut and past that into excel and use excel to write all the
select * from <table> where col1 like '%`' or col2 like '%`%' etc. It's actually faster this way than writing and executing dynamic sql.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

sharona
Yak Posting Veteran

75 Posts

Posted - 2011-04-15 : 11:02:09
we are looking for a specific date that isnt labeled for easy to find in the db. that is why im not sure of the table, the tables it should be in, it isnt.

thanks
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2011-04-15 : 11:09:36
What part is faster? Writing the queries? or executing? I'm not really sure what you meant.

As for my sample ^... it only took 15 minutes or so to slap it together and it's more or less reusable. For the current db I'm working in, it only takes a couple seconds to return the results.

on the other hand... I've written (generated, really) plenty of queries in Excel. I also have enjoyed UltraEdit (text editor with column editting support)

quote:
Originally posted by jimf

I come across this on a smaller scaler all the time, when you get the "can't convert ` to a numeric" type answer.
I use select * from INFORMATION_SCHEMA.COLUMNS WHERE< whatever>
to get all teh table names and column names and the cut and past that into excel and use excel to write all the
select * from <table> where col1 like '%`' or col2 like '%`%' etc. It's actually faster this way than writing and executing dynamic sql.

Jim

Everyday I learn something that somebody else already knew



Corey

I Has Returned!!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-04-15 : 11:15:59
Running the queries. I'm probably more familiar with using Excel to write the SQL Code than I should be, it's just been my experince that the whole process runs faster than doing it dynamically. Plus you get to stop as soon as you find the needle in the haystack.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -