How can I write a SQL statement to update a column in a table from another column in the same table? - sql

I have an oracle DB where a program is writing into two columns when it updates the table. The 2nd column is based on the value from the 1st column. Well over time people have hand edited the database and forgot to insert values into the 2nd column. I'd like to write a simple sql statement that updates all columns and syncs the 2nd column to the 1st column. I know there's some simple statement to do this. Doing a little googling I thought something like the following:
UPDATE suppliers
SET supplier_name = (
SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id
)
WHERE EXISTS (
SELECT customers.name
FROM customers
WHERE customers.customer_id = suppliers.supplier_id
);
However, that is between 2 different tables where I would be doing it on the same table.

The following works in SQL Server (haven't checked Oracle yet).
UPDATE SUPPLIERS SET Supplier_Name = CustomerName
I'd give this a try and see if it works...

If both columns are in the same table, you can use the simplest query:
UPDATE your_table
SET column1 = column2
WHERE column1 != column2;
This supposes that both columns are NOT NULL. If the columns are nullable however, use decode instead:
UPDATE your_table
SET column1 = column2
WHERE decode(column1, column2, 1, 0) = 0;

update tableName set col2 = col1

Related

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

How can I update the value of multiple rows to a different specified value in the same column?

Say I have a table where there are product IDs, product desc., and the language of each desc. I would like it so that if there was a description with a NULL value for American-English, it would update to the British-English version of that the description for that product. Is there a way to do this using the update command in SQL?
I normally prefer this syntax for updating values in one table from values in another (or in this case the same) table, b/c it is easy to change the UPDATE...SET to a SELECT for testing and to quickly see what values would be updated.
UPDATE p_US
SET p_US.product_desc = p_GB.product_desc
FROM dbo.product p_US
INNER JOIN dbo.product p_GB ON p_US.productID = p_GB.productID
WHERE p_US.language_code = 'US'
AND p_GB.language_code = 'GB'
AND p_US.product_desc IS NULL
;
and then you can swap out the first two lines above with this for quick testing to see what would be updated:
SELECT p_US.productID, p_US.product,
oldDesc = p_US.product_desc, newDesc = p_GB.product_desc
update [table] set [column]= case [change factor] when '1' then 'X' else 'Y' end where [where clause]
Maybe:
UPDATE my_table SET desc=(SELECT desc from my_table WHERE my_table.id=id AND my_table.lang='british') WHERE lang='american' and desc is NULL;

update table by giving old and new entry from query

I select a list of old and new values for a table with a query:
select new, old from SOME_TABLE;
new old
----------- -----------
1174154 1064267743
1174164 1072037230
1174167 1065180221
1174180 1071828953
1174181 1067402664
1174204 1073143287
1174215 1057480190
1174222 1061816319
1174331 1072011864
1174366 1061275972
now i need to update a table that contains these old values and replace them by the new
ones.
update OTHER_TABLE set some_column = <newvalue> where some_column = <oldvalue>
Is it possible to do this with one query or do i need to loop over the result tuples and update for each row?
I cannot change the database layout or write a trigger that does this automatically...
Try the below:
UPDATE OTHER_TABLE t1
SET some_column = (SELECT t2.new FROM SOME_TABLE t2
WHERE t2.old = t1.old_value_column)
Just replace old_value_column with the column name that hold the old value in OTHER_TABLE, along with the other table and column names.
i would not use a subquery as afaik you will select some_table for each row in the table that is to be updated. should the number of rows in both tables be large, you may run into problems. therfore i suggest the update-from method outlined below.
update t
set yourcolumn = n.new
from yourtable t
join some_table s
on t.id = s.old

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