Home » SQL & PL/SQL » SQL & PL/SQL » update using with statement (Oracle 11.2 on Windows 8.1)
icon5.gif  update using with statement [message #647828] Tue, 09 February 2016 16:16 Go to next message
fhsmagna
Messages: 3
Registered: February 2016
Location: USA
Junior Member
Having problem with an update statement using a with clause.

with cte as (select b1,b2,b3,b4 from table2)
update table a
set(a1,a2,a3)=
(select b1,b2,b3,b4 from cte
where b4 = a.a4
and b3 <> a.a3);

When I run this statement I get: Error ORA-00928: missing SELECT keyword
I can run the SELECT statement inside the parentheses for the cte with no errors.
The select statement for the cte is a lot more complex.
What am I doing wrong?

Thanks,
Re: update using with statement [message #647829 is a reply to message #647828] Tue, 09 February 2016 17:06 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
update table a
set(a1,a2,a3)=
(with cte as (select b1,b2,b3,b4 from table2)
select b1,b2,b3,b4 from cte
where b4 = a.a4
and b3 <> a.a3);


SY.
Re: update using with statement [message #647834 is a reply to message #647828] Tue, 09 February 2016 18:22 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:
OraFAQ Forum Guide
How to use {code} tags and make your code easier to read
Re: update using with statement [message #647872 is a reply to message #647834] Wed, 10 February 2016 09:06 Go to previous messageGo to next message
fhsmagna
Messages: 3
Registered: February 2016
Location: USA
Junior Member
Solomon,

I changed my code to how you specified and now I get this error:

ORA-01767: UPDATE...SET expression must be a subquery

If I run this part of the statement
with cte as (select b1,b2,b3,b4 from table2)
select b1,b2,b3,b4 from cte

I do not get any errors.

Re: update using with statement [message #647875 is a reply to message #647872] Wed, 10 February 2016 09:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Use SQL*Plus and copy and paste your session, the WHOLE session including object creation statements.

Note that:
set(a1,a2,a3)
select b1,b2,b3,b4
clearly show a discrepancy.

Re: update using with statement [message #647883 is a reply to message #647872] Wed, 10 February 2016 10:33 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
I just gave you an answer where should WITH clause be. Michel gave you next clue you try updating set of 3 with set of 4 elements.

SY.
Re: update using with statement [message #647951 is a reply to message #647883] Fri, 12 February 2016 07:31 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Why use the with clause when it is totally unnecessary?

update table a
set(a1,a2,a3)=
(select b.b1,b.b2,b.b3
from table2 b
where b.b4 = a.a4
and b.b3 <> a.a3)
where exists
(select null
from table2 b
where b.b4 = a.a4
and b.b3 <> a.a3);

[Updated on: Fri, 12 February 2016 07:32]

Report message to a moderator

Re: update using with statement [message #648197 is a reply to message #647951] Thu, 18 February 2016 07:34 Go to previous messageGo to next message
fhsmagna
Messages: 3
Registered: February 2016
Location: USA
Junior Member
Since I am not a dba I have limited rights in the test database. I cannot create anything. Providing a test case scenario would be difficult since I would not know whether the CREATE statement worked.

Writing the UPDATE statement without a WITH clause sounded appealing to me so I tried that but it did not update anything.
Since the table I am working with is small(less than 5K records) I am going to update everything with no filter. This works so I am abandoning the attempt to filter for only records that have changed.
Thanks for your help.
Re: update using with statement [message #648198 is a reply to message #648197] Thu, 18 February 2016 07:48 Go to previous message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Quote:
Since I am not a dba I have limited rights in the test database.

Your DBA will be able to give you any privileges you need.
Previous Topic: Scheduling job at 6 am and 7.30 pm
Next Topic: Retrieve specified characters from LONG datatype
Goto Forum:
  


Current Time: Fri Apr 19 19:15:00 CDT 2024