【发布时间】:2011-08-15 16:16:35
【问题描述】:
我有一个包含 120 多列的表,需要确定使用最少的列。我尝试使用 sql 查询来执行此操作,但发现 T-SQL 更简单一些。
我尝试了以下方法,但每列的计数都为 0。
Declare data1 Cursor for select column_name
from information_schema.columns
where table_name = 'repository'
Declare @mField nvarchar(255)
Declare @count int
Open data1;
fetch next from data1 into @mField;
set @count = -1;
while @@fetch_status = 0
begin
select @count = count(@mField)
from repository where tablereference =
'central' and ( @mField!= null )
print @mField+' ' ;
print @count;
Fetch next from data1 into @mField;
end
close data1;
deallocate data1;
【问题讨论】:
-
定义“最少使用”。此外,您的查询逻辑被破坏,您无法将任何内容与 NULL (@mField != null) 进行比较。做到这一点(@mField 不为空)