About Me

My photo
Colombo, Sri Lanka
Professional Report/ Technical/ Blog/ Academic and Ghost Article Writer,Application Developer, Database Administrator, Content Creator and Project Manager in a wide variety of business & enterprise applications. Particularly interested in client/server and relational database design using MS-SQL Server & Oracle. Always interested in new hi-tech projects, as well as close interaction with the DB querying & reporting. Also a specialist in Education Management. Actively seeking the processes for merging Enterprise Lean Sigma (ELS) with IT.

Monday, 29 November 2010

Useful T-SQL Sripts

Search all columns in every table for a value


Have you ever known a value in a database but had no clue what column or table it resided in? If so, you understand how frustrating it can be to spend great amounts of time searching through masses of information for the proverbial needle in a haystack. To help identify locations holding a given value, a search script can be used to spotlight all of the areas containing the needed character string.
The below script references the SQL Server metadata view, "information_schema.columns". As this provides a listing of all columns and tables within a database, it can be used as a foundation of a search. The script uses a common table expression query to populate a temporary table full of individual queries (one for each column in the database -- so it can get large and may take a few minutes). After that a "while" loop is used to execute these statements. The final result is a data set displaying all of the tables and columns matching the information searched for.
To use this script, simply place it within a query window and change the @searchValue parameter. I find it very helpful when used with an application UI - I can enter a value into the interface and use this script to find where in the database the value was stored.
Note: A few data types, such as image, cannot be converted to varchar and thus are ignored. The varchar data type was used as the base type (all data is converted to this) as it produced the fastest standardized value for comparison.
Here is the script:


--initialize transaction
set transaction isolation level read uncommitted
set nocount on
--initial declarations
declare @rowID int, @maxRowID int
declare @sql nvarchar(4000)
declare @searchValue varchar(100)
declare @statements table (rowID int, SQL varchar(8000))
create table #results (tableName varchar(250), tableSchema varchar(250), columnName varchar(250))
set @rowID = 1
set @searchValue = 'test'
--create CTE table holding metadata
;with MyInfo (tableName, tableSchema, columnName) as (
select table_name, table_schema, column_name from information_schema.columns where data_type not in ('image','text','timestamp','binary','uniqueidentifier')
)
--create search strings
insert into @statements
select row_number() over (order by tableName, columnName) as rowID, 'insert into #results select distinct '''+tableName+''', '''+tableSchema+''', '''+columnName+''' from ['+tableSchema+'].['+tableName+'] where convert(varchar,['+columnName+']) like ''%'+@searchValue+'%''' from myInfo
--initialize while components and process search strings
select @maxRowID = max(rowID) from @statements
while @rowID <= @maxRowID
 begin
 select @sql = sql from @statements where rowID = @rowID
 exec sp_executeSQL @sql
 set @rowID = @rowID + 1
 end
--view results and cleanup
select * from #results
drop table #results

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...