SQL Server search for a column by name

后端 未结 5 720
小蘑菇
小蘑菇 2020-12-31 02:12

I\'m doing some recon work and having to dig through a few hundred SQL Server database tables to find columns.

Is there a way to easily search for columns in the da

相关标签:
5条回答
  • 2020-12-31 02:19
    SELECT OBJECT_NAME(object_id) FROM sys.columns WHERE name = 'foo'
    

    This includes views though but can be further filtered . It may be useful though.

    More generally...

    SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'foo'
    

    sys.columns

    0 讨论(0)
  • 2020-12-31 02:19

    To get the

    1) full column name
    2) object name (including schema)
    3) object type (table/view)
    4) data type (nice format: varchar(6) or numeric(5,2), etc.)
    5) null/not null
    6) information on identity, check constraint, and default info

    try this:

    DECLARE @Search varchar(200)
    SET @Search='YourColumnName'  --can be a partial or a complete name
    
    SELECT
        s.name as ColumnName
            ,sh.name+'.'+o.name AS ObjectName
            ,o.type_desc AS ObjectType
            ,CASE
                 WHEN t.name IN ('char','varchar') THEN t.name+'('+CASE WHEN s.max_length<0 then 'MAX' ELSE CONVERT(varchar(10),s.max_length) END+')'
                 WHEN t.name IN ('nvarchar','nchar') THEN t.name+'('+CASE WHEN s.max_length<0 then 'MAX' ELSE CONVERT(varchar(10),s.max_length/2) END+')'
                WHEN t.name IN ('numeric') THEN t.name+'('+CONVERT(varchar(10),s.precision)+','+CONVERT(varchar(10),s.scale)+')'
                 ELSE t.name
             END AS DataType
    
            ,CASE
                 WHEN s.is_nullable=1 THEN 'NULL'
                ELSE 'NOT NULL'
            END AS Nullable
            ,CASE
                 WHEN ic.column_id IS NULL THEN ''
                 ELSE ' identity('+ISNULL(CONVERT(varchar(10),ic.seed_value),'')+','+ISNULL(CONVERT(varchar(10),ic.increment_value),'')+')='+ISNULL(CONVERT(varchar(10),ic.last_value),'null')
             END
            +CASE
                 WHEN sc.column_id IS NULL THEN ''
                 ELSE ' computed('+ISNULL(sc.definition,'')+')'
             END
            +CASE
                 WHEN cc.object_id IS NULL THEN ''
                 ELSE ' check('+ISNULL(cc.definition,'')+')'
             END
                AS MiscInfo
        FROM sys.columns                           s
            INNER JOIN sys.types                   t ON s.system_type_id=t.system_type_id and t.is_user_defined=0
            INNER JOIN sys.objects                 o ON s.object_id=o.object_id
            INNER JOIN sys.schemas                sh on o.schema_id=sh.schema_id
            LEFT OUTER JOIN sys.identity_columns  ic ON s.object_id=ic.object_id AND s.column_id=ic.column_id
            LEFT OUTER JOIN sys.computed_columns  sc ON s.object_id=sc.object_id AND s.column_id=sc.column_id
            LEFT OUTER JOIN sys.check_constraints cc ON s.object_id=cc.parent_object_id AND s.column_id=cc.parent_column_id
        WHERE s.name LIKE '%'+@Search+'%'
    
    0 讨论(0)
  • 2020-12-31 02:24

    http://www.red-gate.com/products/SQL_Search/index.htm?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=sqlsearch&gclid=COSfqe_mmKQCFSE1gwodSxOrEQ

    This should do it for you. I use it everyday.

    0 讨论(0)
  • 2020-12-31 02:35
    select c.name as ColumnName, o.name as TableName
    from sys.columns c
    inner join sys.objects o on c.object_id = o.object_id
    where c.name = 'MyColumnName'
    
    0 讨论(0)
  • 2020-12-31 02:40
    select table_name from information_schema.columns
    where column_name = '<your column name here>'
    

    Using the information_schema views is 'more correct' as system details in the system databases are subject to change between implementations of SQL Server.

    0 讨论(0)
提交回复
热议问题