I'm trying to do an update on my SQL DB (2008 R2), but for some reason it's updating more than I expected. I think there's an issue with my where, join, and update commands but I'm having trouble finding info on scope and order of operations. Fortunately, I'm practicing on DB backups that are restored before I make the change in the production one!
I've found this link that seems similar (join and update), but it's tough for me to apply it to my case.
For the testUnitCount of 15578, I'm trying to change the unitNo from 05101 to 05088. For some reason, it's moving the unitNo for all of them for this TestNumber. It's an OLAP DB (Cubes), so it may seem more complicated with all the tables than people are used to. I thought I had the command correct this time:
SELECT     DashboardData.TestNumber, TestUnits.unitNo, TestUnitCounts.testUnitCount
FROM         DashboardData INNER JOIN
                      TestUnitCounts ON DashboardData.TestUnitCountID = TestUnitCounts.testUnitCountID INNER JOIN
                      MeasurementData ON TestUnitCounts.testUnitCountID = MeasurementData.TestUnitCountID INNER JOIN
                      TestUnits ON DashboardData.TestUnitID = TestUnits.testUnitID AND TestUnitCounts.testUnitID = TestUnits.testUnitID AND 
                      MeasurementData.TestUnitID = TestUnits.testUnitID
                      where unitNo='05101'
                      AND TestNumber='1024'
                      AND TestUnitCounts.testUnitCount='15578'
                      order by testUnitCount asc
                      UPDATE TestUnits
                      SET unitNo='05088' where unitNo='05101'
Does anyone know what the command needs to be changed to so I just change the testUnitCount of 15578 for unitNo 05101->05088 for Test 1024? Why is it changing all of them for that TestNumber?
Here is my CTE attempt for the same thing. I'm getting 0 row(s) affected when I execute it:
Use OLAP05132014C
GO
WITH QueryName_CTE (unitNo,TestNumber,testUnitCount)
AS
(
SELECT     DashboardData.TestNumber, TestUnits.unitNo, TestUnitCounts.testUnitCount
FROM         DashboardData INNER JOIN
                      TestUnitCounts ON DashboardData.TestUnitCountID = TestUnitCounts.testUnitCountID INNER JOIN
                      MeasurementData ON TestUnitCounts.testUnitCountID = MeasurementData.TestUnitCountID INNER JOIN
                      TestUnits ON DashboardData.TestUnitID = TestUnits.testUnitID AND TestUnitCounts.testUnitID = TestUnits.testUnitID AND 
                      MeasurementData.TestUnitID = TestUnits.testUnitID
                      where unitNo='05101'
                      AND TestNumber='1024'
                      AND TestUnitCounts.testUnitCount='15578'
)
Update QueryName_CTE
                      SET unitNo='05088' where unitNo='05101'
 
     
     
    