SQL update query syntax with inner join
Asked Answered
F

3

34

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
Foxglove answered 5/10, 2010 at 19:49 Comment(3)
@OMG Ponies- Thanks for looking, but this query has the same effect as the original (not working due to syntax error). I believe this is because SQL server ignores spaces and carriage returns in a query.Foxglove
I just reformatted what you posted--easier to read, easier to helpSepaloid
Ok ponies, I understand. I was working off an example, online... tek-tips.com/faqs.cfm?fid=1958 But your way is better.Foxglove
F
63

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
Fromenty answered 5/10, 2010 at 19:52 Comment(8)
+1: SS2000 Update documentationSepaloid
@Joe Stefanelli - Close, but the parser doesn't like the names when an alias is set.Foxglove
This worked:UPDATE CE SET CE.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) = sOperationCodeFoxglove
@MAW74656: I know my eyes are getting worse as I age, but isn't that what I gave you?Fromenty
@Joe Stefanelli - Hmmm, your right. Maybe I'm the one with the bad eyes!Foxglove
In MAW74656's comment, sjobnumber has the table aliasSepaloid
@MAW74656: No problem either way. Since this worked, would you please mark this as the accepted answer?Fromenty
@OMG Ponies: See, I told you my eyes were getting bad! :-)Fromenty
E
4

Once you have set an alias name for the table, you cannot use the table name. Try your query this way, it will work.

UPDATE CostEntry CE 

        INNER JOIN 
            ActiveCostDetails AD 
            ON (CE.lUniqueID = AD.UniqueID)

           SET CE.sJobNumber = AD.JobNumber

         WHERE CE.SEmployeeCode = '002'
           AND SubString(CostCentre, 1, 1) = sDepartmentCode
           AND substring(CostCentre, 3, 1) = sCategoryCode
           AND substring(CostCentre, 5, 2) = sOperationCode
Elspet answered 20/7, 2020 at 12:56 Comment(1)
This is true in case of MySQLJuni
M
0

This should work

UPDATE CE
SET CostEntry.sJobNumber = ActiveCostDetails.JobNumber
FROM CostEntry CE 
INNER JOIN ActiveCostDetails As AD ON CostEntry.lUniqueID = ActiveCostDetails.UniqueID       
     WHERE CostEntry.SEmployeeCode = '002'
       AND SubString(CostCentre, 1, 1) = sDepartmentCode
       AND substring(CostCentre, 3, 1) = sCategoryCode
       AND substring(CostCentre, 5, 2) = sOperationCode
Mortensen answered 17/5, 2017 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.