SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find a text string in any column in a database.


Find a text string in any column in a database.

Author
Message
Kenneth Fisher
Kenneth Fisher
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6511 Visits: 2064
Comments posted to this topic are about the item Find a text string in any column in a database.

Kenneth FisherI strive to live in a world where a chicken can cross the road without being questioned about its motives.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
toniupstny
toniupstny
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1032 Visits: 940
Nice script.

A couple of questions for you:
- Shouldn't there be a set of [ ] around the variables in case they contain special characters like blanks or SQL keywords?
- How can you handle special characters in the LIKE comparison if a string you would search for has a %, _, [ ], [^ ] or quotes?

Toni
Tim Wehr-392184
Tim Wehr-392184
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 40
toniupstny (2/8/2008)
Nice script.

A couple of questions for you:
- Shouldn't there be a set of [ ] around the variables in case they contain special characters like blanks or SQL keywords?
- How can you handle special characters in the LIKE comparison if a string you would search for has a %, _, [ ], [^ ] or quotes?


I agree. It failed immediately for me for a column named "Group".

SET @Sql = 'SELECT @Row_Count = COUNT(*) '+
' FROM ' + @Table_Name + ' (NOLOCK) WHERE [' + @Column_Name + '] LIKE ''%' + @Search_For + '%'''

Adding the brackets prevents the ambiguity of the word.

Given that, I see this as a useful time-saver when a word or phrase may have been used in numerous places.

Thanks!
Kenneth Fisher
Kenneth Fisher
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6511 Visits: 2064
Fair point. I'll have to make the change. The times that I used it I wasn't looking for anything with special characters or keywords.

Kenneth FisherI strive to live in a world where a chicken can cross the road without being questioned about its motives.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
Kenneth Fisher
Kenneth Fisher
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6511 Visits: 2064
toniupstny (2/8/2008)
Nice script.

A couple of questions for you:
- Shouldn't there be a set of [ ] around the variables in case they contain special characters like blanks or SQL keywords?
- How can you handle special characters in the LIKE comparison if a string you would search for has a %, _, [ ], [^ ] or quotes?

Toni



I've now added the []'s around the table_name and column_name. I'm not sure when the update will be posted but its a fairly simple change.

Change this line

' FROM ' + @Table_Name + ' (NOLOCK) WHERE ' + @Column_Name + ' LIKE ''%' + @Search_For + '%'''



To this line

' FROM [' + @Table_Name + '] (NOLOCK) WHERE [' + @Column_Name + '] LIKE ''%' + @Search_For + '%'''



As far as pattern searching goes if you want to search on a pattern matching character put it in []'s. So for example if you want to search for the literal string 'test%' you would put it as 'test[%]'.

I thought about putting it in code but then you couldn't add extra patterns. For example as it stands you can search on the word 'test' with a single digit number after it by making the search string 'test[0-9]'

Kenneth

Kenneth FisherI strive to live in a world where a chicken can cross the road without being questioned about its motives.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
Philip Davis
Philip Davis
Valued Member
Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)

Group: General Forum Members
Points: 67 Visits: 100
I admit my SQL knowledge is extremely limited, but am I missing something very basic here? All I get is an Error 208 - Invalid Object Name INFORMATION_SCHEMA.Tables. It would appear that these views only exist in and relate to the master database on our system. All of our other databases, setup by applications such as a Sage ERP system, Backup Exec, etc., have no such views.
Kenneth Fisher
Kenneth Fisher
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6511 Visits: 2064
They are system views. They do only exist in Master but everyone should have access to them and they can be used from any database. Just like the system stored procedures. I'm honestly not sure why you can't use them normally. If you want I can re-write it to work on the base system tables.

Kenneth FisherI strive to live in a world where a chicken can cross the road without being questioned about its motives.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
Philip Davis
Philip Davis
Valued Member
Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)

Group: General Forum Members
Points: 67 Visits: 100
Actually the answer was quite simple: quite why I am not sure, but on our system all these objects are defined with upper case names and have to be referenced as such, e.g. "INNER JOIN INFORMATION_SCHEMA.TABLES Tables ON Columns.TABLE_NAME = Tables.TABLE_NAME". Many Thanks.
Kenneth Fisher
Kenneth Fisher
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6511 Visits: 2064
Actually I'll bet you have your default collation set to be case sensative. On all of the systems I have run it on the server has been case in-sesative so I didn't have any problems.

Hope you find the script helpful Smile

Kenneth

Kenneth FisherI strive to live in a world where a chicken can cross the road without being questioned about its motives.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/Link to my Blog Post --> www.SQLStudies.com
punkcoi
punkcoi
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 60
How do you see the results?. When I execute the script I get a bunch of '(1 row(s) affected'. Thanks for your assistance.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search