This query will search a database for a specific column name. I would like to go one step further and search the returned columns for a specific value.
SELE
There is not such system table present for this kind of searching. Whereas you can try this for your purpose
DECLARE @ValueToSearch NVARCHAR(500)
DECLARE @SearchColumn NVARCHAR(100)
DECLARE @TableName NVARCHAR(200)
DECLARE @ColumnName NVARCHAR(200)
SET @ValueToSearch ='YOUR VALUE TP SEARCH'
SET @SearchColumn = 'YOUR COLUMN'
DECLARE @getResult CURSOR
SET @getResult = CURSOR FOR
SELECT t.name AS table_name,c.name AS column_name FROM sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE c.name = @SearchColumn
OPEN @getResult
FETCH NEXT FROM @getResult INTO @TableName,@ColumnName
WHILE @@FETCH_STATUS = 0
BEGIN
SET NOCOUNT ON ;
DECLARE @RESULT INT;
DECLARE @TYPE INT
DECLARE @QUERY NVARCHAR(1000)
SET @QUERY = 'select @RESULT=count(*) from ' + ISNULL(@TableName,'') +' WHERE '+ ISNULL(@ColumnName,'')+'='''+ ISNULL(@ValueToSearch,'') +''''
EXEC sp_executesql @QUERY,
N'@result int OUTPUT, @type int OUTPUT',
@RESULT OUTPUT,
@TYPE OUTPUT
IF(ISNULL(@RESULT,0)>0)
BEGIN
SET NOCOUNT ON;
SELECT ' COLUMN '+ @ColumnName + ' OF TABLE ' +@TableName+ ' HAS THIS VALUE.'
END
FETCH NEXT FROM @getResult INTO @TableName,@ColumnName
END
CLOSE @getResult
DEALLOCATE @getResult
Thanks
Manoj
For example, I have a database named Organisation. I have more than one table where tax_id column is present.
Most of the time, we have to find such a column from the whole database. The solution is provided below:
select table_name,column_name from information_schema.columns
where column_name like '%tax%'
There is no matter in query to database name which ever you just need to change willing Column Name and will found required result
Search any value Like computer in whole database in which column and in which tables value computer exists
For it first we need to write a store procedure then we reuse it for our search i got it from http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm very perfect result.
after executing store procedure we got required result as in given below image.
Image showing complete search result of keyword computer from whole database.
Above was concept to solve it.Exact Query fullfilling above requirment is below
Select tax_id from (select table_name from information_schema.columns
where column_name = 'tax_id') as temp