Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...search entire database with Host...SQLsearch entire database with Host...SQL
Previous
 
Next
New Post
7/8/2009 6:48 PM
 

is there a simple SQL statement to search for a keyword in every table in the database?

And, then return the table it's in.

My host doesn't have a database search feature for some reason...

 
New Post
7/8/2009 10:05 PM
 

Here's how to do it:

1. Login as host

2. Go to Host...SQL.

3.  Paste the following, check Run as Script, hit Execute

 

 

CREATE PROCEDURE FindMyData_String
    @DataToFind NVARCHAR(4000),
    @ExactMatch BIT = 0
AS
SET NOCOUNT ON
 
DECLARE @Temp TABLE(RowId INT IDENTITY(1,1), TableName sysname, ColumnName SysName, DataType VARCHAR(100), DataFound BIT)
 
    INSERT  INTO @Temp(TableName, ColumnName, DataType)
    SELECT  C.Table_Name, C.Column_Name, C.Data_Type
    FROM    Information_Schema.Columns AS C
            INNER Join Information_Schema.Tables AS T
                ON C.Table_Name = T.Table_Name
    WHERE   Table_Type = 'Base Table'
            And Data_Type In ('ntext','text','nvarchar','nchar','varchar','char')
 
 
DECLARE @i INT
DECLARE @MAX INT
DECLARE @TableName sysname
DECLARE @ColumnName sysname
DECLARE @SQL NVARCHAR(4000)
DECLARE @PARAMETERS NVARCHAR(4000)
DECLARE @DataExists BIT
DECLARE @SQLTemplate NVARCHAR(4000)
 
SELECT  @SQLTemplate = CASE WHEN @ExactMatch = 1
                            THEN 'If Exists(Select *
                                           From   [ReplaceTableName]
                                           Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                        = ''' + @DataToFind + '''
                                           )
                                      Set @DataExists = 1
                                  Else
                                      Set @DataExists = 0'
                            ELSE 'If Exists(Select *
                                           From   [ReplaceTableName]
                                           Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                        Like ''%' + @DataToFind + '%''
                                           )
                                      Set @DataExists = 1
                                  Else
                                      Set @DataExists = 0'
                            END,
        @PARAMETERS = '@DataExists Bit OUTPUT',
        @i = 1
 
SELECT @i = 1, @MAX = MAX(RowId)
FROM   @Temp
 
WHILE @i <= @MAX
    BEGIN
        SELECT  @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName', TableName), 'ReplaceColumnName', ColumnName)
        FROM    @Temp
        WHERE   RowId = @i
 
 
        PRINT @SQL
        EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @DataExists = @DataExists OUTPUT
 
        IF @DataExists =1
            UPDATE @Temp SET DataFound = 1 WHERE RowId = @i
 
        SET @i = @i + 1
    END
 
SELECT  TableName, ColumnName
FROM    @Temp
WHERE   DataFound = 1
 
go
 
 
 
 
 
 
-----------------------------------------
 
4. after the script runs, take check out of Run As Script, delete all the text (if still there)... and paste following (replace whateveryouwantTofind)...and hit execute
 
 
EXEC FindMyData_string 'whateveryouwantTofind', 0
 
 
New Post
7/9/2009 5:46 AM
 

nice !


Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
New Post
7/9/2009 7:02 PM
 

Thanks -- forgot to post credit where credit is due... http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/the-ten-most-asked-sql-server-questions--1

 
New Post
7/10/2009 5:54 PM
 

Interesting -- different than the SQL table search I use (posted recently in this thread).  I'll have to give this one a whirl and see if it's noticeably faster than the old solution I've been relying on the last few years.  I'm wondering about the performace difference of repeatedly calling a stored procedure (as this solution does) versus keeping the repeat SELECTs internal as the other solution does.
 
Simplify the Code
Note that you can simplify the posted SQL by getting rid of that CASE statement.  All the CASE is doing is determining whether you're looking for an exact match or a substring match.  Instead of repeating code in the THEN and ELSE clauses just perform a check prior to the SELECT for @ExactMatch = 1.  If true, set @DataToFind = '%' + @DataToFind + '%' and you're all done.  You can replace the whole CASE statement with just the inside of the "THEN" clause as it appears except change the "=" to "LIKE".
 
 
Thanks for sharing!
 
-mamlin
 
 


esmamlin atxgeek.me
 
Previous
 
Next
HomeHomeOur CommunityOur CommunityGeneral Discuss...General Discuss...search entire database with Host...SQLsearch entire database with Host...SQL


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out