• 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

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

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

    - 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