I believe I've asked something like this question in the past. Next week, I think it very well may benefit me (time-wise) to be able to do this.
Basically, in adhoc situations, I'm given one table (well, there are a few pieces, but I'm focusing on one piece - one specific table of that piece) to make use of for our production group, and produce some analysis.
In this process, there are always several columns which are NULL (nearly 100 total columns in table - anywhere from 1/10 to 1/2 could be ALL NULL).
Well, I have to always look at the table, taking the time to see what data is there, and then of that data, what is useful, and what is not. I think that if I can pull a quick snapshot of the table upon learning the table name, and seeing what fields have ALL Null values vs Some/No Null values, it'll be a big help.
To that end, today I put together something that ALMOST gets me there. And this is the point where I want to ask for a little bit of guidance.
Here is what I have (I've changed it a few times b/c I've been working through different ideas):
What I am getting is:[tt]
ColumnID ColumnName Records
1 Name 0
2 Add 0
3 City 0
4 State 0
5 Color 0
6 Car 0
7 Mule 0
[/tt]
Hopefully you can tell the values are totally made up.
So I'm getting the ColumnID and ColumnName, but not the Count. So what I'm guessing is the problem is that perhaps I need to have the entire thing run as a dynamic SQL Variable, rather than part as "normal" SQL, and only part dynamic.
If anybody can give me any insights that'd be most wonderful!
Thanks in advance.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
Basically, in adhoc situations, I'm given one table (well, there are a few pieces, but I'm focusing on one piece - one specific table of that piece) to make use of for our production group, and produce some analysis.
In this process, there are always several columns which are NULL (nearly 100 total columns in table - anywhere from 1/10 to 1/2 could be ALL NULL).
Well, I have to always look at the table, taking the time to see what data is there, and then of that data, what is useful, and what is not. I think that if I can pull a quick snapshot of the table upon learning the table name, and seeing what fields have ALL Null values vs Some/No Null values, it'll be a big help.
To that end, today I put together something that ALMOST gets me there. And this is the point where I want to ask for a little bit of guidance.
Here is what I have (I've changed it a few times b/c I've been working through different ideas):
Code:
DROP TABLE #Fields
CREATE TABLE #Fields(ColumnID int ,Field varchar(150) NOT NULL ,Records bigint NULL)
DECLARE @count INT
DECLARE @colCount INT
DECLARE @Column VARCHAR(150)
DECLARE @RecordCount BIGINT
SET @count = 1
SELECT @colCount = MAX(COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),COLUMN_NAME, 'ColumnID'))
FROM Sandbox.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'MyTableName'
WHILE (@count < @colCount + 1)
BEGIN
EXEC('DECLARE @RecordCount BIGINT SELECT @RecordCount = COUNT(1) FROM MyTableName WHERE [' + @Column + '] IS NOT NULL')
SELECT @RecordCount = COUNT(1) FROM MyTableName WHERE @Column IS NOT NULL
INSERT INTO #Fields
SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),COLUMN_NAME, 'ColumnID') AS COLUMN_ID
,COLUMN_NAME ,@RecordCount
FROM Sandbox.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'MyTableName'
AND COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME),COLUMN_NAME, 'ColumnID') = @count
SET @count = @count + 1
END
SELECT * FROM #Fields
What I am getting is:[tt]
ColumnID ColumnName Records
1 Name 0
2 Add 0
3 City 0
4 State 0
5 Color 0
6 Car 0
7 Mule 0
[/tt]
Hopefully you can tell the values are totally made up.
So I'm getting the ColumnID and ColumnName, but not the Count. So what I'm guessing is the problem is that perhaps I need to have the entire thing run as a dynamic SQL Variable, rather than part as "normal" SQL, and only part dynamic.
If anybody can give me any insights that'd be most wonderful!
Thanks in advance.
"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57