I am having an issue going through column by column and getting a count of all the non-null values in each column. I think issue is how I am getting the column names. In the last select statement where I am trying to select all of the values in each column, I am just getting the column name as many times as there are rows. Any idea on how to fix this?
DECLARE @cnt INT = 1; WHILE @cnt < (SELECT COUNT(*) FROM mySchema.INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tblName') + 1 BEGIN DECLARE @column varchar(9) = (select column_name FROM mySchema.INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tblName' AND ordinal_position = @cnt); SELECT @column FROM [mySchema].[dbo].[tblName] WHERE @column is not null AND @column <> '' SET @cnt = @cnt + 1; END;