0

I don't understand why this provokes a syntax error (missing right parenthesis):

UPDATE table
SET doc =
  (SELECT 'table-2844-doc' || SUBSTR(doc_file, INSTR(doc_file, '.', -1))
   FROM docvers
   WHERE (docvers.table_name = 'other_table'
          AND docvers.field_name = 'doc')
     AND ROWNUM = 1
   ORDER BY VERSION DESC)
WHERE table_id = 2844

This looks right to me, does get executed correctly in SQL Server, and is similar to requests found, for example, in Oracle SQL: Update a table with data from another table.

Any tip?

2
  • 1
    It probably doesn't do what you want (because it selects a single arbitrary row and then orders it -- which does nothing). But I don't see an obvious syntax error. Commented Jul 20, 2016 at 21:47
  • 2
    It is syntax error. Scalar subquery block does not have ORDER BY in its syntax. Commented Jul 20, 2016 at 21:56

1 Answer 1

2

Do it like this:

UPDATE table
SET doc = (
 select r.myval
 from (
 SELECT 'table-2844-doc' || SUBSTR(doc_file, INSTR(doc_file, '.', -1)) myval, ROWNUM RN
 FROM docvers
 WHERE docvers.table_name = 'other_table'
 AND docvers.field_name = 'doc'
 ORDER BY VERSION DESC
 ) r
 where r.RN = 1
)
WHERE table_id = 2844

Select the data set first including the ROWNUM, then select from that data set the first row.

Sign up to request clarification or add additional context in comments.

1 Comment

Weird that we have to make so many contorsions to be able to run a quite simple query. However, your solution stays quite clear, and does work. Thanks!

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.