计算数据库中所有表的名称列中的特殊字符数

时间:2018-05-09 11:28:44

标签: sql sql-server

我想在标题中找到带有'name'的列,并计算其中包含数字或特殊字符的记录数(这使得它们无效)。我想将它应用于我的数据库中的所有表和列。

我的代码是:

SELECT DB_Name() AS DatabaseName
    ,s.[name] AS SchemaName
    ,t.[name] AS TableName
    ,c.[name] AS ColumnName
    ,COUNT(c.name) As InvalidNameCnt
    ,'[' + DB_Name() + ']' + '.[' + s.name + '].' + '[' + T.NAME + ']' AS FullQualifiedTableName
    ,d.[name] AS DataType
FROM sys.schemas s
INNER JOIN sys.tables t ON s.schema_id = t.schema_id
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.types d ON c.user_type_id = d.user_type_id
    WHERE c.NAME LIKE '%name%'
    AND [DBName].[SystemName].[TableName].[ColumnName] NOT LIKE '%[0-9]%';

我的代码目前无法正常工作,因为它会抛出一条消息,说无法找到最后一行的多部分标识符,请问有什么解决方案吗?

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

SELECT DB_Name() AS DatabaseName,
s.[name] AS SchemaName,
t.[name] AS TableName,
c.[name] AS ColumnName,
COUNT(c.name) As InvalidNameCnt,
'[' + DB_Name() + ']' + '.[' + s.name + '].' + '[' + T.NAME + ']' AS 
 FullQualifiedTableName,
d.[name] AS DataType
FROM sys.schemas s
INNER JOIN sys.tables t ON s.schema_id = t.schema_id
INNER JOIN sys.columns c ON t.object_id = c.object_id
INNER JOIN sys.types d ON c.user_type_id = d.user_type_id
WHERE c.NAME LIKE '%name%'
AND c.name NOT LIKE '%[0-9]%'
group by s.[name] ,t.[name],c.[name],d.[name]