Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Search all columns in every table for a value Expand / Collapse
Author
Message
Posted Wednesday, January 6, 2010 11:44 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 12, 2013 5:48 AM
Points: 27, Visits: 163
Comments posted to this topic are about the item Search all columns in every table for a value
Post #843074
Posted Monday, January 25, 2010 3:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 15, 2012 1:12 AM
Points: 185, Visits: 43
One good idea is only search from columns which column length is
greather than @searchValue length


set nocount on

--initial declarations
declare @rowID INT, @maxRowID INT
declare @sql NVARCHAR(4000)
declare @statements TABLE (rowID INT, SQLL NVARCHAR(MAX) COLLATE DATABASE_DEFAULT)
CREATE TABLE #results (tableName NVARCHAR(250) COLLATE DATABASE_DEFAULT, tableSchema NVARCHAR(250) COLLATE DATABASE_DEFAULT
, columnName NVARCHAR(250) COLLATE DATABASE_DEFAULT, foundtext NVARCHAR(MAX) COLLATE DATABASE_DEFAULT )

SET @rowID = 1
declare @searchValue NVARCHAR(100)
SET @searchValue = 'text to find'
DECLARE @pikkus INT
SET @pikkus=LEN(@searchValue)


--TEXT 35
--NTEXT 99
--VARCHAR 167
--CHAR 175
--NVARCHAR, SYSNAME 231
--NCHAR 239
--XML 241

--create CTE table holding metadata
;WITH MyInfo (tableName, tableSchema, columnName, XTYPE) AS (
SELECT sysobjects.name AS tableName, USER_NAME(sysobjects.uid) AS tableSchema
, syscolumns.name AS columnName, syscolumns.XTYPE
FROM sysobjects WITH(NOLOCK) INNER JOIN syscolumns WITH(NOLOCK)
ON (sysobjects.id = syscolumns.id)
WHERE sysobjects.xtype = 'U' AND sysobjects.category=0
AND sysobjects.name <> 'sysdiagrams' --MSSQL diagrams
AND syscolumns.XTYPE IN (35,99,167,175,231,239,214) AND syscolumns.prec >= @pikkus
)

--create search strings

INSERT INTO @statements
SELECT row_number() over (order by tableName, columnName) AS rowID, 'INSERT INTO #results SELECT '''+tableName+''', '''+tableSchema+''', '''+columnName+''', CAST('+columnName+' AS NVARCHAR(MAX)) FROM ['+tableSchema+'].['+tableName+'] WITH (NOLOCK) WHERE '+
CASE WHEN myInfo.XTYPE=241 --XML
THEN +'CONVERT(NVARCHAR(MAX),['+columnName+'])'
ELSE '['+columnName+']'
END+' LIKE ''%'+@searchValue+'%'''
FROM myInfo

--select * from @statements

--initialize while components and process search strings
SET @maxRowID = ( SELECT MAX(rowID) FROM @statements )
WHILE @rowID <= @maxRowID
BEGIN
SET @sql = (SELECT sqll FROM @statements WHERE rowID = @rowID )
EXEC sp_executeSQL @sql
SET @rowID = @rowID + 1
END

--view results and cleanup
SELECT * FROM #results
drop table #results
Post #852905
Posted Monday, January 25, 2010 6:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 23, 2010 7:57 AM
Points: 1, Visits: 18
First of all, thank you for the query. If I can get it to run, it will definitely be a very handy tool.

Unfortunately, I am receiving the following error:
Msg 8170, Level 16, State 2, Line 1
Insufficient result space to convert uniqueidentifier value to char.

I tried adding "uniqueidentifier" to the "...data_type not in..." clause of the statement (after all, we certainly do not need to search those fields), but I still receive the same results.

Any ideas?

Thanks.
Post #852960
Posted Monday, January 25, 2010 6:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, November 12, 2013 5:48 AM
Points: 27, Visits: 163
Thanks for the feedback Kevin - that data type should have been included.

I replicated the error (by creating a table with a GUID) and then modified the section that limits the types...all worked for me.

replace:

select table_name, table_schema, column_name from information_schema.columns where data_type not in ('image','text','timestamp')

with:

select table_name, table_schema, column_name from information_schema.columns where data_type not in ('image','text','timestamp','uniqueidentifier')

I'll update the script on the article. I also like the above comment to limit the field search based on the column size. No use looking in a two character field to see if a 5 character string exists!!! That is the great part about a forum like this -- many people will suggest improvements. Also, to anyone else out there I am aware of the ms_forEachTable procedure but decided to write the script as shown.
Post #852967
Posted Monday, January 25, 2010 6:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, May 30, 2014 7:53 AM
Points: 33, Visits: 117
I keep getting errors related to a view:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Short Name'.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'dbo.assets_in_supportworks_not_in_dynamics' because of binding errors.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Short Name'.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'dbo.assets_in_supportworks_not_in_dynamics' because of binding errors.
Msg 207, Level 16, State 1, Line 1
Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.tq_config_temp1'.
Msg 4413, Level 16, State 1, Line 1
Could not use view or function 'dbo.Missing ME Relationships' because of binding errors.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ''.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ''.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ''.

Is there a way I can get it to ignore errors?
Post #852968
Posted Monday, January 25, 2010 7:36 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 12:55 PM
Points: 644, Visits: 2,139
I have a colleague working on a side project to search all columns for any errant information that looks like a credit card and I was wondering if there are performance issues involved with this script? He's going about the concept of creating "dynamic" stored procedures as they run faster, but still trying to optimize it. Will this script work efficiently for truly massive amounts of data?

Gaby
________________________________________________________________
"In theory, theory and practice are the same. In practice, they are not."
- Albert Einstein
Post #852999
Posted Monday, January 25, 2010 7:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, October 15, 2012 1:12 AM
Points: 185, Visits: 43
No, it will not run efficiently, because there is CAST() and LIKE %search% operations in conditions which will not able to use indexes

GabyYYZ (1/25/2010)
I have a colleague working on a side project to search all columns for any errant information that looks like a credit card and I was wondering if there are performance issues involved with this script? He's going about the concept of creating "dynamic" stored procedures as they run faster, but still trying to optimize it. Will this script work efficiently for truly massive amounts of data?
Post #853014
Posted Monday, January 25, 2010 1:06 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 17, 2013 5:31 PM
Points: 23, Visits: 91
You might consider removing the wildcards from the dynamic sql and allow them to be specified in the search string. That way, you could explicitly specify left based LIKE statements that would allow you to take advantage of indexes on some fields. That would certainly speed up *some* of the tests. But basically, by it's very nature, you're doing a massive amount of text based searching here so the larger the date sets, the longer the result time.

This seems like a pretty cool script for those one-off "I wonder" kind of tests where you don't mind waiting a bit, but if you're implementing something business critical (like the credit card pattern checking example), you'll probably want to be a little more specific about what fields your testing in what tables. No doubt your application doesn't allow updating of most fields through whatever the interface is except certain textual fields. Focus on scanning only those and you'll get better results. No need to be testing date fields for possible entry of credit card numbers, right?

Tim
Post #853255
Posted Monday, January 25, 2010 1:12 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 12:55 PM
Points: 644, Visits: 2,139
Timothy Graffham (1/25/2010)
You might consider removing the wildcards from the dynamic sql and allow them to be specified in the search string. That way, you could explicitly specify left based LIKE statements that would allow you to take advantage of indexes on some fields. That would certainly speed up *some* of the tests. But basically, by it's very nature, you're doing a massive amount of text based searching here so the larger the date sets, the longer the result time.

This seems like a pretty cool script for those one-off "I wonder" kind of tests where you don't mind waiting a bit, but if you're implementing something business critical (like the credit card pattern checking example), you'll probably want to be a little more specific about what fields your testing in what tables. No doubt your application doesn't allow updating of most fields through whatever the interface is except certain textual fields. Focus on scanning only those and you'll get better results. No need to be testing date fields for possible entry of credit card numbers, right?

Tim

Definitely, restrict to char/varchar/nvarchar/(text?)


Gaby
________________________________________________________________
"In theory, theory and practice are the same. In practice, they are not."
- Albert Einstein
Post #853261
Posted Monday, January 25, 2010 1:49 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:46 PM
Points: 79, Visits: 1,331
A few years back I needed to search for data in some DBs I wasn't familiar with. So I created a quick and dirty script to do this. I put together a few versions of it, one for string/character based stuff, another for numerics and so on as needed. Here is what I did for searching for strings. I used dynamic sql to put together the queries and opted to PRINT them out rather than select them to rows so there are a few tricks I needed to use to implement this. It does still have some bugs (like field type of text isn't implemented) I haven't worked out but thought I would present it as one way of doing this sort of thing.

--Script to look for varchar data in tables 
--Run this replacing the value in @dataLookingFor with the exact string you are looking for
--check the Messages tab for info on what tables have the string
DECLARE
@staticSql nvarchar(max)
,@dynamicSql nvarchar(max)
,@combinedSql nvarchar(max)
,@dataLookingFor varchar(100)
,@parmDefinition nvarchar(100)
,@dateStart datetime
,@dateEnd datetime
,@dateTot datetime
,@print varchar(100)
;

SELECT @dateStart = GETDATE()
SET @dataLookingFor = 'SomeString';
SET @parmDefinition = N'@data varchar(100)'; --change to size/type needed
SELECT
@dynamicSql = theSql.value('/row[1]/column_alias[1]', 'nvarchar(max)')
FROM
(
SELECT column_alias
FROM
(
SELECT N'IF EXISTS(SELECT 1 FROM '
+ N'dbo.[' + C.TABLE_NAME + N']'
+ N' WHERE [' + C.COLUMN_NAME + N'] = @data) BEGIN '
+ N' SELECT @cnt = CAST(COUNT(*) AS varchar(20)) FROM ' + N'dbo.[' + C.TABLE_NAME + N']'+ N' WHERE [' + C.COLUMN_NAME + N'] = @data;'
+ N' RAISERROR (''' + C.TABLE_NAME + '.' + C.COLUMN_NAME + N' has data - %d times'', 0, 1, @cnt) WITH NOWAIT;'
+ N' END'
+ N';'
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN
INFORMATION_SCHEMA.TABLES T
ON C.TABLE_CATALOG = T.TABLE_CATALOG
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
AND C.TABLE_NAME = T.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'
--add tables and column types that you don't want to check (columns should remain as is since they are not string data)
-- AND T.TABLE_NAME NOT IN ('???')
AND C.DATA_TYPE NOT IN (
'bigint'
,'decimal'
,'int'
,'numeric'
,'smallint'
,'money'
,'tinyint'
,'smallmoney'
,'bit'
,'float'
,'real'
,'datetime'
,'smalldatetime'
,'binary'
,'image'
,'varbinary'
,'cursor'
,'timestamp'
,'sql_variant'
,'uniqueidentifier'
,'table'
,'xml' --got this type below
,'text' --need to implement this?
)
FOR XML PATH(''), TYPE
) tab_alias (column_alias)
FOR XML RAW, TYPE
) tab_alias2 (theSql)

SELECT @dateEnd = GETDATE()
SELECT @dateTot = @dateEnd - @dateStart
SELECT @print = 'Time elapsed for running create query: ' + CONVERT(VARCHAR(20),@dateTot,114)

--note that @cnt has to be create before the above query can use it in the query
--so we concatenate @staticSql with @dynamicSql
SET @staticSql = N' DECLARE @cnt int;'
+ N' RAISERROR (''Data being checked for - %s'', 0, 1, @data) WITH NOWAIT;'
+ N' RAISERROR ('''', 0, 1) WITH NOWAIT;'
SET @combinedSql = @staticSql + N' ' + @dynamicSql
-- PRINT @combinedSql
SELECT @dateStart = GETDATE()
EXEC sp_executesql
@combinedSql
,@parmDefinition
,@data=@dataLookingFor
;

SELECT @dateEnd = GETDATE()
PRINT ''
PRINT @print
SELECT @dateTot = @dateEnd - @dateStart
SELECT @print = 'Time elapsed for running query: ' + CONVERT(VARCHAR(20),@dateTot,114)
PRINT @print

--look in the XML now
SELECT @dateStart = GETDATE()

SELECT
@dynamicSql = theSql.value('/row[1]/column_alias[1]', 'nvarchar(max)')
FROM
(
SELECT column_alias
FROM
(
SELECT N'IF EXISTS(SELECT 1 FROM '
+ N'dbo.[' + C.TABLE_NAME + N']'
+ N' WHERE CAST([' + C.COLUMN_NAME + N'] AS varchar(max)) LIKE ''%' + @dataLookingFor + '%'') BEGIN '
+ N' SELECT @cnt = CAST(COUNT(*) AS varchar(20)) FROM ' + N'dbo.[' + C.TABLE_NAME + N']'+ N' WHERE CAST([' + C.COLUMN_NAME + N'] AS varchar(max)) LIKE ''%' + @dataLookingFor + '%'';'
+ N' RAISERROR (''' + C.TABLE_NAME + '.' + C.COLUMN_NAME + N' has data - %d times'', 0, 1, @cnt) WITH NOWAIT;'
+ N' END'
+ N';'
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN
INFORMATION_SCHEMA.TABLES T
ON C.TABLE_CATALOG = T.TABLE_CATALOG
AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
AND C.TABLE_NAME = T.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE'
--add tables and column types that you don't want to check (columns should remain as is since they are not string data)
-- AND T.TABLE_NAME NOT IN ('???')
AND C.DATA_TYPE ='xml'
FOR XML PATH(''), TYPE
) tab_alias (column_alias)
FOR XML RAW, TYPE
) tab_alias2 (theSql)

SELECT @dateEnd = GETDATE()
SELECT @dateTot = @dateEnd - @dateStart
SELECT @print = 'Time elapsed for running create query: ' + CONVERT(VARCHAR(20),@dateTot,114)

--note that @cnt has to be create before the above query can use it in the query
--so we concatenate @staticSql with @dynamicSql
SET @staticSql = N' DECLARE @cnt int;'
+ N' RAISERROR (''Data being checked for - %s'', 0, 1, @data) WITH NOWAIT;'
+ N' RAISERROR ('''', 0, 1) WITH NOWAIT;'
SET @combinedSql = @staticSql + N' ' + @dynamicSql
-- PRINT @combinedSql
SELECT @dateStart = GETDATE()
EXEC sp_executesql
@combinedSql
,@parmDefinition
,@data=@dataLookingFor
;

SELECT @dateEnd = GETDATE()
PRINT ''
PRINT @print
SELECT @dateTot = @dateEnd - @dateStart
SELECT @print = 'Time elapsed for running query: ' + CONVERT(VARCHAR(20),@dateTot,114)
PRINT @print


Post #853290
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse