0

I am trying to update rows on a SQL Server table using data from an excel sheet. I want to choose the rows of the table on the SQL server to update using the Primary Key of the table (StockCode), but I don't want to drop the rows which I am not updating.

Below are some screenshots of the excel data headers and the SQL table headers.

There are some StockCode's which do not have a box weight. They must remain NULL. The StockCode's which do have box weights must be updated using the excel data.

Here is the excel data with the first row of data:
Excel Headers

Here is the SQL Table with the first row of data:
SQL Table Headers

Thom A
  • 88,727
  • 11
  • 45
  • 75
  • *"I don't want to drop the rows which I am not updating"* An `UPDATE` wouldn't "drop" the rows; a `DELETE` *deletes* rows (removes them), an `UPDATE` *updates* them (changes the values within the row). – Thom A Apr 19 '22 at 08:13
  • Will this all be done on the SQL Server. How are you planning on importing the Excel spreadsheet values? – Peter Smith Apr 19 '22 at 08:15
  • @PeterSmith Yes it will be done on SQL server. I can either use the import wizard, or import the table and make the update. – ALC Corp Apr 19 '22 at 08:37
  • If you have a table to update from (following import) then, as Larnu suggests you can use standard SQL to update the main table. `UPDATE` will only update matched rows and you can do a `NULL` check for box weight. – Peter Smith Apr 19 '22 at 08:43
  • please show data as text and not images. Thank you –  Apr 19 '22 at 08:45
  • @PeterSmith and Larnu, Thanks for the responses. I decided to do what you guys suggested, following the answers on this question: https://stackoverflow.com/questions/224732/sql-update-from-one-table-to-another-based-on-a-id-match – ALC Corp Apr 19 '22 at 12:35

0 Answers0