views:

36

answers:

1

Can anyone find my error in this query? I'm using sql server 2000 and I want to update all entries in the CostEntry table to the corresponding value in the ActiveCostDetails table. The where clause DOES work with a select statement.

    UPDATE CostEntry CE 
INNER JOIN ActiveCostDetails As AD ON CostEntry.lUniqueID = ActiveCostDetails.UniqueID
       SET CostEntry.sJobNumber = ActiveCostDetails.JobNumber
     WHERE CostEntry.SEmployeeCode = '002'
       AND SubString(CostCentre, 1, 1) = sDepartmentCode
       AND substring(CostCentre, 3, 1) = sCategoryCode
       AND substring(CostCentre, 5, 2) = sOperationCode
+5  A: 

The SET needs to come before the FROM\JOIN\WHERE portion of the query.

UPDATE CE
SET sJobNumber = AD.JobNumber
FROM CostEntry CE 
    INNER JOIN ActiveCostDetails As AD 
        ON CE.lUniqueID = AD.UniqueID
WHERE CE.SEmployeeCode = '002'
    AND SubString(CostCentre, 1, 1) = sDepartmentCode
    AND substring(CostCentre, 3, 1) = sCategoryCode
    AND substring(CostCentre, 5, 2) = sOperationCode
Joe Stefanelli
+1: [SS2000 Update documentation](http://msdn.microsoft.com/en-us/library/aa260662%28SQL.80%29.aspx)
OMG Ponies
@Joe Stefanelli - Close, but the parser doesn't like the names when an alias is set.
MAW74656
This worked:UPDATE CESET CE.sJobNumber = AD.JobNumberFROM CostEntry CE INNER JOIN ActiveCostDetails As AD ON CE.lUniqueID = AD.UniqueIDWHERE CE.SEmployeeCode = '002' AND SubString(CostCentre, 1, 1) = sDepartmentCode AND substring(CostCentre, 3, 1) = sCategoryCode AND substring(CostCentre, 5, 2) = sOperationCode
MAW74656
@MAW74656: I know my eyes are getting worse as I age, but isn't that what I gave you?
Joe Stefanelli
@Joe Stefanelli - Hmmm, your right. Maybe I'm the one with the bad eyes!
MAW74656
In MAW74656's comment, sjobnumber has the table alias
OMG Ponies
@MAW74656: No problem either way. Since this worked, would you please mark this as the accepted answer?
Joe Stefanelli
@OMG Ponies: See, I told you my eyes were getting bad! :-)
Joe Stefanelli