Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way you have your statement coded now will work (once you add the column reference to the case statement, as mentioned by other posts), however, to let the rest of your syntax go uncommented on would be a disservice to others in your situation.</p> <p>While you may only need to run this query once, I and others have run into similar situations where an <code>Update</code> to multiple rows also relies on data 3 or 4 tables away from our source and has to be run many times (like in a report). </p> <p>By collapsing your sub selects into a single <code>select</code> statement and saving the results of that into a <code>#Temp</code> table or a <code>@Table</code> variable, you only have to do that lookup once, then select from the result set for your update. </p> <p>Here is a sample using a @table variable: </p> <pre><code>declare @OilStatus table (oilDatasetStatusID int) insert into @OilStatus select odss.oildatasetstatusid from OildataSetStatus odss join oilDataSet ods on ods.OilDataSetID = odss.OilDataSetID join SamplePoint sp on sp.SamplePointID = odss.SamplePointID join CustomerSite cs on cs.CustomerSiteID = sp.CustomerSiteID where cs.CustomerID = 2 update oildatasetstatus set oildatasetstatusid = case oildatasetstatusid WHEN 5 THEN 16 WHEN 6 THEN 17 WHEN 7 THEN 18 WHEN 8 THEN 18 WHEN 9 THEN 18 WHEN 10 THEN 19 WHEN 11 THEN 20 end where oildatasetlabstatusid in ( select oilDatasetStatusID from @OilStatus ) </code></pre> <p>Since I do not have your exact schema, there may be errors when trying to implement the sample above but I think you will get the idea. </p> <p>Also, whenever multiple tables are used in a single statement try to preface every column name with an alias or the full table name. It helps keep both the sql engine and the people reading your code from getting lost.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload