Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql server search column name in all tables

SELECT      COLUMN_NAME AS 'ColumnName'
            ,TABLE_NAME AS  'TableName'
FROM        INFORMATION_SCHEMA.COLUMNS
WHERE       COLUMN_NAME LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
Comment

find table from column name in sql

SELECT      c.name  AS 'ColumnName'
            ,t.name AS 'TableName'
FROM        sys.columns c
JOIN        sys.tables  t   ON c.object_id = t.object_id
WHERE       c.name LIKE '%MyName%'
ORDER BY    TableName
            ,ColumnName;
Comment

search column name sql

SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%wild%';
Comment

find a column by name in a sql server table

Get Table by Column Name
Comment

How to search for a String in all Columns in all tables in SQL Server Database

USE YourDBName
GO

Declare @SearchString VARCHAR(100)
--Provide the String here. I am using Aamir for search
SET @SearchString='aamir'
DECLARE @DatabaseName VARCHAR(100)
DECLARE @SchemaName VARCHAR(100)
DECLARE @TableName VARCHAR(100)
DECLARE @ColumnName VARCHAR(100)
DECLARE @FullyQualifiedTableName VARCHAR(500)
Declare @DataType VARCHAR(50)

--Create Temp Table to Save Results
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results

CREATE TABLE #Results (
    DatabaseName VARCHAR(100)
    ,SchemaName VARCHAR(100)
    ,TableName VARCHAR(100)
    ,ColumnName VARCHAR(100)
    ,ColumnDataType VARCHAR(50)
    ,TotalTableRowCount INT
    ,StringOccuranceRecordCount INT
    )

DECLARE Cur CURSOR
FOR
SELECT C.Table_CataLog
    ,C.Table_Schema
    ,C.Table_Name
    ,C.Column_Name
    ,'[' + C.Table_CataLog + ']' + '.[' + C.Table_Schema + '].' 
    + '[' + C.Table_Name + ']' AS FullQualifiedTableName,
    C.Data_Type
FROM information_schema.Columns C
INNER JOIN information_Schema.Tables T ON C.Table_Name = T.Table_Name
    AND T.Table_Type = 'BASE TABLE'
    and (C.Data_Type like '%CHAR%'
    or C.Data_Type like '%Text%')


OPEN Cur
FETCH NEXT
FROM Cur
INTO @DatabaseName
    ,@SchemaName
    ,@TableName
    ,@ColumnName
    ,@FullyQualifiedTableName
    ,@DataType

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @SQL VARCHAR(MAX) = NULL
    SET @SQL = ' Select ''' + @DatabaseName + ''' AS DatabaseName, ''' 
    + @SchemaName + ''' AS TableName,
      ''' + @TableName + ''' AS SchemaName,
      ''' + @ColumnName + ''' AS ColumnName,
      ''' + @DataType + ''' AS ColumnName,
      (Select count(*) from ' + @FullyQualifiedTableName + ' With (Nolock)) 
      AS TotalTableRowCount,
      count(*) as StringOccuranceRecordCount from ' + @FullyQualifiedTableName 
      + 'With (Nolock) Where  '+@ColumnName+' like '''+'%'+ @SearchString+'%'''

   -- Print @SQL
    INSERT INTO #Results
    EXEC (@SQL)

    FETCH NEXT
    FROM Cur
    INTO @DatabaseName
        ,@SchemaName
        ,@TableName
        ,@ColumnName
        ,@FullyQualifiedTableName
        ,@DataType
END
CLOSE Cur
DEALLOCATE Cur

SELECT *,
Cast((StringOccuranceRecordCount/Cast(TotalTableRowCount as Numeric(13,1)))*100
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql reset auto increment value 
Sql :: delete mysql ubuntu 20.04 
Sql :: mysql last 7 days including today 
Sql :: alter session set nls_date_format 
Sql :: postgresql reset sequence 
Sql :: ADD COLOUNS CREATED AND UPDATED AT IN MYSQL 
Sql :: select all fields in soql 
Sql :: alter table add column boolean 
Sql :: sql ignore accents 
Sql :: sql change column types 
Sql :: oracle create synonym 
Sql :: sql list all procedures 
Sql :: mysql list databases 
Sql :: this month mysql where 
Sql :: mysql unix timestamp to date 
Sql :: mac xampp mysql not starting 
Sql :: string to date postgres 
Sql :: mysql users and privileges list 
Sql :: how to check username in mysql command line 
Sql :: wordpress sql find and replace 
Sql :: find names starting with vowels in sql 
Sql :: oracle sql log to console 
Sql :: get hour from date sql 
Sql :: cast to date bigquery 
Sql :: postgresql select all column names 
Sql :: MySql get fields of table 
Sql :: create table if not exists sql 
Sql :: django sqlite database 
Sql :: error code 1175 mysql fix 
Sql :: sql get tables from current database you working with 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =