Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

syscolumns Expand / Collapse
Author
Message
Posted Tuesday, January 06, 2009 1:09 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 10, 2013 3:11 AM
Points: 9, Visits: 24
Folks,

Say I have a table with columns, colA, colB, colC and colD and there are 20 rows in my table. I want to write a sort of dynamic script to show me a count of non-null columns.

The result set would look something like

colA 5
colB 20
colC 10
colD 1

This example would show that each row had colb populated in each of the 20 rows.

I know I could write a select that would generate some individual sql statements using syscolumns.name and syscolumns.id and I could then execute each of those statements but is there a way to do this more neatly ?
Post #630941
Posted Tuesday, January 06, 2009 1:22 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:27 AM
Points: 6,997, Visits: 8,411
Better would be to use the information schema views.

Keep in mind that sql2005 only show object definitions you are granted to view.

(view definition granted)

Is this what you're looking for ?

SELECT CASE t1.[ORDINAL_POSITION] WHEN 1 THEN 'SELECT ' ELSE ' , ' end 
+ ' count( distinct ['+ t1.[COLUMN_NAME] + '] ) as [Count_distinct_' + t1.[COLUMN_NAME] + '_'
+ CASE t1.[ORDINAL_POSITION] WHEN t2.[MAX_ORDINAL_POSITION] THEN ' from [' + t1.[TABLE_SCHEMA] + '].[' + t1.[TABLE_NAME] + '] ;' ELSE ' ' end
FROM [INFORMATION_SCHEMA].[COLUMNS] t1
INNER JOIN
( SELECT [TABLE_NAME], [TABLE_SCHEMA], MAX([ORDINAL_POSITION]) AS MAX_ORDINAL_POSITION
FROM [INFORMATION_SCHEMA].[COLUMNS]
GROUP BY [TABLE_NAME], [TABLE_SCHEMA]
)t2
ON t1.[TABLE_NAME] = t2.[TABLE_NAME]
AND t1.[TABLE_SCHEMA] = t2.[TABLE_SCHEMA]

WHERE t1.[TABLE_NAME] = 'mytable'
ORDER BY t1.[TABLE_NAME], t1.[TABLE_SCHEMA], t1.[ORDINAL_POSITION]




Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #630957
Posted Tuesday, January 06, 2009 1:27 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 10, 2013 3:11 AM
Points: 9, Visits: 24
Sorry ALZDBA - I am only on sqlserver2000 !

Liked the bike on yor profile pic !
Post #630967
Posted Tuesday, January 06, 2009 1:42 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 41,530, Visits: 34,447
Please post SQL 2000-related questions in the SQL 2000 forums. If you post in the 2005 forums, you're very likely to get 2005-specific solutions.

The Information schema exists in 2000 as well, and looks the same as it does in 2005.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #630983
Posted Tuesday, January 06, 2009 1:43 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 10, 2013 3:11 AM
Points: 9, Visits: 24
apologies - just noticed it was the 2005 forum
Post #630984
Posted Tuesday, January 06, 2009 2:53 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 41,530, Visits: 34,447
Continued here - http://www.sqlservercentral.com/Forums/Topic630987-169-1.aspx

No more replies to this thread please.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #631059
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse