Search
 
SCRIPT & CODE EXAMPLE
 

SQL

search all tables in a database for a value

Declare @SearchStr nvarchar(100)

SET  @SearchStr='Search String' BEGIN

CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))

SET NOCOUNT ON

DECLARE @TableName nvarchar(256), @ColumnName nvarchar(128),
 @SearchStr2 nvarchar(110)  SET  @TableName = ''    SET @SearchStr2 =
 QUOTENAME('%' + @SearchStr + '%','''')

WHILE @TableName IS NOT NULL    
BEGIN       
  SET @ColumnName = ''      
  SET @TableName =  (
    SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' +
    QUOTENAME(TABLE_NAME)) FROM INFORMATION_SCHEMA.TABLES 
    WHERE
    TABLE_TYPE = 'BASE TABLE'
    AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
    AND OBJECTPROPERTY(
      OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)),
        'IsMSShipped') = 0)

  WHILE (@TableName IS NOT NULL) AND (@ColumnName IS NOT NULL)      
  BEGIN
    SET @ColumnName = (
      SELECT MIN(QUOTENAME(COLUMN_NAME))
      FROM INFORMATION_SCHEMA.COLUMNS
      WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
        AND TABLE_NAME = PARSENAME(@TableName, 1)
      AND DATA_TYPE IN ('char', 'varchar', 'nchar', 'nvarchar')
      AND QUOTENAME(COLUMN_NAME) > @ColumnName)
      IF @ColumnName IS NOT NULL            
      BEGIN
      INSERT INTO #Results
      EXEC
      (
        'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + 
          ', 3630) FROM ' + @TableName + ' (NOLOCK) ' +
        ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2
      )             
      END       
    END     
  END

  SELECT ColumnName, ColumnValue FROM #Results END
Comment

PREVIOUS NEXT
Code Example
Sql :: sql trigger difference between for and after 
Sql :: Priviledges on table from other schema 
Sql :: leftjoin in sql 
Sql :: sql data type of query 
Sql :: MySQL Age Counter 
Sql :: Un mask mysql 
Sql :: ms sql bacup table 
Sql :: create relationship with betwen two tables in postgersql 
Sql :: creating a simple notify in postgresql 
Sql :: copy data from cell to cell mysql 
Sql :: mysql create database if not exists 
Sql :: mysql conf.d exampel 
Sql :: https://livesql.oracle.com/apex/livesql/s/l8fedwph53gt5gigbacwvu6m0 
Sql :: pl sql join 3 tables 
Sql :: what alter does in db 
Sql :: forenkey code alchemy sql 
Sql :: docmd openargs 
Sql :: oracle_home sqlplus 
Sql :: how do you execute the fragment or sqlBatch using scriptdom 
Sql :: sql insert multiple rows from another table 
Sql :: kimball data warehouse sql calendar 
Sql :: change redo log file size in mysql 5.6 
Sql :: veri seçme SQL 
Sql :: creating directory /var/lib/postgresql/data ... initdb: error: could not create directory "/var/lib/postgres/data": Permission denied 
Sql :: sql server convert string list integers list 
Sql :: sql query use select name inside where clause 
Sql :: Update Query in SQL Server Table - NAYCode.com 
Sql :: mysql table information 
Sql :: psql create usr 
Sql :: cahnge column name apex oracle 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =