加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

sql – 如何查找哪些列没有任何数据(所有值都为NULL)?

发布时间:2021-01-18 01:58:29 所属栏目:编程 来源:网络整理
导读:我在数据库中有几个表.我想找到哪些列(在哪些表中)没有任何值(列中的全部NULL).我在下面的例子中,功效应该是 TestTable1 -- Var2TestTable2 -- Variable1 我不知道怎样建设这种查询.很是感激您的辅佐! --create first tablecreate table dbo.TestTable1 (sur

我在数据库中有几个表.我想找到哪些列(在哪些表中)没有任何值(列中的全部NULL).我在下面的例子中,功效应该是

TestTable1 --> Var2
TestTable2 --> Variable1

我不知道怎样建设这种查询.很是感激您的辅佐!

--create first table
create table dbo.TestTable1 (
sur_id int identity(1,1) not null primary key,var1 int null,var2 int null
)
go

--insert some values
insert into dbo.TestTable1 (var1) 
    select 1 union all select 2 union all select 3

--create second table
create table dbo.TestTable2 (
sur_id int identity(1,variable1 int null,variable2 int null
)

--and insert some values
insert into dbo.TestTable2 (variable2) 
    select 1 union all select 2 union all select 3

办理要领

对付单个列,count(ColumnName)返回ColumName不为null的行数:
select  count(TheColumn)
from    YourTable

您可觉得全部列天生查询.按照Martin的提议,您可以行使is_nullable = 1解除不能为null的列.譬喻:

select  'count(' + name + ') as ' + name + ','
from    sys.columns
where   object_id = object_id('YourTable')
        and is_nullable = 1

假如表的数目很大,您可以以相同的方法为全部表天生查询.全部表的列表都在sys.tables中.

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读