Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entire table search 1

Status
Not open for further replies.

jvande

MIS
Jun 6, 2001
115
US
What is the SQL to search an entire table for a specific string and return those records that have that string in it. For example I want to search the table "test" for "~". Here is the psuedo code.

select *
from test
where any character = ~

Thanks,
Josh

 
I apoligize. I want a query that searches all 500 of my fields for a "~" and returns the records that have any fields that contain a "~" in it.
 
Hi Josh,
Try to do like this:
create procedure search
(@table varchar(50), @toSearch varchar(10)) as
declare @strsql varchar(2000)
select @strsql="select * from "+@table+" where 1=2 "
select @strsql=@strsql+" or "+name+
" like '%"+@toSearch+"%' " from syscolumns
where object_name(id)=@table
exec(@strsql)


execute this procedure as :
execute search 'myTable','mySeachExpression'
It will return you all the records depending on your criteria.

Let me know if it works.
 
Hi josh,
Sorry but just add this condition also after object_name condition.
and type in (47,39)
This will make sure that only char and varchar type columns will be searched.
 
That worked excellent. Thank you very much rajeevnandanmishra



 
Sorry for misreading Josh.

rajeevnandanmishra, that was a very insightful query, thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top