[英]Query for looking for unused columns in MS SQL Server in PHP
I'm working on a bad large database with a lot of unused fields and columns where every data is NULL. 我正在处理一个坏的大型数据库,其中包含许多未使用的字段和列,其中每个数据均为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
我无法通过PHP访问SQL Server Studio
Take a peek at Select * From INFORMATION_SCHEMA.COLUMNS
看看
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 由此,您可以创建一些动态SQL来列出表名和字段名,其中所有值均为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 我应该添加@scsimon有有效的担忧/问题
I didn't have a table where an entire column was null, so I created dbo.temp with 3 records and 3 fields. 我没有一个表,其中整个列为空,所以我创建了具有3条记录和3个字段的dbo.temp。 Only field aaa was completely null.
仅字段aaa完全为空。
The above dynamic sql returned 上面的动态sql返回
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.