3

在我的数据库中,我创建了一些名称中间包含“_”字符的表,我想找到所有这些表。
例如:

Doc_Amit,Doc_Raj,chem_Man etc

我知道查找表名的查询如下:

select * from sys.tables where name like '%%'

但我的问题是我应该在 % 和 % 之间写什么,这样我才能得到包含通配符“_”的表名?

注意:我使用的是 SQL Server 2012

4

2 回答 2

5

试试这个——

SELECT s.name + '.' + o.name
FROM sys.objects o
JOIN sys.schemas s ON o.[schema_id] = s.[schema_id]
WHERE o.name LIKE  '%\_%' ESCAPE '\'
    AND o.[type] = 'U'

回复评论——

    SELECT * 
    FROM sys.objects o

    -> 

ALTER VIEW sys.objects AS
    SELECT *
    FROM sys.objects$

    -----------------------------

    SELECT * 
    FROM sys.tables t

    ->

ALTER VIEW sys.tables AS
    SELECT *
    FROM sys.objects$ o
    LEFT JOIN sys.sysidxstats lob ON lob.id = o.object_id AND lob.indid <= 1
    LEFT JOIN sys.syssingleobjrefs ds ON ds.depid = o.object_id AND ds.class = 8 AND ds.depsubid <= 1   -- SRC_INDEXTOLOBDS 
    LEFT JOIN sys.syssingleobjrefs rfs ON rfs.depid = o.object_id AND rfs.class = 42 AND rfs.depsubid = 0   -- SRC_OBJTOFSDS
    LEFT JOIN sys.syspalvalues ts ON ts.class = 'LEOP' AND ts.value = o.lock_escalation_option
    WHERE o.type = 'U'
于 2013-04-15T12:18:07.147 回答
1

我得到了最简单的答案,而且更简单!

select * from sys.objects where name like '%[_]%'
于 2013-04-15T12:25:55.773 回答