473,396 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how can i update a table which is self joined?

Hello,

I am fresh in Oracle and I encounter a problem when I want to update a field from a table which is joined with itself.

Functional description:

Into a table, it is possible to have a new line which should be sent to the company's customers.
In order to not be sent each time same line, there exists a flag (field TO_SEND). The line which should be sent can be canceled and for this a new line is inserted in same table. If 1st line wasn't sent to the customer and it is canceled, in that moment will exists 2 lines with flag TO_SEND = Y and both of them will be sent to customer. The goal is to send only the 2nd line (the cancel line and not both of them).

For this, I was thinking to use the following script:

update order_table C
set C.TO_SEND = 'N'
from order_table C, order_table S
where C.BU = S.BU
and C.PO_NUM = S.PO_NUM
and C.PO_LINE = S.PO_LINE
and C.CARRIER = S.CARRIER
and C.PO_DATE = S.PO_DATE
and C.PRODUCT_ID = S.PRODUCT_ID
and C.TO_SEND = S.TO_SEND
and C.BU = 'Ac2'
and C.TO_SEND = 'Y'
and C.STATUS = 'CREATION'
and S.STATUS = 'SUPPRESSION';

The error returned is:

Error at Command Line:3 Column:1
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:

Do, someone have any clue why it doesn't work?

Thanks.
Aug 19 '10 #1
10 3154
amitpatel66
2,367 Expert 2GB
Try like this:

Expand|Select|Wrap|Line Numbers
  1. update order_table C
  2. set C.TO_SEND = 'N'
  3. WHERE UPPER(C.BU) = 'AC2'
  4. and   C.TO_SEND = 'Y'
  5. and   C.STATUS IN ('CREATION','SUPPRESSION');
  6.  
  7.  
Aug 19 '10 #2
@Amitpatell66:

unfortunately, your script updates all lines which satisfy the conditions, meaning if The table look like this:

BU |PO_NUM|PO_LINE|CARRIER|PO_DATE |PRODUCT_ID|TO_SEND|STATUS
--------------------------------------------------------------------------------------------
Ac2|A000001|000001|_____A1|08.19.2010|P00000001 |Y|CREATION
---------------------------------------------------------------------------------------------------------
Ac2|A000001|000001|_____A1|08.19.2010|P00000001 |Y|SUPPRESSION

will update both lines.

I would need to update only the line with STATUS = 'CREATION' and not also the one with STATUS = 'SUPPRESSION'

anyway, thanks for your time.
Aug 19 '10 #3
amitpatel66
2,367 Expert 2GB
Try this in that case:

Expand|Select|Wrap|Line Numbers
  1. update order_table C 
  2. set C.TO_SEND = 'N' 
  3. WHERE UPPER(C.BU) = 'AC2' 
  4. and   C.TO_SEND = 'Y' 
  5. and   C.STATUS = 'CREATION';
  6.  
Aug 19 '10 #4
This is working in case when I have only the 1st line but i want to be done in case when there exists both lines (not only one of them).
Aug 19 '10 #5
amitpatel66
2,367 Expert 2GB
Can you please post some sample data and the sample expected output? Also please explain what your below statement means:

"This is working in case when I have only the 1st line but i want to be done in case when there exists both lines (not only one of them). "
Aug 19 '10 #6
the example data are in my 2nd post.

BU |PO_NUM|PO_LINE|CARRIER|PO_DATE |PRODUCT_ID|TO_SEND|STATUS
--------------------------------------------------------------------------------------------
Ac2|A000001|000001|_____A1|08.19.2010|P00000001 |Y|CREATION
---------------------------------------------------------------------------------------------------------
Ac2|A000001|000001|_____A1|08.19.2010|P00000001 |Y|SUPPRESSION

the select i want to create should return only 1st line.

my comment ..."This is working in case when I have only the 1st line but i want to be done in case when there exists both lines (not only one of them). "...refers to the fact that the select should be used only in case when there exists the 2nd line too and not only the 1st line (both lines must exists for an order).
Aug 19 '10 #7
amitpatel66
2,367 Expert 2GB
Ok. Thanks for the explanation.

Can you try this update:

Expand|Select|Wrap|Line Numbers
  1.  
  2. update order_table C  
  3. set C.TO_SEND = 'N'  
  4. WHERE UPPER(C.BU) = 'AC2'  
  5. and   C.TO_SEND = 'Y'  
  6. and   C.STATUS = 'CREATION'
  7. AND   2 = (SELECT COUNT(status) FROM order_table s WHERE
  8. s.PO_NUM = c.PO_NUM
  9. and s.PO_LINE = c.PO_LINE
  10. and s.CARRIER = c.CARRIER
  11. and s.PO_DATE = c.PO_DATE
  12. and s.PRODUCT_ID = c.PRODUCT_ID
  13. and s.TO_SEND = c.TO_SEND
  14. and UPPER(s.BU) = 'AC2' 
  15. and s.TO_SEND = 'Y') 
  16.  
Aug 19 '10 #8
sorry, it is still not working.
the error is:
SQL Error: ORA-00933: SQL command not properly ended
Aug 20 '10 #9
amitpatel66
2,367 Expert 2GB
Are you still trying your old query?

The syntax for the query that i provided is correct.

For Ex:

Expand|Select|Wrap|Line Numbers
  1. SQL> ed
  2. Wrote file afiedt.buf
  3.  
  4.   1  update emp e set salary = 100020
  5.   2  from 2 = (SELECT COUNT(empid) from emp s
  6.   3* where s.empid = e.empid)
  7. SQL> /
  8. from 2 = (SELECT COUNT(empid) from emp s
  9. *
  10. ERROR at line 2:
  11. ORA-00933: SQL command not properly ended
  12.  
  13.  
  14. SQL> ed
  15. Wrote file afiedt.buf
  16.  
  17.   1  update emp e set salary = 100020
  18.   2  where 2 = (SELECT COUNT(empid) from emp s
  19.   3* where s.empid = e.empid)
  20. SQL> /
  21.  
  22. 0 rows updated.
  23.  
  24. SQL> 
  25.  
Aug 20 '10 #10
nop, sorry.
case resolved
Sep 1 '10 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: amwi | last post by:
I have tried to solve this on my own for a long time now, so i really need some help here... I use Oracle 10.1 and SQL *plus 10.1. How do i update table a.fkid from table b.pkid with the...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
9
by: baonks | last post by:
hello all here is my problem: I have 2 table 1: K_POS SALDO_A_D SALDO_A_K 11100 105 5 11200 5 105
2
by: Ron | last post by:
Hello, I am trying to do a simple update on employee information. I am a novice at both aspx and SQLServer, so I hope you are not too offended by my code. The following is the update code: ...
2
by: Ennio-Sr | last post by:
Hi! I'm thinking about creating a table listing my shares (say 'Shares') and then update its price column from a .txt file (say 'Prices') downloaded from an internet site periodically. Recalling...
2
by: Paul712 | last post by:
Recently, I have a table that I use to update a master table. When I run the same Update query that's been successful in the past, most all of the data in the fields in the update fields has been...
0
by: jainapurva108 | last post by:
Hi, I have one table with some values as shown in below format... Column-A Column-B Column-C Column-D Column-E 11AA ------ 1234 ------ ASDF------ FIRST ------ONE 22BB ------ ...
5
by: SQL Learner | last post by:
Hi Alex (Kuznetsov) and All, This is to follow up with my last post, "Link two tables using partial word match". How can I UPDATE table using partial word match? How can I write a SQL statement...
1
by: mrobinsc | last post by:
** This SQL statement returns 4 rows SELECT COUNT(*) G.ACTIVITY_ID G.RESOURCE_TYPE G.RESOURCE_CATEGORY G.RESOURCE_SUB_CAT G.ANALYSIS_TYPE G.PROJECT_ID
1
by: adithi | last post by:
My Table Structure is: Table A Table B Table C colA -PK Col B-PK Col C-PK Col B-FK ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.