473,395 Members | 1,516 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,395 software developers and data experts.

Oracle Update Statement using Concatenate

Hi guys,

This is my first post out here. I read the guidelines, did not find anything relevant via searching, so I would like some help here.

I am trying to write an update statement on a table based on fields from two other tables to a third joined table.

Table 1 has a field part code and a primary key (T1.partcode, T1.1PK)
Table 2 has a field warehouse code and a primary key (T2.whcode, T2.2PK)
Table 3 hold unique information joining table 1 and table 2, so it looks like this:

T3.partcode-whcode with foreign keys indicating the record relationship to T1 and T2. Here's what I have:
Expand|Select|Wrap|Line Numbers
  1. UPDATE table3
  2. SET partcode-whcode = T1.partcode || '-' || T2.whcode 
  3. FROM T1, T2, T3
  4. WHERE T2.2PK = T3.T2FK AND T3.T1FK = T1.1PK AND 
  5. partcode-whcode <> T1.partcode || '-' || T2.whcode
On SQL Server this would work (with diff CONC method) because the FROM clause is acceptable, but Oracle doesn't use it and I'm sure there's a better way anyways, but I am just out of ideas and searching has not come up positive.

There's probably also a better way to do my join as well instead of in the WHERE clause. Any help is appreciated. If any other information is required, just ask! I am relatively new (untrained) in the SQL world, but I learn quick and retain knowledge well!!

-Derek
Feb 28 '08 #1
12 28594
debasisdas
8,127 Expert 4TB
Are you facing any problem with the code ?
Feb 29 '08 #2
Are you facing any problem with the code ?

Thanks for your reply. Yes, this statement won't work with Oracle. I am wondering how to rework the code to update the field to a concatenate for the two source fields combined with a dash.

My problem is really the table linking and how to tell Oracle my source fields from different tables than the table being updated.

Thanks again,

Derek
Feb 29 '08 #3
debasisdas
8,127 Expert 4TB
I can't find any reason why the code will not work in oracle.
Mar 3 '08 #4
I can't find any reason why the code will not work in oracle.

I get a 'SQL Statement Improperly Ended' error. I don't think oracle accepts 'FROM' in UPDATE STATEMENTS. I normally would use a subquery but get errors on that because the subquery results in multiple rows per line, so I need a way to join and represent multiple tables in the main query. My subquery would look like this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE table3
  2. SET partcode-whcode = (SELECT T1.partcode || '-' || T2.whcode 
  3. FROM T1, T2, T3
  4. WHERE T2.2PK = T3.T2FK AND T3.T1FK = T1.1PK AND 
  5. partcode-whcode <> T1.partcode || '-' || T2.whcode)

Thanks again,

Derek
Mar 3 '08 #5
amitpatel66
2,367 Expert 2GB
I get a 'SQL Statement Improperly Ended' error. I don't think oracle accepts 'FROM' in UPDATE STATEMENTS. I normally would use a subquery but get errors on that because the subquery results in multiple rows per line, so I need a way to join and represent multiple tables in the main query. My subquery would look like this:

Expand|Select|Wrap|Line Numbers
  1. UPDATE table3
  2. SET partcode-whcode = (SELECT T1.partcode || '-' || T2.whcode 
  3. FROM T1, T2, T3
  4. WHERE T2.2PK = T3.T2FK AND T3.T1FK = T1.1PK AND 
  5. partcode-whcode <> T1.partcode || '-' || T2.whcode)

Thanks again,

Derek
Is the table3 and T3 the same in your update query??
And are you trying to update two different columns, then why concatnating them while update.

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE table3 T3
  3. SET (T3.partcode,T3.whcode) = (SELECT T1.partcode,T2.whcode 
  4. FROM T1, T2
  5. WHERE T2.2PK = T3.T2FK AND T1.1PK = T3.T1FK AND 
  6. T3.partcode <> T1.partcode AND
  7. T3.whcode <> T2.whcode)
  8.  
  9.  
After the update, you can fetch the data from the table using concatenation method:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT partcode||'-'||whcode from table3
  3.  
  4.  
Mar 4 '08 #6
Is the table3 and T3 the same in your update query??
And are you trying to update two different columns, then why concatnating them while update.

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE table3 T3
  3. SET (T3.partcode,T3.whcode) = (SELECT T1.partcode,T2.whcode 
  4. FROM T1, T2
  5. WHERE T2.2PK = T3.T2FK AND T1.1PK = T3.T1FK AND 
  6. T3.partcode <> T1.partcode AND
  7. T3.whcode <> T2.whcode)
  8.  
  9.  
After the update, you can fetch the data from the table using concatenation method:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT partcode||'-'||whcode from table3
  3.  
  4.  

Thanks for your response. I am actually trying to concatenate the two fields from table 1(part code) and table 2(warehouse code) to a single field in table 3 that separates the identifiers with a dash. the established relationship already in place are foreign keys stored in table 3. Here's the ACTUAL table names and code I am working with:

Expand|Select|Wrap|Line Numbers
  1. UPDATE f_invent_link inv
  2. SET inv.inv_record_code = (SELECT pa.pa_code || '-' || wh.wh_code 
  3. FROM f_parts pa, f_warehouse wh, f_invent_link inv2
  4. WHERE wh.wh_pk = inv2.inv_wh_fk AND pa.pa_pk = inv2.inv_pa_fk AND 
  5. pa.pa_code || '-' || wh.wh_code)
  6.  
This statement doesn't work, gets an error about multiple records in subquery. So there are 3 tables, f_parts, f_warehouse, and f_invent_link. The 3rd table holds transactional inventory quantities unique to part and warehouse. the field inv_record_code is supposed to hold the part code from f_parts and the warehouse code from f_warehouse joined with a dash. I have some that do not and trying to get those populated.

I think that by using 2 different aliases for f_invent_link, I can establish a relationship between the two queries (main and sub) by using a WHERE EXISTS statement, but not sure.

Hope I'm not too confusing here :)

Thanks again

derek
Mar 4 '08 #7
amitpatel66
2,367 Expert 2GB
Alright, your small change here would be as shown in the below code. Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE f_invent_link inv
  3. SET inv.inv_record_code = (SELECT pa.pa_code || '-' || wh.wh_code 
  4. FROM f_parts pa, f_warehouse wh
  5. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  6. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code)
  7.  
  8.  
Mar 4 '08 #8
Alright, your small change here would be as shown in the below code. Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE f_invent_link inv
  3. SET inv.inv_record_code = (SELECT pa.pa_code || '-' || wh.wh_code 
  4. FROM f_parts pa, f_warehouse wh
  5. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  6. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code)
  7.  
  8.  
Same problem I ran into originally: ORA-01427: single-row subquery returns more than one row. That's why I wanted to use this one that works in SQL Server and just get it working with Oracle:

Expand|Select|Wrap|Line Numbers
  1. UPDATE f_invent_link inv
  2. SET inv.inv_record_code = pa.pa_code || '-' || wh.wh_code
  3. FROM f_parts pa, f_warehouse wh
  4. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  5. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code
  6.  
Oracle does not accept FROM arguments in Update statements and does not accept multiple tables in the Update argument. I know this is a tricky one!
Mar 4 '08 #9
amitpatel66
2,367 Expert 2GB
Same problem I ran into originally: ORA-01427: single-row subquery returns more than one row. That's why I wanted to use this one that works in SQL Server and just get it working with Oracle:

Expand|Select|Wrap|Line Numbers
  1. UPDATE f_invent_link inv
  2. SET inv.inv_record_code = pa.pa_code || '-' || wh.wh_code
  3. FROM f_parts pa, f_warehouse wh
  4. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  5. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code
  6.  
Oracle does not accept FROM arguments in Update statements and does not accept multiple tables in the Update argument. I know this is a tricky one!
Since the error says it returns more than one row, now you need to decide what value you want to update with from the multiple records, the MAX value or MIN or any other value?
Mar 5 '08 #10
Since the error says it returns more than one row, now you need to decide what value you want to update with from the multiple records, the MAX value or MIN or any other value?
It returns more than one row because I am not establishing a link between the data returned in the sub query and the table of the primary update query. This is where I believe the WHERE EXISTS statement comes into play, but I am not for sure and wouldn't know how to formulate it.

Again, I could be way off on this assumption, so happy to hear any suggestions.

There's not a MAX or MIN value to to choose though. There's only a single PACODE-WHCODE possibility for the record set return. I think it just needs that relationship established between the main and sub queries.

Thanks again,
Mar 5 '08 #11
amitpatel66
2,367 Expert 2GB
It returns more than one row because I am not establishing a link between the data returned in the sub query and the table of the primary update query. This is where I believe the WHERE EXISTS statement comes into play, but I am not for sure and wouldn't know how to formulate it.

Again, I could be way off on this assumption, so happy to hear any suggestions.

There's not a MAX or MIN value to to choose though. There's only a single PACODE-WHCODE possibility for the record set return. I think it just needs that relationship established between the main and sub queries.

Thanks again,
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE f_invent_link inv
  3. SET inv.inv_record_code = (SELECT pa.pa_code || '-' || wh.wh_code 
  4. FROM f_parts pa, f_warehouse wh
  5. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  6. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code)
  7. WHERE NOT EXISTS( SELECT 'X' FROM f_parts p, f_warehouse w WHERE inv.inv_record_code = p.pa_code || '-' || w.wh_code)
  8.  
  9.  
Mar 6 '08 #12
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. UPDATE f_invent_link inv
  3. SET inv.inv_record_code = (SELECT pa.pa_code || '-' || wh.wh_code 
  4. FROM f_parts pa, f_warehouse wh
  5. WHERE wh.wh_pk = inv.inv_wh_fk AND pa.pa_pk = inv.inv_pa_fk AND 
  6. inv.inv_record_code <> pa.pa_code || '-' || wh.wh_code)
  7. WHERE NOT EXISTS( SELECT 'X' FROM f_parts p, f_warehouse w WHERE inv.inv_record_code = p.pa_code || '-' || w.wh_code)
  8.  
  9.  
This worked!!

One question though...in the Where Not Exists argument, you're querying a specific value, just any record return for the link you want? You can SELECT 'ANYTHING'?
Mar 6 '08 #13

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

Similar topics

4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
4
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
4
by: Don | last post by:
Hi, I am moving from Sybase to Oracle and I used to be able to do update statement like this in Sybase: UPDATE TABLE1 SET T1.field1 = T2.field2 FROM TABLE1 T1, TABLE2 T2 WHERE T1.field2...
0
by: JWM | last post by:
I am trying to implement Oracle connection pooling for the following code, which was written by someone else. Here is my main question -- this java file creates code that is executed every hour,...
1
by: Saimon | last post by:
Questo Statement in SQL Server funziona. In Oracle PL/SQL se lo lancio funziona Quando lo devo far funzionare da Vb.net mi si pianta e non va avanti . Sta lì a pensare. Come mai ? UPDATE...
24
by: clinttoris | last post by:
Could some please finish this line of code. For the life of me whatever I try it does not work. I am trying to insert the current date time into oracle. Now I know that in oracle if I run ...
0
by: gshawn3 | last post by:
Hi, I am having a hard time creating a Trigger to update an Oracle database. I am using a SQL Server 2005 Express database on a Win XP Pro SP2 desktop, linked to an Oracle 10g database on a...
2
by: Vinod Sadanandan | last post by:
All, Below listed are the new features in Oracle 11g ,please join me in this discussion to generate a testcase and analyze each of the listed features . Precompilers:...
3
by: Anonymous | last post by:
I resolved this issue. If you are using an Oracle XA DataSource, Oracle's JDBC driver will automatically issue a COMMIT when it sees a "DROP TABLE foobar" statement. Oracle's JDBC driver...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.