SQL Server按名称搜索列

时间:2010-09-21 15:09:23

标签: sql sql-server sql-server-2005

我正在做一些侦察工作,不得不挖掘几百个SQL Server数据库表来查找列。

有没有办法轻松搜索数据库中的列并只返回该列所属的表名?

我找到了this,但这也返回了包含该列名的存储过程......

6 个答案:

答案 0 :(得分:35)

SELECT OBJECT_NAME(object_id) FROM sys.columns WHERE name = 'foo'

这包括视图,但可以进一步过滤。但它可能很有用。

更一般地......

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

sys.columns

答案 1 :(得分:7)

获取

1)全栏名称
2)对象名称(包括模式)
3)对象类型(表/视图)
4)数据类型(漂亮格式:varchar(6)或数字(5,2)等)
5)null / not null
6)有关身份,检查约束和默认信息的信息

试试这个:

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+'%'

答案 2 :(得分:3)

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'

答案 3 :(得分:1)

答案 4 :(得分:1)

此存储过程将搜索 table.name 和 column.name 对。

当我在代码中有“WhateverId”并且我想知道它(可能)存储在数据库中的位置时使用,而实际上不必通读和理解代码。 :)

CREATE OR ALTER PROC FindColumns  
@ColumnName VARCHAR(MAX) = NULL,  
@TableName VARCHAR(MAX) = NULL    
AS    
    
SELECT T.[name] AS TableName, C.[name] AS ColumnName  
FROM sys.all_columns C    
JOIN sys.tables T ON C.object_id = T.object_id    
JOIN sys.types CT ON C.user_type_id = CT.user_type_id    
WHERE (@ColumnName IS NULL OR C.[name] LIKE '%' + TRIM(@ColumnName) + '%')  
AND (@TableName IS NULL OR T.[name] LIKE '%' + TRIM(@TableName) + '%')  
ORDER BY T.[name], C.[name]

答案 5 :(得分:0)

select table_name from information_schema.columns
where column_name = '<your column name here>'

使用information_schema视图“更正确”,因为系统数据库中的系统详细信息可能会在SQL Server的实现之间发生变化。