"UPDATE P SET Stock = T.Stock FROM prices P INNER JOIN #prices " +
"T ON P.Avg = T.Avg AND P.Colour = T.Colour;" +
" " +
"INSERT INTO prices(Avg, Colour, Stock) SELECT T.Avg, T.Colour, T.Stock FROM #prices T " +
"WHERE NOT EXISTS (SELECT 1 FROM prices P WHERE P.Avg = T.Avg AND P.Colour = T.Colour);";
I'm using c# to import excel data to sql server, if the record exists update else insert new record with Stock field as the primary key. My code inserts new records without any problems, If I reupload without any changes in the excel file the database also doesn't change, BUT if I change the Avg or Colour field in the excel then a new record is inserted and not updated, can someone please help?
From the excel file I insert data into a temp table (#prices) which inserts to sql server. Please note I'm using sql server 2005.