search all database tables for a value sqlserver

Solutions on MaxInterview for search all database tables for a value sqlserver by the best coders in the world

showing results for - "search all database tables for a value sqlserver"
Emmanuelle
04 Nov 2017
1DECLARE @SearchStr nvarchar(100)
2SET @SearchStr = '## YOUR STRING HERE ##'
3 
4 
5-- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
6-- Purpose: To search all columns of all tables for a given search string
7-- Written by: Narayana Vyas Kondreddi
8-- Site: http://vyaskn.tripod.com
9-- Updated and tested by Tim Gaunt
10-- http://www.thesitedoctor.co.uk
11-- http://blogs.thesitedoctor.co.uk/tim/2010/02/19/Search+Every+Table+And+Field+In+A+SQL+Server+Database+Updated.aspx
12-- Tested on: SQL Server 7.0, SQL Server 2000, SQL Server 2005 and SQL Server 2010
13-- Date modified: 03rd March 2011 19:00 GMT
14CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
15 
16SET NOCOUNT ON
17 
18DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
19SET  @TableName = ''
20SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
21 
22WHILE @TableName IS NOT NULL
23 
24BEGIN
25    SET @ColumnName = ''
26    SET @TableName = 
27    (
28        SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
29        FROM     INFORMATION_SCHEMA.TABLES
30        WHERE         TABLE_TYPE = 'BASE TABLE'
31            AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
32            AND    OBJECTPROPERTY(
33                    OBJECT_ID(
34                        QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
35                         ), 'IsMSShipped'
36                           ) = 0
37    )
38 
39    WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
40         
41    BEGIN
42        SET @ColumnName =
43        (
44            SELECT MIN(QUOTENAME(COLUMN_NAME))
45            FROM     INFORMATION_SCHEMA.COLUMNS
46            WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
47                AND    TABLE_NAME    = PARSENAME(@TableName, 1)
48                AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
49                AND    QUOTENAME(COLUMN_NAME) > @ColumnName
50        )
51 
52        IF @ColumnName IS NOT NULL
53         
54        BEGIN
55            INSERT INTO #Results
56            EXEC
57            (
58                'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
59                ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
60            )
61        END
62    END   
63END
64 
65SELECT ColumnName, ColumnValue FROM #Results
66 
67DROP TABLE #Results
68
queries leading to this page
search whole table for value in sqlto sql search tables rows for a valuesearch a value in all tables in sql serversearch table in all database sql serversearch a value from all tables where sql serversearch all database tables for a valuesql search string all tablessearch all tables of a value in sqlfind value in all field sqlsql query search value in all tablessearch sql database table for valuesql query search all tables for valuesearch in all tables for a column sql servercheck if a field has certain value sqlmoddify table sqlsql server search tables for valuesearch for value across all tables in sqlsearch field in all tables sqlsql find all columns with a particular valuesearch all tables in database for valuesearch tables for values in column sqlhow to search all tables in a database for a value sql serversql server search table in all databasessql find all value in listsql search all tablessql server search value in all tables value intsql query to search for a string in all tablessql search all tables in db for stringhow to search through all tables in sqlsql search for a value in the tablesearch for column value in all tables sqlsql search all tables for valuehow to search tables in sqlfind something in all table sqlfind a table in all sql server databasessql search all tables for stringfind value from sql tablesql query find value in all tablessearch table on sql servercan we find any value in all tables in sql server databasesql query to find tables with a specific value in a columnsql search value from all tablessql find all values with string in itcopyright c2 a9 2002 narayana vyas kondreddi all rights reserved sql search in all tablessearch all tables in a database for a valuemssql search all tables for valuesearch for a table in sql serversearch for a value in all tables sqlsql find value in all tablessql search string in all tableshow to search all tables in sqlsql search tables for columnsearch data table for a value sql serversearch for value in a table in table sqlt sql how to search data in all tableshow to find a value in a table sqlcheck value all tables in sqlhow to search for a table in sql check a value is used in entire databasesql search for a value in all tablessearch in all tables sqlsql search field value in all tablesget all tables in sqlserver databasesearch all tables for value sqlsql server search for value in all tablessearch tables in db for valuefind value in all tables sqlsearch sql table for valuemssql search value in all tableshow to search value in table sqlsql server search text in all tablessql search for a field in all tablessql find all instances of a artibutesearch value in all tables sqlt sql search tables for valuesql search in table and return itsearch a table for a value sqlhow to search for a column in all tables in sqlsearch all tables for string sqlsql search entire database for valuesql server search in all tablessql search table for valuesql find all tables with a particular valuesql search in tablessearch all tables in sql for a valuesearch all tables for valuesearch all sql tables for a valuesearch all tables in sqlsql search for value in all tablessql server how to find a value in all tablessql search all tables for attributesfind a column value in all of the databasesql find in all tablessearch a value in all tables sql serversearch a value in all tables sqlsearch value in table sqlsql search in all tables sql serversql query to find tables with a specific value in any columnsql server find tables in databasesearch value in all database tablessearch whole table for value sqlsearch sql tables for valuesql server search all tables for valuesql server find value in all tablessql select in all tables contains valuesql server find table in all databaseshow to search all tables in a database for a valuesearch all database tables for a value sqlserver