0

I found multiple solutions of running a query only if a parameter is not null, but none of them seems to fit my scenario.

I have to following UPDATE script:

DO '
BEGIN
    IF EXISTS
        (SELECT 1 FROM pg_class where relname=$J$scm_repos$J$)
    THEN
        UPDATE scm_repos repos SET token_id=(SELECT token_id FROM scm_orgs orgs WHERE repos.org_id=orgs.id);
        UPDATE scm_orgs SET token_id = NULL;
    END IF ;
END;
'

I want the the 1st UPDATE script to run only if the token_id on the scm_orgs is not null. What is the proper way doing it?

1 Answer 1

1

What you need is using UPDATE statement with FROM part in it:

UPDATE scm_repos AS repos SET 
  token_id= orgs.token_id
FROM 
  scm_orgs AS orgs
WHERE
  repos.org_id=orgs.id
  AND orgs.token_id IS NOT NULL
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.