Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if column exists

This is similar to the code I use:

SELECT id, col1 FROM table1
UNION ALL
SELECT id, col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3
...

For example col2 in table2 does not exist, how to make this query work and not to query for columns that do not exist?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using NULL or any other value and alias it to match column list defintion:

SELECT id, col1 FROM table1
UNION ALL
SELECT id, NULL AS col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading