-1

I have this query that I need to update

UPDATE pomhst hst
   SET hst.ponot1 = hdr.poshp1 
  FROM pomhdr hdr
 WHERE LENGTH(LTRIM(RTRIM(hst.poshp1))) > 0
   AND hdr.postat = 3;

and when I execute it, it shows this error

SQL Error [42601]: [SQL0199] Keyword FROM not expected. Valid tokens: USE SKIP WAIT WITH FETCH LIMIT ORDER WHERE OFFSET. I do not know what is wrong with my query

I tried adding inner join but it didnt work as well.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Weboshii
  • 21
  • 4

3 Answers3

1

Some Database Management System such as Oracle, DB2 doesn't support the syntax

UPDATE a_table SET .. FROM another_table unlike to some others such as Postgres.

You can alternatively use a MERGE statement as the below one :

MERGE INTO pomhst hst
     USING pomhdr AS hdr
        ON LENGTH(LTRIM(RTRIM(hst.poshp1)))  > 0 
       AND postat = 3
       AND hdr.id = hst.id
      WHEN MATCHED THEN UPDATE SET hst.ponot1 = hdr.poshp1

Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
1

You can use exists:

UPDATE pomhst hst SET hst.ponot1 = hdr.poshp1 
WHERE exists (select *  from FROM pomhdr hdr where LENGTH(LTRIM(RTRIM(hst.POSHP1)))  > 0 AND hdr.POSTAT = 3);
Jens
  • 67,715
  • 15
  • 98
  • 113
0

DB2 still does not support joins in update statements, so you'll need to use MERGE;

OR, You can simply do it using a subquery as follows :

update pomhst hst 
set hst.ponot1 = (
    select hdr.poshp1 
    from pomhdr hdr 
    where hdr.ponum = hst.ponum and hdr.POSTAT = 3
) 
where LENGTH(LTRIM(RTRIM(hst.POSHP1))) > 0;

Here is a similar example.

Tushar
  • 3,527
  • 9
  • 27
  • 49