CREATE VIEW in SQL Server using UNION ALL -
please consider below example:
create view vw_yearlysales select 2011 yearno, productid, sum(amount) invoicetable2011 union select 2012 yearno, productid, sum(amount) invoicetable2012 union select 2013 yearno, productid, sum(amount) invoicetable2013 go
the invoicetable2013
doesn't exist , don't want create right now, created automatically when recording first invoice year 2013.
can me on how specify condition verify existence of table before doing union all
?
many help.
as others have correctly said, can't achieve view, because select statement has reference concrete set of tables - , if of them don't exist, query fail execute.
it seems me problem more fundamental. there should conceptually 1 invoicetable
, rows different dates. separating out different logical tables year presumably that's been done optimisation (unless columns different, doubt).
in case, partitioning seems way remedy problem (partitioning large tables year/quarter/month canonical example). let have single invoicetable
logically, yet specify sql server should store data behind scenes if different tables split out year. best of both worlds - accurate model, , fast performance - , makes view definition simple.
Comments
Post a Comment