--Generate SQL Counts for all columns in a table using SQL meta data
begin try
with cteFx (rowcountFx, fxpercentage)
as
(select @@ROWCOUNT as rowcountFx, concat('select ''',table_name,''' as [table_names], ''',column_name,''' as [column_names],','(select count(*) from (select ts.[',information_schema.columns.column_name,'] from [',information_schema.columns.table_name,'] as ts where ts.[',information_schema.columns.column_name,'] is not null) as column_counts) column_counts,
(select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts) table_counts,',
'iif((select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts)=0,0,',
'(cast((select count(*) from (select ts.[',information_schema.columns.column_name,'] from [',information_schema.columns.table_name,'] as ts where ts.[',information_schema.columns.column_name,'] is not null) as column_counts) as numeric)/
cast((select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts) as numeric))) as fxpercentage
union all')
from INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME like 'Enter Table Name Here')
select rowcountFx, fxpercentage
from cteFx;
end try
begin catch
print error_message();
end catch;
begin try
with cteFx (rowcountFx, fxpercentage)
as
(select @@ROWCOUNT as rowcountFx, concat('select ''',table_name,''' as [table_names], ''',column_name,''' as [column_names],','(select count(*) from (select ts.[',information_schema.columns.column_name,'] from [',information_schema.columns.table_name,'] as ts where ts.[',information_schema.columns.column_name,'] is not null) as column_counts) column_counts,
(select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts) table_counts,',
'iif((select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts)=0,0,',
'(cast((select count(*) from (select ts.[',information_schema.columns.column_name,'] from [',information_schema.columns.table_name,'] as ts where ts.[',information_schema.columns.column_name,'] is not null) as column_counts) as numeric)/
cast((select count(*) from (select * from [',information_schema.columns.table_name,']) as table_counts) as numeric))) as fxpercentage
union all')
from INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME like 'Enter Table Name Here')
select rowcountFx, fxpercentage
from cteFx;
end try
begin catch
print error_message();
end catch;
No comments:
Post a Comment