如何查找只有空值的列表?

时间:2016-02-04 10:29:59

标签: sql-server

我有一个大型数据库CDS_CQM我想在数据库中找到只有空值的列列表 欢迎任何帮助

2 个答案:

答案 0 :(得分:1)

我认为这可以帮助您找到所有没有数据的列的列表

Create Procedure spGetColumnsWithAllNullValues
( 
    @Database   Varchar (100),
    @Schema     Varchar (100),
    @Table      Varchar (100)
)
As Begin

    Declare @Column Varchar (100)

    Declare @Columns Table
    (
        ColumnName Varchar (100)
    )

    Declare @Results Table
    (
        ColumnName Varchar (100)
    )

    Declare @Temp Table
    (
        Result Bit
    )

    Insert  @Columns
    Select  COLUMN_NAME
    From    INFORMATION_SCHEMA.COLUMNS
    Where   IS_NULLABLE = 'YES'
    And     TABLE_CATALOG = @Database
    And     TABLE_SCHEMA = @Schema
    And     TABLE_NAME = @Table

    Declare cur Cursor For
    Select  ColumnName
    From    @Columns

    Open cur

    While (1 = 1)
    Begin
        Fetch Next From cur Into @Column

        If (@@FETCH_STATUS <> 0) Break

        Declare @sql NVarchar(Max) = N'Select Case When Exists (Select * From ' 
                                    + QuoteName(@Database) + '.' 
                                    + QuoteName(@Schema) + '.' 
                                    + QuoteName(@Table) 
                                    + ' Where ' + QuoteName(@Column) + ' Is Not Null) Then 0 Else 1 End'

        Delete @Temp
        Insert @Temp Execute (@sql)

        Insert  @Results
                (ColumnName)
        Select  @Column
        From    @Temp
        Where   Result = 1
    End

    Close cur        
    Deallocate cur

    Select  ColumnName
    From    @Results
    Order By ColumnName

End

答案 1 :(得分:0)

如果您需要找到NULL constraint的列,请尝试以下

select *from INFORMATION_SCHEMA.columns
where IS_NULLABLE = 'yes'