We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,154 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do I search for a text in a database but I dont know what table its in

Hey

I want to search for a text string but I dont know what table or column it is in. I just know the database that it is in. I know also that the type is a text type.

Any query I can do or in MySQL Workbench?

Thanks!

4
Contributors
6
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
7
Views
riahc3
 
Team Colleague
1,300 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11

You could use the concat function:

select * from mytable where concat(field1,field2,field3...) like '%searchtext%'

This is highly inefficient, but with small databases it works.
An alternative is to export (dump) the database to a text file and then search with grep or a text editor.

smantscheff
Nearly a Posting Virtuoso
1,297 posts since Oct 2010
Reputation Points: 321
Solved Threads: 270
Skill Endorsements: 8

You could use the concat function:

select * from mytable where concat(field1,field2,field3...) like '%searchtext%'
This is highly inefficient, but with small databases it works.
An alternative is to export (dump) the database to a text file and then search with grep or a text editor.

The problem is that there is a large ammount of columns so I can add them. Does
:

Select * from * where concat(*) like 'textimsearchingforhere'

Work?

riahc3
 
Team Colleague
1,300 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11

What kind of a problem is that? Is the number of columns larger than the maximum number of arguments for concat? Or are you just too lazy to type all the names?
No, your suggestion does not work. concat takes string arguments.

smantscheff
Nearly a Posting Virtuoso
1,297 posts since Oct 2010
Reputation Points: 321
Solved Threads: 270
Skill Endorsements: 8

In my library I've got a script found ages ago in the internet. It's for MS SQL server but it might help you get an idea or something.

This stored procedure creates a temporary table for storing search result data and steps through each column found.  
CREATE PROC [dbo].[SearchAllTablesAllColumns]
(
   @SearchStr nvarchar(100)
)
AS
BEGIN
   -- Purpose: To search all columns in all tables for a given search string
   -- Written by: Francisco Tapia
   -- Site: http://sqlthis.blogspot.com
   -- Inspired by: Narayana Vyas Kondreddi    
   -- http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm
   -- Tested on: SQL Server 7.0, 2000 and 2005
   -- Date modified: June 23, 2009

   CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

   SET NOCOUNT ON

   DECLARE @TableName nvarchar(256), @ColumnName nvarchar(255), @Parse AS INT

   SELECT @TableName = '', @ColumnName = ''
   SET @SearchStr = QUOTENAME('%' + @SearchStr + '%','''')

      WHILE (@ColumnName IS NOT NULL) 
      BEGIN
      SET @ColumnName = (
         SELECT MIN(QUOTENAME(C.Table_Schema) + '.' + QUOTENAME(C.Table_name)
         + '|' + QUOTENAME(C.Column_name))
               FROM INFORMATION_SCHEMA.COLUMNS c
                  INNER JOIN INFORMATION_SCHEMA.TABLES t on c.table_schema = t.table_schema and c.Table_name = t.table_name
               WHERE    T.TABLE_TYPE = 'BASE TABLE'
                  AND C.DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'text', 'ntext')
                  AND   QUOTENAME(C.Table_Schema) + '.' + QUOTENAME(C.Table_name) + '.' + QUOTENAME(COLUMN_NAME) > @TableName 

+ '.' + @ColumnName 

               )

         SET @Parse = PATINDEX ('%|%', @ColumnName)

         SET @TableName = SUBSTRING(@ColumnName, 1, @Parse - 1)
         SET @ColumnName = SUBSTRING(@ColumnName, @Parse +1, LEN(@ColumnName))    

         IF @ColumnName IS NOT NULL
         BEGIN
            INSERT INTO #Results
            EXEC
            (
               'SELECT ''' + @TableName + '.' + @ColumnName + ''', SUBSTRING('
               + @ColumnName + ',1, 3630) 
               FROM ' + @TableName + ' (NOLOCK) ' +
               ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr
            )
         END
      END   

   SELECT ColumnName, ColumnValue FROM #Results
   ORDER BY ColumnName 

END
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

What kind of a problem is that? Is the number of columns larger than the maximum number of arguments for concat? Or are you just too lazy to type all the names?
No, your suggestion does not work. concat takes string arguments.

Well, there are about 200-300 tables. Im not sure if that reaches the max and/or you would type them ALL out.....

In my library I've got a script found ages ago in the internet. It's for MS SQL server but it might help you get an idea or something. This stored procedure creates a temporary table for storing search result data and steps through each column found. CREATE PROC [dbo].[SearchAllTablesAllColumns]( @SearchStr nvarchar(100))ASBEGIN -- Purpose: To search all columns in all tables for a given search string -- Written by: Francisco Tapia -- Site: http://sqlthis.blogspot.com -- Inspired by: Narayana Vyas Kondreddi -- http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm -- Tested on: SQL Server 7.0, 2000 and 2005 -- Date modified: June 23, 2009 CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630)) SET NOCOUNT ON DECLARE @TableName nvarchar(256), @ColumnName nvarchar(255), @Parse AS INT SELECT @TableName = '', @ColumnName = '' SET @SearchStr = QUOTENAME('%' + @SearchStr + '%','''') WHILE (@ColumnName IS NOT NULL) BEGIN SET @ColumnName = ( SELECT MIN(QUOTENAME(C.Table_Schema) + '.' + QUOTENAME(C.Table_name) + '|' + QUOTENAME(C.Column_name)) FROM INFORMATION_SCHEMA.COLUMNS c INNER JOIN INFORMATION_SCHEMA.TABLES t on c.table_schema = t.table_schema and c.Table_name = t.table_name WHERE T.TABLE_TYPE = 'BASE TABLE' AND C.DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'text', 'ntext') AND QUOTENAME(C.Table_Schema) + '.' + QUOTENAME(C.Table_name) + '.' + QUOTENAME(COLUMN_NAME) > @TableName + '.' + @ColumnName ) SET @Parse = PATINDEX ('%|%', @ColumnName) SET @TableName = SUBSTRING(@ColumnName, 1, @Parse - 1) SET @ColumnName = SUBSTRING(@ColumnName, @Parse +1, LEN(@ColumnName)) IF @ColumnName IS NOT NULL BEGIN INSERT INTO #Results EXEC ( 'SELECT ''' + @TableName + '.' + @ColumnName + ''', SUBSTRING(' + @ColumnName + ',1, 3630) FROM ' + @TableName + ' (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr ) END END SELECT ColumnName, ColumnValue FROM #Results ORDER BY ColumnName END

Intresting :) Thanks

riahc3
 
Team Colleague
1,300 posts since May 2008
Reputation Points: 62
Solved Threads: 13
Skill Endorsements: 11

Well, there are about 200-300 tables. Im not sure if that reaches the max and/or you would type them ALL out.....

If this is a one-time problem, dump the database and grep the output.
If it is a recurring challenge, use a script like adam_k proposed. In PHP you could write a routine which retrieves all table names, then walks through the tables retrieving all the column names and combining them into a concat expression.

smantscheff
Nearly a Posting Virtuoso
1,297 posts since Oct 2010
Reputation Points: 321
Solved Threads: 270
Skill Endorsements: 8

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0739 seconds using 2.69MB