CASE, WHEN, THEN on UPDATE

dman

I have this issue where if there is no record in copy_on_write.id then the UPDATE listings SET images = (SELECT images FROM new_vals) runs and wipes out listings.images with nothing.

So, I am trying to use a condition to only run the UPDATE listings if copy_on_write.images exist.

right now I get:

psql:queries/copy-to-source.sh:20: ERROR:  syntax error at or near "CASE"
LINE 10:       CASE WHEN images <> 

WITH
  new_vals AS (
    SELECT *
    FROM copy_on_write
    WHERE copy_on_write.posted_by = 102550922::text
      AND copy_on_write.id = 4
  ),

  updates AS (
    SELECT images FROM new_vals,
      CASE WHEN images <> ''
        THEN UPDATE listings SET images = (SELECT images FROM new_vals)
      END
  )
SELECT internal_id FROM new_vals
Oto Shavadze

You can use updates CTE like this:

...
updates AS (
    UPDATE listings SET 
    images = new_vals.images
    FROM new_vals
    WHERE new_vals.images <> ''
)
....

Note, that:

  • Your new_vals CTE should always return maximum one record, otherwise this won't works correct.

  • Also this not updates listings table, if new_vals returns images column, but it is empty string (or null). If in such cases you need run update anyway, then remove WHERE new_vals.images <> '' at all.

  • And also, this statement will update all listings.images records. Do you really want this?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update Case When, with multiple conditions

From Dev

Update with the Case When Exists clause

From Dev

Update multiple rows using CASE WHEN - ORACLE

From Dev

Ruby update_all with CASE and WHEN

From Dev

Doing an Update in SQL, with a CASE WHEN Statement

From Dev

update using case when condition is met

From Dev

Update Not Working With Case...When...Else

From Dev

How to use case, when, in with an update query

From Dev

how to use context.getContentResolver() to execute a update with CASE WHEN statement

From Dev

Mysql Bulk update performance improvements for (when.. case)

From Dev

Unable to update multiple records with one single query (CASE/WHEN))

From Dev

SQL UPDATE using CASE WHEN with PHP variable as argument

From Dev

Mysql Bulk update performance improvements for (when.. case)

From Dev

Unable to update multiple records with one single query (CASE/WHEN))

From Dev

While 'CASE WHEN' update, other columns become null

From Dev

ON DUPLICATE KEY UPDATE - Condition WHERE vs CASE WHEN vs IF?

From Dev

How to use Case-When and Update in Trigger?(Error in my code)

From Dev

CASE WHEN ... THEN

From Dev

MySql - Update/Case

From Dev

Case Statement In Update Query

From Dev

SqlServer Update Case statement

From Dev

UPDATE with WITH and CASE - PostgreSQL

From Dev

CASE in UPDATE for choose a column

From Dev

SQL Update with CASE statement

From Dev

Update Salesforce Case information

From Dev

Update with multiple CASE

From Dev

SQL Update and CASE

From Dev

Update column with case

From Dev

Sql update using case