By doing this I have made sure I don’t have a bogus WHERE clause on my UPDATE statement that is going to incorrectly identify rows to be updated in my table.
In my examples above I only updated a single column with my UPDATE statement.
I then used the New Toy Price column values for Toy Name and Price to update my Toy table column values on rows that have matching column ID values.
We have already seen how to limit the rows being updated by using the WHERE clause.
By doing this I can see what my WHERE condition will return to make sure it identifies the same rows I want to update.
Once my SELECT statement does return the correct rows I can then copy the WHERE clause from my SELECT statement, and paste it into my UDPATE statement code.
If you are an application programmer then you will more than likely need to write TSQL code to update your SQL Server database tables.
In order to update a row in a SQL Server table you will use the UDPATE statement.
In my first TSQL script above, when I create the TOY table, there is typo in the first Toy Name.
I created a Toy Name of “Magic Wnd” when it should have been “Magic Wand”.
There may be times when you don’t want to manually write a bunch of UPDATE statements with different literal strings to update your table.
Suppose I wanted to change all the prices of my Toys with a single UPDATE statement.
Below is the TSQL code to create and populate this table.