Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Search All Tables Function

Posted on February 25, 2013October 26, 2015 By David Kittell

If you have access to create functions/procedures use this script.

CREATE PROC SearchAllTables (@SearchStr NVARCHAR(100))
AS
BEGIN
	-- 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'
							)
						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

Run the above function like this

EXEC  SearchAllTables ':'

If you don’t have access to create functions/procedures use this script.

DECLARE @SearchStr NVARCHAR(100)

SET @SearchStr = '<Search Term>';

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'
						)
					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

Originally Posted on February 25, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MSSQL

Post navigation

Previous post
Next post

Related Posts

UNIX – Laptop Setup

Posted on April 9, 2018April 9, 2018

This is my basic setup for a laptop/desktop that I have for my family to use. Debian Based Should work on Debian, Ubuntu (all Ubuntu flavors) sudo apt-get –assume-yes install ntp sudo timedatectl set-timezone America/Detroit sudo service ntp stop sudo ntpdate us.pool.ntp.org sudo service ntp start echo nameserver 208.67.222.123 >…

Read More

PowerShell – Backup / Restore IIS Site and Configuration

Posted on November 10, 2015June 8, 2017

<# .SYNOPSIS This script will archive IIS App Pool and IIS Sites .DESCRIPTION This script will archive all App Pool and IIS Site settings and configurations. This script will also allow the restore of the App Pool and IIS Site settings and configurations. .PARAMETER Path .PARAMETER LiteralPath .Example IISBackup.ps1 1…

Read More

DNN – Regain User Access

Posted on March 25, 2015October 26, 2015

If you have ever been locked out of a DNN site you maintain (must have SQL access) this solution will help. NOTE: If you don’t have SMTP settings configured this process will not help First find your “Portals” table and update “UserRegistration” = 2 Note: In the example below I…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes