Tim

Footprints in the snow of a warped mind

StoredProcedure

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Tag Cloud

AJAX (4) ASP (6) ASP.Net (50) Error Reporting (4) Web Service (1) WSDL (1) Atlas (2) Business (76) Business Start-up Advice (25) Client (14) Expanding Your Business (17) C# (16) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (4) DDD (1) Design (9) Icons (1) Development (12) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Home Cinema (2) Hosting (2) IIS (10) iPhone (1) JavaScript (4) jQuery (1) Marketing (5) Email (1) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (2) Press Release (1) Productivity (2) Security (2) SEO (5) Server Maintenance (4) Server Management (9) Social Media (1) Social Networking (2) Experiment (1) Software (9) Office (5) Visual Studio (12) Windows (4) Vista (1) SQL (1) SQL Server (13) Stored Procedure (1) Testing (1) The Site Doctor (104) Turnover Challenge (1) Twitter (2) Umbraco (17) 2009 (1) Web Development (54) WebDD (33) Wii (1)

Blog Archive

Search

<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Recent Tracks

last.fm - The Social Music Revolution

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.2.8279.16125

Send mail to the author(s) Email Me (Tim Gaunt)

© 2010 Tim Gaunt.

Sign In

    # Friday, February 19, 2010

    Search every table and field in a SQL Server Database Updated

    Friday, February 19, 2010 12:07:28 PM (GMT Standard Time, UTC+00:00)

    As some of my blog posts are a little out of date, I thought I would spend some time updating the most popular ones. As I use this script on a regular basis and there was an error with the original posting, I thought I'd update it with a "corrected" version to get things started.

    If you want to see the original script, you can refer to How to search every table and field in a SQL Server Database. This one's just fixed :)

    CREATE PROC SearchAllTables
    (
        @SearchStr nvarchar(100)
    )
    AS
    
    BEGIN
    DECLARE @SearchStr nvarchar(100)
    SET @SearchStr = 'test'
        -- Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved.
        -- Purpose: To search all columns of all tables for a given search string
        -- Written by: Narayana Vyas Kondreddi
        -- Site: http://vyaskn.tripod.com
        -- Tested on: SQL Server 7.0 and SQL Server 2000
        -- Date modified: 28th July 2002 22:50 GMT
        CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
    
        SET NOCOUNT ON
    
        DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128), @SearchStr2 nvarchar(110)
        SET  @TableName = ''
        SET @SearchStr2 = QUOTENAME('%' + @SearchStr + '%','''')
    
        WHILE @TableName IS NOT NULL
        
        BEGIN
            SET @ColumnName = ''
            SET @TableName = 
            (
                SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
                FROM     INFORMATION_SCHEMA.TABLES
                WHERE         TABLE_TYPE = 'BASE TABLE'
                    AND    QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                    AND    OBJECTPROPERTY(
                            OBJECT_ID(
                                QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)
                                 ), 'IsMSShipped'
                                   ) = 0
            )
    
            WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)
                
            BEGIN
                SET @ColumnName =
                (
                    SELECT MIN(QUOTENAME(COLUMN_NAME))
                    FROM     INFORMATION_SCHEMA.COLUMNS
                    WHERE         TABLE_SCHEMA    = PARSENAME(@TableName, 2)
                        AND    TABLE_NAME    = PARSENAME(@TableName, 1)
                        AND    DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
                        AND    QUOTENAME(COLUMN_NAME) > @ColumnName
                )
        
                IF @ColumnName IS NOT NULL
                
                BEGIN
                    INSERT INTO #Results
                    EXEC
                    (
                        'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
                        ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
                    )
                END
            END    
        END
    
        SELECT ColumnName, ColumnValue FROM #Results
    END
    
    DROP TABLE #Results