May 29, 2003 at 9:47 am
Okay, this T-SQL novice has another question.
What is the best way to update multiple values in a table?
Let me see if I can explain what I want to do . . .
Let's say I have a table (I'll call it TableA) with values as follows:
Value1
------
This value
That value
Some value
Now, let's say I have TableB as follows:
Value1 Value2
------ ------
This value Val1
That value Val2
Some value Val3
What I want to do is populate TableA with corresponding values from TableB.
In other words, I want to do something like this:
if TableA.Value1 = TableB.Value1, then set TableA.Value2 = TableB.Value2
where Value1 is a column containing unique values.
Note: I have hundreds of rows that need to be updated in this manner, and I've come across many other instances where I'll need to do this on thousands of rows.
What's the best approach for doing this? I tried it using an UPDATE statement with JOIN, and it doesn't seem to work. Is there a way I could use a FOR..NEXT or DO..UNTIL loop to perform a line-by-line update?
Thanks in advance for your help!
May 29, 2003 at 9:55 am
try this:
Update tableA Set TableA.Value2 = TableB.Value2 join TableB on TableA.Value1 = TableB.Value1
Luani
Luani
May 29, 2003 at 10:06 am
I'm getting "Incorrect syntax near the keyword 'join'."
Ideas?
May 29, 2003 at 10:08 am
Update tableA Set TableA.Value2 = TableB.Value2 from tableA join TableB on TableA.Value1 = TableB.Value1
Luani
Luani
May 29, 2003 at 10:15 am
Or you can do just this:
Update tableA Set TableA.Value2 = TableB.Value2 from TableB where TableA.Value1 = TableB.Value1
Gregory Larsen, DBA
If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples
Gregory A. Larsen, MVP
May 29, 2003 at 10:16 am
As we say here in New York, "you da man!"
Thanks again for your help!
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply