
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 0.86 KB | hits: 10 | expires: Never
Merge data from multiple source tables into a single destination table
INSERT INTO Destination
(Col1, Col2, Col3, Col4, Col5)
SELECT s.Col1,
s.type,
/* The next two columns illustrate choosing a */
/* subset from either Type_A or Type_B */
COALESCE(a.Col3, b.Col3),
COALESCE(a.Col4, b.Col4),
/* The next column illustrates choosing a */
/* static value based on Source.type */
CASE WHEN s.type = 0 THEN 'Static Value 1'
WHEN s.type = 1 THEN 'Static Value 2'
ELSE NULL
END /* CASE */
FROM Source s
LEFT JOIN Type_A a
ON s.Col1 = a.Col1
AND s.type = 0
LEFT JOIN Type_B b
ON s.Col1 = b.Col1
AND s.type = 1