I'm trying to insert rows into a table from multiple tables but only those rows that do not already exist in the updating table.CREATE TABLE #mytable( MyID INT, LName VARCHAR(30), StateID INT)GOCREATE TABLE #personid( PersonID INT, FiscalYR CHAR(9))GOCREATE TABLE #extpersonid( ExtPersonID INT, FiscalYR CHAR(9))GOCREATE TABLE #personname( NameID INT, LName VARCHAR(30))GOCREATE TABLE #personstateid( PerStateID INT, StateID INT)GOINSERT INTO #mytable (MyID, LName, StateSID)SELECT 401771, 'Andrews', 9278354 UNION ALLSELECT 401882, 'Bonds', 7432833 UNION ALLSELECT 402353, 'Jones', 5647322 UNION ALLSELECT 404705, 'Jackson', 3758990 UNION ALLSELECT 416755, 'Baker', 5485748 UNION ALLSELECT 428473, 'Smith', 8695372 UNION ALLSELECT 437564, 'Johnson', 9584733GOINSERT INTO #personid (PersonID, FiscalYR)SELECT 401882, '2009-2010' UNION ALLSELECT 401882, '2010-2011' UNION ALLSELECT 401994, '2009-2010' UNION ALLSELECT 401994, '2010-2011' UNION ALLSELECT 404705, '2009-2010' UNION ALLSELECT 404705, '2010-2011' UNION ALLSELECT 405692, '2009-2010' UNION ALLSELECT 405692, '2010-2011'GOINSERT INTO #extpersonid (ExtPersonID, FiscalYR)SELECT 400972, '2009-2010' UNION ALLSELECT 400972, '2010-2011' UNION ALLSELECT 401994, '2009-2010' UNION ALLSELECT 401994, '2010-2011' UNION ALLSELECT 402353, '2009-2010' UNION ALLSELECT 402353, '2010-2011' UNION ALLSELECT 404705, '2009-2010' UNION ALLSELECT 404705, '2010-2011' UNION ALLSELECT 416755, '2009-2010' UNION ALLSELECT 416755, '2010-2011'GOINSERT INTO #personname (NameID, LName)SELECT 400972, 'Smith' UNION ALLSELECT 401771, 'Andrews' UNION ALLSELECT 401882, 'Bonds' UNION ALLSELECT 401994, 'James' UNION ALLSELECT 402353, 'Jones' UNION ALLSELECT 404705, 'Jackson' UNION ALLSELECT 405692, 'Lawton' UNION ALLSELECT 416755, 'Baker' UNION ALLSELECT 428473, 'Smith' UNION ALLSELECT 437564, 'Johnson'GOINSERT INTO #personstateid (PerStateID, StateID)SELECT 400972, 9875842 UNION ALLSELECT 401771, 9278354 UNION ALLSELECT 401882, 7432833 UNION ALLSELECT 401994, 2589875 UNION ALLSELECT 402353, 5647322 UNION ALLSELECT 404705, 3758990 UNION ALLSELECT 405692, 6598241 UNION ALLSELECT 416755, 5485748 UNION ALLSELECT 428473, 8695372 UNION ALLSELECT 437564, 9584733GO
I want to insert those rows that are NOT in #mytable but are in either #personid and/or #extpersonid for FiscalYR = ‘2010-2011’ (NOTE: the same ID can be in BOTH #personid & #extpersonid for the same fiscal year). When exist join to #personname to retrieve LName and join to #personstateid to retrieve StateID.So my desired results should be:400972 Smith 9875842401994 James 2589875405692 Lawton 6598241416755 Baker 5485748Thanks.