No ha pasado que se desea buscar un valor en todas las tablas y columnas de la base de datos. Bueno aquí dejo una sentencia sql server que ayudará en esta tarea y para ello solo se necesita definir el valor que se desea buscar en la variable
"@searchWord" y ejecutar toda la sentencia.
El ejemplo que se muestra busca la palabra
"http" dentro de todas las tablas y columnas.
SET NOCOUNT ON
DECLARE @searchWord AS VARCHAR(255)
SET @searchWord = 'http'
IF OBJECT_ID('tempdb..#tmpResults') IS NOT NULL
DROP TABLE #tmpResults
CREATE TABLE #tmpResults (idRow int identity(1,1), tableName VARCHAR(255), ColumnName VARCHAR(255))
DECLARE @idTable AS INT
SELECT @idTable = MIN(OBJECT_ID)
FROM sys.tables
WHILE @idTable IS NOT NULL
BEGIN
DECLARE @nameTable AS VARCHAR(255)
SELECT TOP 1 @nameTable = RTRIM(LTRIM(name))
FROM sys.tables
WHERE OBJECT_ID = @idTable
DECLARE @idColumn AS INT
SELECT @idColumn = MIN(COLUMN_ID)
FROM sys.columns
WHERE OBJECT_ID = @idTable
WHILE @idColumn IS NOT NULL
BEGIN
DECLARE @nameColumn AS VARCHAR(255)
SELECT @nameColumn = RTRIM(LTRIM(name))
FROM sys.columns
WHERE OBJECT_ID = @idTable
AND COLUMN_ID = @idColumn
DECLARE @sqlCommand AS NVARCHAR(255)
SET @sqlCommand = N'SELECT @value=COUNT(1) FROM ' + @nameTable + ' WHERE ' + @nameColumn + ' LIKE ''%' + @searchWord + '%'''
DECLARE @sqlParameter AS NVARCHAR(20)
SET @sqlParameter = N'@value INT OUTPUT'
DECLARE @valueOut AS INT
EXECUTE sp_executesql @sqlCommand,@sqlParameter,@value=@valueOut OUTPUT
IF @valueOut > 0
INSERT INTO #tmpResults(tableName, columnName) VALUES (@nameTable,@nameColumn)
SELECT @idColumn = MIN(COLUMN_ID)
FROM sys.columns
WHERE OBJECT_ID = @idTable
AND COLUMN_ID > @idColumn
END
SELECT @idTable = MIN(OBJECT_ID)
FROM sys.tables
WHERE OBJECT_ID > @idTable
END
SELECT * FROM #tmpResults
IF OBJECT_ID('tempdb..#tmpResults') IS NOT NULL
DROP TABLE #tmpResults
SET NOCOUNT OFF
Esta consulta devolverá como resultado el nombre de la tablas y las columnas en donde encontró la palabra
"http".