How do I UPDATE from a SELECT of same table - sql

I should update a column of table by converting other column of same table in oracle. something like this:
update table1 set colum1=(select TO_CHAR(column2) from table1)
but I have this error
single-row subquery returns more than one row
could I do this?

It's just
update table1 set
column1 = to_char(column2);

If you ever need it for more dynamic value generation, use WHERE to match a specific record
update table1 a
set colum1 = (select TO_CHAR(column2) from table1 b where b.id = a.id)
where a.id = .... -- <-- optional, for specific records

Related

How to use all rows from outside table as parameter for stored procedure?

I want to use all the rows from a table in a stored procedure. Let's call that table2, or alias b.
The procedure I have currently is basically setting values depending on a where condition of a field.
UPDATE table1
SET a.Name = "VALID"
FROM table1 a
WHERE a.Quality = 0
I want the WHERE a.Quality = 0 clause to basically say "where a.Quality matches any number from table2 b"
How do I do this? Would it be a matter of passing the rows from the table2 as a parameter? Or can I solely use the where clause?
Thanks!
How about using a simple INNER JOIN?
Like this:
UPDATE table1
SET a.Name = "VALID"
FROM table1 a
INNER JOIN table2 b ON a.Quality = b.SomeColumnNameHere
If you insist on using "only" the WHERE clause, you could try something like this:
UPDATE table1
SET a.Name = "VALID"
FROM table1 a
WHERE a.Quality IN (SELECT SomeColumnNameHere
FROM table2)

ORACLE - Update a value from two different columns in differente tables with a filter

I'have a little question about a query.
I have to update a column from a table where there are only record of expense(integer).
I must increase the expense of 5% if the client is from a specific state, the column of the state is in a different table and the key in common is the address.
This is my query below :
UPDATE table 1 a
SET expense_vl = (
SELECT expense*1.05 FROM table 1
LEFT JOIN table2 b ON b.ADDRESS_ID=a.ADDRESS_ID
WHERE description_state IN 'lollyland'
)
I'd recommend using a semi-join:
update table_1 a
set expense_v1 = expense * 1.05
where exists (
select null
from table2 b
where
a.address_id = b.address_id and
b.description_state = 'lollyland'
)
Althought I must add that it would help if you include the DDL for your table. We're sort of guessing at which table "description" came from.
Also, when possible, include sample input for each table and desired output. We don't need a million records, just an example that illustrates your issue.
Try below
UPDATE table1 a SET expense_vl = (SELECT expense*1.05
FROM table2 b
WHERE b.ADDRESS_ID=a.ADDRESS_ID)
WHERE description_state IN 'lollyland'
Or try with subselect:
UPDATE table1
SET expense_vl = expense*1.05
WHERE ADDRESS_ID IN (SELECT ADDRESS_ID FROM table2 WHERE description_state IN 'lollyland')
I think you need to change your query like below :
UPDATE table 1 A
SET expense_vl=expense*1.05 FROM table 1
LEFT JOIN table2 B ON B.ADDRESS_ID=A.ADDRESS_ID
WHERE B.description_state IN 'lollyland'

SQL - Add column data from one table into another while preserving original data

I need to add data from one table (table1) into another table (table2) WHERE the data in the fullname column matches in both tables. The code below almost does what I want, except that it deletes all of the other data in table1's title column.
UPDATE table1
SET title = (SELECT title
FROM table2
WHERE table2.fullname = table1.fullname)
My goal is to update table1's title column to have both the data it originally had plus the data from table2's title column without erasing the data that was in table1's title column prior to running the SQL query.
I'm assuming you're using Oracle given the syntax you've given. The issue is that, when you use this form of the UPDATE statement, you also need a WHERE EXISTS clause or something similar:
UPDATE table1
SET title = ( SELECT title
FROM table2
WHERE table2.fullname = table1.fullname )
WHERE EXISTS ( SELECT 1 FROM table2
WHERE table2.fullname = table1.fullname )
Otherwise the non-matching titles will get NULLed out! The reason for this is that the result of the subquery will be NULL when fullname doesn't exist in table2.
If concatenation is what you're looking for (as #PM 77-1 comments above), then you'll want to do something like the following:
UPDATE table1
SET title = title || ',' || ( SELECT title
FROM table2
WHERE table2.fullname = table1.fullname )
WHERE EXISTS ( SELECT 1 FROM table2
WHERE table2.fullname = table1.fullname )
Hope this helps.
In Oracle you can use a Merge:
MERGE INTO table1 t1
USING (SELECT fullname, title
FROM table2) t2
ON t1.fullname = t2.fullname
WHEN MATCHED THEN
UPDATE SET t1.title = t2.title;
This will only update the rows where a match on fullname is found.

SQL update where value in other table is

I need to update a value in a table if a specific value exists in another table.
i.e.
update table1
set value1=3
where table2.value2='Y'
There is a key ref1 in both tables- how do i use this key to link these together? Many thanks!
update table1
inner join table2 on table1.ref1 = table2.ref1
set value1 = 3
where table2.value2 = 'Y'
you can write a join between both tables and then make the update from the join.
Something like:
update tableA
set column = b.value
from tableA a
join tableB on a.key = b.key

refer to outside field value in subselect?

I want to do a query to update values that I forgot to copy over in a mass insert. However I'm not sure how to phrase it.
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = **current row in update statement, outside parens**.id )
How do I do this? It seems like a job for recursion.
Use:
UPDATE YOUR_TABLE
SET text_field_1 = (SELECT t.text_field_2
FROM other_table t
WHERE t.id = YOUR_TABLE.id)
Warning
If there's no supporting record in other_table, text_field_1 will be set to NULL.
Explanation
In standard SQL, you can't have table aliases on the table defined for the UPDATE (or DELETE) statement, so you need to use full table name to indicate the source of the column.
It's called a correlated subquery -- the correlation is be cause of the evaluation against the table from the outer query.
Clarification
MySQL (and SQL Server) support table aliases in UPDATE and DELETE statement, in addition to JOIN syntax:
UPDATE YOUR_TABLE a
JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
...is not identical to the provided query, because only the rows that match will be updated -- those that don't match, their text_field_1 values will remain untouched. This is equivalent to the provided query:
UPDATE YOUR_TABLE a
LEFT JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
If there is one ID field:
UPDATE updtable t1
SET t1.text_field_1 = (
SELECT t2.text_field_2
FROM seltable t2
WHERE t1.ID = t2.ID
)
;
UPDATE Table1, Tabl2
SET Table1.myField = Table2.SomeField
WHERE Table1.ID = Table2.ID
Note: I have not tried it.
This will only update records where IDs match.
Try this:
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = table.id )