Alternative update SQL query - sql

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.

Related

sql server if statement not working

Can anyone advise me as to what is wrong with the following SQL server update statement:
IF (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT') = ''
GO
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID = (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
You don't need an if statement - you can just run the update statement, and if the subquery returns no rows, no rows will be updated. The if won't really save anything - you're performing two queries instead of one.
You either want
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID In (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
if there are multiple ID's with that code OR use
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE code = 'SOUND_WRONG_GARMENT'
either way and lose the IF statement as #Mureinik said.
Although Mureinik's answer is the logical solution to this, I will answer why this isn't actually working. Your condition is wrong, and this approach will work instead using IF EXISTS:
IF EXISTS (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT')
BEGIN
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID IN (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
END
As a side note, you're using an = sign instead of IN, which means you'll be matching to an arbitrary singular ID and only update 1 row based on this. To use a set based operation, use the IN clause.
You could actually 'golf' this by doing away with the derived query altogether, and using a simple WHERE code='SOUND_WRONG_GARMENT' on the table you're updating on.

Update a table from another table on multiple columns in Oracle 11g

Oracle 11g SQL & both tables have the same column definitions:
VARCHAR2(11)
NUMBER
DATE
DATE
I tried to find a solution to this problem, and this is what I ended up with, which fails:
update jjjTable
set [fourthCol] = B.[fourthOtherCol]
from jjjTable, otherTable B
where jjjTable.[firstCol] = B.[firstOtherCol]
and jjjTable.[secondCol] = B.[secondOtherCol]
and jjjTable.[thirdCol] = B.[thirdOtherCol]
I'm under the impression that I need to have the from in this was based on this article:
SQL update from one Table to another based on a ID match and the edited response from Shivkant
I'm under the impression that I may need to use a join based on this article:
How do I UPDATE from a SELECT in SQL Server? and the response from Robin Day
but as I understand it, joins are only on one column match per row. I'm interested in matching on 3 elements, and I'm not finding a clear path for solution.
Any direction would be well received.
This is what I ended up needing to do as a solution:
DECLARE
CURSOR j_CUR IS
SELECT A.[fourthCol]
FROM JJJtable A, otherTable B
WHERE A.[firstCol] = B.[firstOtherCol]
and A.[secondCol] = B.[secondOtherCol]
and A.[thirdCol] = B.[thirdOtherCol]
FOR UPDATE OF B.[fourthOtherCol];
SOME_DATE DATE;
BEGIN
FOR IDX IN j_CUR LOOP
SOME_DATE :=(IDX.[fourthCol]);
UPDATE otherTable
SET [fourthOtherCol] = SOME_DATE
WHERE CURRENT OF j_CUR;
END LOOP;
END;
Thank you for your efforts and guidance.
This is the close best I was able to get it to work on my similar use case. Try this out.
update jjjTable
SET jjjTable.[fourthCol] = (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol)
WHERE EXISTS (SELECT distint otherTable.fourthOtherCol from otherTable
WHERE otherTable.firstOtherCol = jjjTable.firstCol and
otherTable.secondOtherCol = jjjTable.secondCol and
otherTable.thirdOtherCol = jjjTable.thirdCol);

Update table from another table for certain fields

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

Oracle SQL update

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;

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234