2

I have got an error "ERROR: subquery must return only one column " when I am runing this query:

INSERT INTO details (id, object_id, detail)
  (
    SELECT
      CASE
      WHEN (SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id))
        THEN
          concat(SUBSTRING(main_base.id, '(\d+.\d+.)'), n.counted :: TEXT, 'A')
        ELSE
           concat( SUBSTRING (main_base.id, '(\d+.\d+.)'), n.counted :: TEXT)
        END AS id,
          main_base.object_id,
          main_base.details
    FROM main_base
    CROSS JOIN LATERAL
    generate_series(1, COALESCE ((string_to_array(main_base.id, '-')) [2] :: INT, 1)) AS n (counted)
    WHERE main_base.id LIKE '%-%' AND NOT main_base.details ~ '^\.\d+|\(\.\d+\)'
  );

I have not clue what is wrong. I've read some topic that people had the same problem but still dont know how to fix it.

3
  • And that will probably raise "sub-query returned more than 1 row" error. Commented Feb 23, 2016 at 15:42
  • correct. multiple columns is returned to WHEN expr. Commented Feb 23, 2016 at 15:44
  • 1
    Maybe start by describing what you were trying to achieve with this query? Because you certainly overcomplicated it. Commented Feb 23, 2016 at 15:51

1 Answer 1

2

I think the problem is that:

SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)

Can return more than one row, so causes problems in the WHEN statement. It can return more than one row, as the subquery will return 1 every time the condition is met.

If you want to trigger the case statement based on when there exists some records in this set, could you use:

(SELECT COUNT(*) FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)) > 1
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.