Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

dynamic column name

Is it possible to build a SQL query in which the name of a column would be dynamic?

I need to create statistics about the content of tables.

Hereafter is an extract of the request. For each column of MYTABLE, I want to know how many rows have a NULL value.

select  co.TABNAME,  co.COLNAME, (select count(*) from SQLDBA.MYTABLE where co.COLNAME is NULL)
from SYSCAT.COLUMNS co
where co.TABNAME = 'MYTABLE'
order by co.COLNO

Thanks

Fred
0
fho
Asked:
fho
1 Solution
 
BigSchmuhCommented:
I think you can do it 3 ways:
1/ Use the standard statistics
as they perfectly answer your how many nulls question

2/ Use a SQL query which returns a SQL query
Example:
  SELECT Concat('SELECT Count(*) FROM ',Concat(co.TABNAME,';'))
  FROM SYSCAT.COLUMNS co
put the results in a batch and run it logging to a txt file (you'll found it easier than everything else)

3/ Use C and APIs
Perfect results guaranteed but it's a lot of work

hth
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now