title: Setting a PostgreSQL variable from inside a CASE statement
body: <p>Currently using PostgresSQL 10 as a newbie, and following documentation as best I can. All help welcome. </p>
  
  <p>I'm trying to set the value of a variable based on a case statement so I can use that value in the next part of my code. I'm having issues with syntax, and honestly just understanding how to set the value from within the case statement. </p>
  
  <p>Below is the code that I've got so far. </p>
  
  <p>I'm trying to set the value of the variable "AndSol". The value will either bet set at 0, or 1 based upon the case statement. </p>
  
  <p>The block with the comment "Solar Radiation" is where I'm having trouble setting the value of the variable. The rest of the code is included just for reference as to what I'm trying to achieve. I'm ending each where clause with something along the lines of <strong>THEN AndSol := 1;</strong>, and I assume this is where the error lies. I don't know how to fix it.</p>
  
  <p>Basic flow:</p>
  
  <ol>
  <li><p>Work out whether the variable should be 0, or 1. </p></li>
  <li><p>Set the variable.</p></li>
  <li><p>Use the variable in the next part.</p></li>
  </ol>
  
  <pre><code>DO $$
  DECLARE AndSol NUMERIC;
  BEGIN
      DROP MATERIALIZED VIEW IF EXISTS view_6day_mat;
      CREATE MATERIALIZED VIEW view_6day_mat
      AS
          SELECT prod_qld_6km_grids.weather_cell_id,
                  prod_qld_6km_grids.latitude,
                  prod_qld_6km_grids.longitude,
  
                 /* Solar Radiation */
                  CASE
                      WHEN EXTRACT(MONTH FROM prod_weathergrids.dates) &gt;= 8 AND EXTRACT(MONTH FROM prod_weathergrids.dates) &lt;= 12 THEN AndSol := 1;
                      WHEN EXTRACT(MONTH FROM prod_weathergrids.dates) = 1 AND EXTRACT(MONTH FROM prod_weathergrids.dates) &lt;= 2 AND prod_weathergrids.cloudcover &lt; 30 then AndSol := 1;
                  ELSE AndSol := 0; END/* 0 END AS AndersonsSolarRad, */
  
                  /* Moisture Content */
                  CASE WHEN prod_weathergrids.rh &gt; 0 AND prod_weathergrids.temperature &gt; 0 THEN
                      ROUND(4.37+0.161*prod_weathergrids.rh-0.1*(prod_weathergrids.temperature-25)-AndSol*0.027*prod_weathergrids.rh), 2)
                  ELSE 0 END AS AndersonsHMC
  
                  FROM MyTable
      ORDER BY prod_qld_6km_grids.weather_cell_id
  END $$;
  </code></pre>
  
  <p>The error message I receive from pgAdmin is:</p>
  
  <pre><code>ERROR:  syntax error at or near ":="
  LINE 49: ...(MONTH FROM prod_weathergrids.dates) &lt;= 12 THEN AndSol := 1;
                                                                     ^
  SQL state: 42601
  Character: 2440
  </code></pre>
  
  <p>I must admit, I don't know how to overcome the error, beacuse the documentation and examples I've seen mostly all set the variable by using ":=".</p>