dongtangu8403 2016-11-17 23:33
浏览 29

查询在PHP中查找MS SQL Server中未使用的列

I'm working on a bad large database with a lot of unused fields and columns where every data is NULL. Is there any way to retrieve only the populated columns of a table with a query? I have no access to SQL Server Studio but via PHP

  • 写回答

1条回答 默认 最新

  • douhuantui6259 2016-11-18 00:07
    关注

    Take a peek at Select * From INFORMATION_SCHEMA.COLUMNS

    From that, you can create some dynamic SQL to list table names and field names where all values are null

    Declare @SQL varchar(max) = '>>>'
    Select @SQL=@SQL+'Union All Select TableName=''['+Table_Schema+'].['+Table_Name+']'',FieldName='''+Column_Name+''' From ['+Table_Schema+'].['+Table_Name+'] having max(['+Column_Name+']) is null'+char(13)
      From INFORMATION_SCHEMA.COLUMNS
    
    Set @SQL = Replace(@SQL,'>>>Union All','')
    Exec(@SQL)
    

    I should add @scsimon has valid concerns/questions

    I didn't have a table where an entire column was null, so I created dbo.temp with 3 records and 3 fields. Only field aaa was completely null.

    The above dynamic sql returned

    TableName       FieldName
    [dbo].[temp]    aaa
    

    One Line

    Declare @SQL varchar(max) = '>>>';Select @SQL=@SQL+'Union All Select TableName=''['+Table_Schema+'].['+Table_Name+']'',FieldName='''+Column_Name+''' From ['+Table_Schema+'].['+Table_Name+'] having max(['+Column_Name+']) is null'+char(13) From INFORMATION_SCHEMA.COLUMNS;Set @SQL = Replace(@SQL,'>>>Union All','');Exec(@SQL);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据