如何找到潜在的非空列?

时间:2018-04-02 16:10:11

标签: sql sql-server database

我正在使用SQL Server数据库,该数据库对约束非常轻松,并希望应用一些非空约束。有没有办法扫描数据库中的所有可空列,并选择哪些列不包含任何空值,甚至更好地计算空值的数量?

4 个答案:

答案 0 :(得分:2)

也许有点动态SQL

示例

Declare @SQL varchar(max) = '>>>'

Select @SQL = @SQL 
            + 'Union All Select TableName='''+quotename(Table_Schema)+'.'+quotename(Table_Name)+''''
                             +',ColumnName='''+quotename(Column_Name)+''''
                             +',NullValues=count(*)'
                         +' From '+quotename(Table_Schema)+'.'+quotename(Table_Name)
                         +' Where '+quotename(Column_Name)+' is null '
  From INFORMATION_SCHEMA.COLUMNS
  Where Is_Nullable='YES'

Select @SQL='Select * from (' + replace(@SQL,'>>>Union All ','') + ') A Where NullValues>0'
Exec(@SQL)

返回(例如)

TableName       ColumnName  NullValues
[dbo].[OD-Map]  [Map-Val2]  185
[dbo].[OD-Map]  [Map-Val3]  225
[dbo].[OD-Map]  [Map-Val4]  225

对于计数> = 0

的所有表/列
...
Select @SQL=replace(@SQL,'>>>Union All ','') 
Exec(@SQL)

答案 1 :(得分:1)

是的,这是相当直接的。注意:如果表包含大量记录,我建议使用SELECT TOP 1000 *,而不是SELECT *。

-- Identify records where a specific column is NOT NULL
SELECT * 
FROM TableName
WHERE ColumNName IS NOT NULL

-- Identify the count of records where a specific column contains NULL
SELECT COUNT(1)
FROM TableName
WHERE ColumNName IS NULL

-- Identify all NULLable columns in a database
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE IS_NULLABLE = 'YES'

有关INFORMATION_SCHEMA视图的详细信息,请参阅:https://docs.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql

如果要扫描给定数据库中的所有表和列是否为NULL,则这是一个两步过程。

1。)获取NULLABLE的表和列的列表。

-- Identify all NULLable columns in a database 
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE IS_NULLABLE = 'YES'

2.使用Excel创建SELECT语句以获取每个表/列的NULL计数。为此,请将步骤1中的查询结果复制并粘贴到EXCEL中。假设您已复制标题行,则数据从第2行开始。在单元格E2中,输入以下公式。

="SELECT COUNT(1) FROM "&A2&"."&B2&"."&C2&" WHERE "&D2&" IS NULL"

将其复制并粘贴到整张纸上。这将生成您需要的SQL SELECT语句。将结果复制到E列并粘贴到SQL Server中并运行它。这可能需要一段时间,具体取决于要扫描的表/列的数量。

答案 2 :(得分:1)

检查此查询。这最初由Linda Lawton撰写 原文:https://www.daimto.com/sql-server-finding-columns-with-null-values

在数据库中查找具有空值的列 - 查找空脚本

set nocount on
declare @columnName nvarchar(500)
declare @tableName nvarchar(500)
declare @select nvarchar(500)
declare @sql nvarchar(500)
-- check if the Temp table already exists
if OBJECT_ID('tempdb..#LocalTempTable') is  null
    Begin
        CREATE TABLE #LocalTempTable(
            TableName varchar(150),
            ColumnName varchar(150))
        end
else 
    begin 
        truncate table #LocalTempTable; 
    end
-- Build a select for each of the columns in the database.  That checks for nulls
DECLARE check_cursor CURSOR FOR 
select column_name, table_name, concat(' Select ''',column_name,''',''',table_name,''' from ',table_name,' where [',COLUMN_NAME,'] is null')
from INFORMATION_SCHEMA.COLUMNS

OPEN check_cursor

FETCH NEXT FROM check_cursor 
INTO @columnName, @tableName,@select

WHILE @@FETCH_STATUS = 0
BEGIN
-- Insert it if there if it exists.
set @sql = 'insert into #LocalTempTable (ColumnName, TableName)' + @select
print @sql

-- Run the statment
exec( @sql)

FETCH NEXT FROM check_cursor 
INTO @columnName, @tableName,@select
end
CLOSE check_cursor;
DEALLOCATE check_cursor;

SELECT TableName, ColumnName, COUNT(TableName) 'Count' 
FROM #LocalTempTable 
GROUP BY TableName, ColumnName
ORDER BY TableName

查询结果将是这样的。

enter image description here

答案 3 :(得分:1)

这将告诉您数据库中的哪些列当前为NULLABLE。

USE <Your_DB_Name>
GO

SELECT o.name AS Table_Name
     , c.name AS Column_Name
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
    AND c.is_nullable = 1 /* 1 = NULL, 0 = NOT NULL */
WHERE o.type_desc = 'USER_TABLE'
    AND o.type NOT IN ('PK','F','D') /* NOT Primary, Foreign of Default Key */