Reputation: 2019
I am currently working with a data set that relates to the current year. Data that relates to each year is housed in a separate table. When the data is queried, it is done so using a UNION ALL query.
Unfortunately, the data sets provided in the past do not share the same schema as that for the current year, some fields have been added, some retired, and others have been renamed. I have no control over this.
In this case, how am I to do UNION ALL queries across these tables when the schema are different? The differences are not very significant, but they deviate enough to cause problems.
Any suggestions?
Do I merge everything into one large table including all fields spanning across all years and then add new ones as they appear? Or, do I continue to keep these tables separate?
Upvotes: 1
Views: 4033
Reputation:
Well, for one, don't try to UNION (actually UNION ALL would probably be more appropriate) with SELECT *.
You can:
For example:
DECLARE @a TABLE(d DATE, c INT, x FLOAT);
DECLARE @b TABLE(d DATETIME, c VARCHAR(32));
DECLARE @c TABLE(d DATE, x INT, y INT);
SELECT d, c = CONVERT(VARCHAR(32), c), x = CONVERT(INT, x) FROM @a
UNION ALL
SELECT CONVERT(DATE, d), c, x = NULL FROM @b
UNION ALL
SELECT d, c = 'not supplied', x FROM @c;
Upvotes: 2