Kumar,
Qaisar's query has hard-coded part that does not change across differnet tables. It generates one sql for each table in DBC.Tables (instead of creating the Sqls manually). You can then run the Sql generated in result to get the final output you want.
I have modified the sql to better suit your need, since you wanted all tables which have column nmaed SRC_CD
SELECT 'SELECT ' || TRIM(TABLENAME) || ' AS TABLE_NM' || '
,CASE WHEN SUM(CASE when SRC_CD=''S1'' THEN 1 ELSE 0 END) >=1 THEN ''Yes'' else ''No'' end AS S1' || '
,CASE when SUM(CASE when SRC_CD=''S2'' THEN 1 ELSE 0 END) >=1 THEN ''Yes'' ELSE ''No'' END AS S2' || '
,CASE when SUM(CASE when SRC_CD=''S3'' THEN 1 ELSE 0 END) >=1 THEN ''Yes'' ELSE ''No'' END AS S3' || '
,CASE when SUM(CASE when SRC_CD=''S4'' THEN 1 ELSE 0 END) >=1 THEN ''Yes'' ELSE ''No'' END AS S4' || '
,CASE when SUM(CASE when SRC_CD NOT IN (''S1'',''S2'',''S3'',''S4'') THEN 1 ELSE 0 END) >=1 THEN ''Yes'' ELSE ''No'' END AS UNK' || '
FROM ' || TRIM(DATABASENAME) || '.' || TRIM(TABLENAME) || ';'
FROM DBC.columns WHERE columnname like 'SRC_CD%' and DATABASENAME = <<databasename>>
↧