6

保留为空白/空/空格或零的列(给定表列表)。

假设我可以从中央系统表中提取表和列定义,我应该如何检查上述情况?

4

1 回答 1

9

SQL Server 内部没有特殊的过程来进行此确定。您需要查询每个表。这是一个蛮力解决方案:

If object_id('tempdb..#Results') is not null
    Drop Table #Results;
GO
Create Table #Results
    (
    TableSchema sysname not null
    , TableName sysname not null
    , ColumnName sysname not null
    );
GO

Declare @TableSchema sysname;
Declare @TableName sysname;
Declare @ColumnName sysname;
Declare @DataType sysname;
Declare @Columns Cursor;
Declare @BaseSql nvarchar(max);
Declare @Sql nvarchar(max);
Declare @AdditionalFilter nvarchar(max);

Set @BaseSql = 'Insert #Results( TableSchema, TableName, ColumnName )
                Select ''TABLE_SCHEMA'', ''TABLE_NAME'', ''COLUMN_NAME''
                From ( Select 1 As V ) As Z
                Where Not Exists    (
                                    Select 1
                                    From [TABLE_SCHEMA].[TABLE_NAME]
                                    Where [COLUMN_NAME] Is Not Null
                                        ADDITIONAL_FILTER
                                    )';

Set @Columns = Cursor Fast_Forward For
    Select C.TABLE_SCHEMA, C.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE
    From INFORMATION_SCHEMA.COLUMNS As C
        Left Join INFORMATION_SCHEMA.VIEWS As V
            On V.TABLE_SCHEMA = C.TABLE_SCHEMA
                And V.TABLE_NAME = C.TABLE_NAME
    Where V.TABLE_NAME Is Null;

Open @Columns;
Fetch Next From @Columns Into @TableSchema, @TableName, @ColumnName, @DataType;

While @@Fetch_Status = 0
Begin
    If @DataType In('int','smallint','tinyint','bigint','numeric','bit','decimal','money','smallmoney','float','real')
        Set @AdditionalFilter = 'And [COLUMN_NAME] <> 0';
    Else If @DataType In('char','nchar','varchar','nvarchar','text','ntext')
        Set @AdditionalFilter = 'And Len([COLUMN_NAME]) > 0';
    Else
        Set @AdditionalFilter = '';

    Set @Sql = Replace(@BaseSql, 'ADDITIONAL_FILTER', @AdditionalFilter);
    Set @Sql = Replace(@Sql, 'TABLE_SCHEMA', @TableSchema);
    Set @Sql = Replace(@Sql, 'TABLE_NAME', @TableName);
    Set @Sql = Replace(@Sql, 'COLUMN_NAME', @ColumnName);

    --Print @Sql
    Exec(@Sql)
    Fetch Next From @Columns Into @TableSchema, @TableName, @ColumnName, @DataType;
End

Close @Columns;
Deallocate @Columns;

Select *
From #Results

上述解决方案中的一个怪癖是,将返回表中为空的任何列。如果要排除空表,只需将查询调整为以下内容:

Set @BaseSql = 'Insert #Results( TableSchema, TableName, ColumnName )
                Select ''TABLE_SCHEMA'', ''TABLE_NAME'', ''COLUMN_NAME''
                From ( Select 1 As V ) As Z
                Where Exists    (
                                Select 1
                                From [TABLE_SCHEMA].[TABLE_NAME]
                                )
                And Not Exists  (
                                Select 1
                                From [TABLE_SCHEMA].[TABLE_NAME]
                                Where [COLUMN_NAME] Is Not Null
                                    ADDITIONAL_FILTER
                                )';
于 2013-01-31T19:47:49.750 回答