When you say combine, I assume you want a UNION
, you could create a new table with both sets:
SELECT classid
, dbh
, zone
, inspect
INTO Trees
FROM Tree2012
UNION
SELECT classid
, CAST(dbh AS DECIMAL(15,6))
, CAST(zone AS NVARCHAR(254))
, CAST(inspect AS DATETIME)
FROM Tree2009
That UNION
should be ALL
if you want to just combine both sets (disregarding duplicates) just can't post it that way due to firewall issue, UNION
will check for and remove duplicate lines between the two sets and is therefore slower.
You could also use INSERT
if you just wanted to keep Tree2012:
INSERT INTO Tree2012
SELECT classid
, CAST(dbh AS DECIMAL(15,6))
, CAST(zone AS NVARCHAR(254))
, CAST(inspect AS DATETIME)
FROM Tree2009
You don't need to CAST()
every data type, ie zone in your example going from shorter nvarchar to longer nvarchar.
If you have overlapping classID's and the values for the same classID are different in each table then you might be wanting to JOIN
and have multiple columns for each year:
SELECT COALESCE(a.Classid,b.Classid)'ClassID'
, a.dbh 'dbh-2012'
, a.zone 'zone-2012'
, a.inspect 'inspect-2012'
, b.dbh 'dbh-2009'
, b.zone 'zone-2009'
, b.inspect 'inspect-2009'
INTO NewTable
FROM Tree2012 a
FULL JOIN Tree2009 b
ON a.Classid = b.Classid