A
A
aarifkhamdi2017-10-06 13:05:10
MySQL
aarifkhamdi, 2017-10-06 13:05:10

What is the correct way to write a double update?

WITH hide AS
(
  UPDATE "TABLE1"
  SET
    "Value1" = FALSE
  WHERE "кое-что" = ANY ($1 :: INT [])
)
UPDATE "TABLE1"
SET
  "Value2" = TRUE
WHERE "кое-что" = ANY ($1 :: INT [])
      AND NOT EXISTS(
    SELECT NULL
    FROM "TABLE2"
    WHERE "что-то" = "кое-что"
)
      AND NOT EXISTS(
    SELECT NULL
    FROM "TABLE3"
    WHERE "что-то" = "кое-что"
)

as a result, only the second part works. how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2017-10-06
@aarifkhamdi

You are greatly mistaken if you think that your cte is being executed sequentially.
https://www.postgresql.org/docs/9.6/static/queries...
You are performing different actions on the same data nugget of the same rows. Do not do it this way. I have no idea what effect this will have.
In addition, you simply rewrite in one simple request

UPDATE "TABLE1"
SET
  "Value2" = (NOT EXISTS(
    SELECT NULL
    FROM "TABLE2"
    WHERE "что-то" = "кое-что"
)
      AND NOT EXISTS(
    SELECT NULL
    FROM "TABLE3"
    WHERE "что-то" = "кое-что"
))
WHERE "кое-что" = ANY ($1 :: INT [])

R
res2001, 2017-10-06
@res2001

Remove the with statement, leave only the inner update from it.
If you declare with, then you need to use it, but you don’t use it, so the first update doesn’t work either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question