Set Nocount On
Declare @Query Varchar(1000)
Declare @Status Table(Available Varchar(100))
Select @Query = 'Select ' + @Column + ' from ' + @Table + ' where ' + @Column + ' like ''%' + @Search + '%'''
Insert @Status
Exec(@Query)
Select @Available = Available from @Status
Go
/*Creating stored procedure to identify/fetch the matching Columns & Tables in current Database*/
If Object_Id('Usp_FindString') Is Not Null
Drop Proc Usp_FindString
Create Proc Usp_FindString
(
@Database Varchar(100) = NULL,
@TableName Varchar(100) = NULL,
@ColumnName Varchar(100) = NULL,
@SearchString Varchar(100)
)As
Begin
Set Nocount On
Create Table #Table_Column(TableName Varchar(100),ColumnName Varchar(100))
Create Table #Columns(TableSlno Int,ColumnSlno Int, TableName Varchar(100),ColumnName Varchar(100))
Create Table #Status(DatabaseName Varchar(100), TableName Varchar(100), ColumnName Varchar(100), MatchingValue Varchar(4000))
Insert #Table_Column
Select distinct Object_name(c.id) [Table], c.name [Column] from sys.syscolumns c join sys.types t on (c.xtype = t.system_type_id)
where t.collation_name Is Not null
and c.id >100
and Objectproperty(c.id,'IsUserTable') = 1
And Object_name(c.id) = ISNULL(@TableName, Object_name(c.id))
And c.name = ISNULL(@ColumnName, c.name)
Order by 1
Declare @Tables Int, @Seq Int, @Columns Varchar(4000), @Table Varchar(4000), @Script Varchar(MAX),@IsAvailable Varchar(100), @DatabaseName Varchar(100), @ColSeq Int, @ColTotal Int
;With CTEs
As
(
Select distinct o.TableName,o.ColumnName from #Table_Column o
)
Insert #Columns
Select Dense_Rank() Over(order by TableName),Dense_Rank() Over(Partition by TableName Order by ColumnName),TableName,ColumnName from CTEs
Select @Tables = Max(TableSlno) From #Columns
Select @Seq =1, @Columns ='', @Script ='', @DatabaseName = DB_Name(), @ColSeq = 1
While (@Seq <=@Tables)
Begin
Select @ColTotal = Max(ColumnSlno) From #Columns where TableSlno = @Seq
Select @ColSeq = 1
While(@ColSeq <= @ColTotal)
Begin
select @Table = TableName, @Columns = ColumnName from #Columns where TableSlno = @Seq And ColumnSlno = @ColSeq
EXEC Usp_Isavailable @Table,@Columns,@SearchString,@IsAvailable Output
If (@IsAvailable Is Not null AND @IsAvailable <>'')
Insert #Status Values(@DatabaseName,@Table,@Columns,@IsAvailable)
Select @IsAvailable = NULL
Select @ColSeq = @ColSeq + 1
End
Select @Seq = @Seq + 1
End
Select * from #Status
Drop Table #Table_Column
Drop Table #Columns
Drop Table #Status
End
/*1. Searching a specific data("SQL") in all the tables & columns in current database*/
Exec Usp_FindString @SearchString = 'SQL'
Go
/*2. Searching a specific data("SQL") in all the tables in specific columns("Column1") in current database*/
Exec Usp_FindString @ColumnName ='Column1', @SearchString = 'SQL'
Go
/*3. Searching a specific data("SQL") in specific table("Table1") in specific columns("Column1") in current database*/
Exec Usp_FindString @TableName='Table1',@SearchString = 'SQL'
Go
/*4. Searching a specific data("SQL") in specific table("Table1") in specific columns("Column1") in current database*/
Exec Usp_FindString @TableName='Table1', @Columnname ='Column1', @SearchString = 'SQL'