I need you help in updating changes a table.
the table i want to update is IP_VISIT_EASTERN_REGION, the field I want to update is VISIT_STAT
so I wrote this query
UPDATE IP_VISIT_EASTERN_REGION SET
IP_VISIT_EASTERN_REGION.VISIT_STAT=TEST_IP_VISIT_EASTERN_REGION.VISIT_STAT
WHERE IP_VISIT_EASTERN_REGION.VISIT_ID = TEST_IP_VISIT_EASTERN_REGION.VISIT_ID
The Issue is I get an error
ORA-00904 INVALID IDENTIFIER
and is this the correct way to do it.
your help will be really appreciated.
thank you
You can't write an UPDATE from a SELECT by using a join. Try this approach instead.
update set from inner join query thowing error
In your case:
UPDATE IP_VISIT_EASTERN_REGION REGION
SET REGION.VISIT_STAT= ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID )
WHERE EXISTS ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID );
Fiddle:
http://sqlfiddle.com/#!4/2178ae/1
Related
I am currently taking my first course in SQL and have encountered a bit of a problem. I will now try to explain what I am trying to do. I have this select statement which properly displays what I need. MY problem arises when I try to convert it into an UPDATE statement instead.
SELECT infobb02.uni+tempbb02.sal
from infobb02 JOIN tempbb02 ON infobb02.empno=tempbb02.empno;
In case its not obvious im adding value of uni from table infobb02 to sal in table tempbb02. I have tried all sorts of things to get it to be a permanent update but keep getting errors mostly
"SQL command not properly ended"
Any help is appreciated! Thanks.
Assuming that your query is:
SELECT i.uni + t.sal
FROM infobb02 i JOIN
tempbb02 t
ON i.empno = t.empno;
If you want to update tempbb02, then:
update tempbb02 t
set t.sal = t.sal +
(select i.uni from infobb02 i where i.empno = t.empno)
where exists (select 1 from infobb02 i where i.empno = t.empno);
Instead of using an UPDATE statement, you could use a MERGE:
merge into tempbb02 tgt
using infobb02 src
on (tgt.empno = src.empno)
when matched then
update set tgt.sal = tgt.sal + src.uni;
I am looking for an alternative for this query. I know that such query will end up with invalid identifier in Oracle. So please give me the same query for updating one filed from another field in another table.
update RBT_CMP_RECOM_9304
set FIRST_RECOM_NAME=(select rbt_cmp_base_code.RBT_NAME
from rbt_cmp_base_code
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM)
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM;
FYI:
RBT_CMP_RECOM_9304=(firt_recom,first_recom_name)
RBT_CMP_BASE_CODE = (rbt_code, rbt_name)
I get this error when I try it:
ORA-00904: RBT_CMP_BASE_CODE.RBT_CODE: invalid identifier
Regards.
One way is to repeat the subquery:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select bc.RBT_NAME
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
EDIT:
If you are getting an error that more than one row is returned, then you have to decide which value. Nothing in your code suggests that this might be an issue (hint: sample data and desired results always help a question).
The easiest solution is to use and aggregation function:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select max(bc.RBT_NAME)
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
But you might want to fix the rbt_cmp_base_code table so it doesn't have duplicates. From the table name, it sounds like there should be one row per code.
I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;
The following does not work. I get the error message that it doesn't recognize Query.Field4, Query.Field5, and Query.Field6 :
SELECT Table.*
FROM Table
WHERE ((Table.Field1=Query.Field4)
AND ((Table.Field2)=(Query.Field5))
AND (Table.Field3=Query.Field6));
How can I fix this?
Edit:
I have an issue though, I need to be able to edit the records and when I use the JOIN clause it will not let me edit the records.
It looks like you haven't put a JOIN clause in your statement.
For example:
SELECT Table.*
FROM Table
JOIN Query ON (Table.Field1 = Query.Field4) AND
(Table.Field2 = Query.Field5) AND
(Table.Field3 = Query.Field6)
EDIT:
As you're using a GUI editor to edit rows in a table, you might need to do something like this:
SELECT Table.*
FROM Table
WHERE Table.UniqueIdentifier IN (
SELECT Table.UniqueIdentifier
FROM Table
JOIN Query ON (Table.Field1 = Query.Field4) AND
(Table.Field2 = Query.Field5) AND
(Table.Field3 = Query.Field6)
)
The query above assumes that you have a primary key set on your table, or a column that gives each row it's own unique identifier (such as an incrementing integer, or a GUID).
I'm trying to change all the values I have in one column (string) in my table to all lowercase letters. I've tried this statement:
update LoanerHeader t1 set Requester = (select LOWER(Requester) from LoanerHeader t2 where t2.ISO_ID = t1.ISO_ID)
But am getting the following error: Incorrect syntax near 't1'.
I don't really understand why this isn't working, because the answer to this following question has similar syntax. Can anyone help me out? Thx.
You don't need to use a subquery. You can just do:
UPDATE LoanerHeader SET Requester = LOWER(Requester);