--If Exists Union views and update the table
IF EXISTS(UPDATE Table_1
SET ColDate = X.ColDate, ColB = X.ColB, ColC = X.ColC FROM
SELECT ColDate, ColB, SUM(ColC) AS ColC
GROUP BY ColDate, ColB FROM View_1
UNION ALL
SELECT ColDate, ColB, SUM(ColC) AS ColC
GROUP BY ColDate, ColB FROM View_2
)X
ELSE--Union views and insert into the table
INSERT INTO Table_1(ColDate, ColB, ColC)
SELECT ColDate, ColB, ColC FROM
(
SELECT ColDate, ColB, SUM(ColC) AS ColC
GROUP BY ColDate, ColB FROM View_1
UNION ALL
SELECT ColDate, ColB, SUM(ColC) AS ColC
GROUP BY ColDate, ColB FROM View_2
)X
WHERE ColDate = '1/25/2008'
ORDER BY ColDate, ColB, ColC
|