I have got a problem with an update statement in sql. I want to update param1 in table1 if the conditions are true (shown in my example).
Currently I am trying this:
update table1
set table1.param1 = 1
from (select * table1, table2
where table1.param2=table2.paramA
and table2.paramB='123456'
and table1.param3='XXX123');
You can try this.
update (select *
from table1 join table2
on table1.param2=table2.paramA
where table2.paramB='123456'
and table1.param3='XXX123') t
set t.param1 = 1
You don't need to do join in subquery. You can do the join and update in one go. Please try below query for your update.
update table1 join table2 on table1.param2=table2.paramA
set table1.param1 = 1
where table2.paramB='123456'
and table1.param3='XXX123';
Related
I want to update a column of a table1 but I should update only records where conditions are true in another table
something like this, but I don't know how to implement this purpose in Oracle SQL
update table1
join table2 on table1.msg_id = table2.id
set table1.index = table1.index-1
where table1.index > 10 and table2.type = 'myType'
I would write this as an exists subquery in any database:
update table1 t1
set index = t1.index - 1
where table1.index > 10 and
exists (select 1
from table2
where t2.id = t1.msg_id and
t2.type = 'myType'
);
The join sort of implies that you are going to use data from table2 in the update of table1. Instead, you simply want to change a value in a row when a particular condition is met.
Oracle does not support this syntax (sigh).
For your use case, you could use a not exists condition with a correlated subquery instead:
update table1
set table1.index = table1.index - 1
where
table1.index > 10
and exists (
select 1 from table2 where table1.msg_id = table2.id and table2.type = 'mytype'
)
Note: make your live easier, do not use index for a column name. This is a reserved work in pretty much all RDBMS.
I'm working on SQL Server, Oracle, DB2 Database.
Currently, I'm performing the below 2 update statement to update one table.
Update 1:
UPDATE TABLE1
SET COL1=TABLE2.ATTRIBUTE1,
COL2=TABLE2.ATTRIBUTE2,
COL3=TABLE2.ATTRIBUTE3
FROM TABLE1
INNER JOIN TABLE2
ON COL1=TABLE2.PID1
AND COL2=TABLE2.PID2
WHERE ROWNUM<10
Update 2:
UPDATE TABLE1
SET COL1=’123-4567890-1’,
COL2=’0000000000’,
COL3=’CONSTANT FULL NAME’
WHERE NOT EXISTS (SELECT 1 FROM TABLE2 WHERE COL1=TABLE2.PID1)
Update 1, helps to updates the TABLE1 if the values match with Table2 and
Update 2, helps to update the TABLE1 if the values, not match with Table2
Is there any other way to convert two update statement into single UPDATE statement?
NOTE: We can use MERGE also, but MERGE will update if the data matched and will insert if the data not matched.
To have one update, you can use two LEFT JOINs, one for each table join condition. And then, in SET part, you use CASE WHEN ... THEN ... END checking if the PK from related joins IS NULL.
Something like below
UPDATE TABLE1
SET COL1=CASE
WHEN T1.PID1 IS NOT NULL THEN T1.ATTRIBUTE1
WHEN T2.PID1 IS NULL THEN ’123-4567890-1’
ELSE COL1
END,
etc.
FROM TABLE1
LEFT JOIN TABLE2 T1 ON COL1=T1.PID1 AND COL2=T1.PID2 AND ROWNUM < 10
LEFT JOIN TABLE2 T2 ON CEDULA=T2.PID1
WHERE T2.PID1 IS NULL OR T1.PID1 IS NOT NULL
However, having one UPDATE statement doesn't mean it will be faster - check the query plan and actual performance.
looking for some advice on how to perform an update to a table, but where my 2 WHERE statements come from different tables. Here's what I'm thinking:
UPDATE table1
SET table1.t1field=('new parameter');
FROM table1
WHERE table1.t1field > ('parameter')
INNER JOIN
table 2
ON table2.t2field = ('my other parameter');
Basically, what I want to do is update X, where X = myparameter (from table 1) and myparameter2 (from table 2).
Am I getting the syntax right? If I take out the semi-colon after the new parameter, I get a "SQL command not properly ended" error, but with it left in, I think it's trying to update everything in the table! Obviously I don't want this, I only want it tp update that paramater if it meets the 2 criteria.
Thanks for any help you can give.
It should be something like following:
UPDATE table1
INNER JOIN table2
ON table2.join_field = table1.join_field
SET table1.t1field=('new parameter'), table2.t2field = ('my other parameter')
FROM table1
WHERE table1.t1field > ('parameter');
The correct syntax for update queries with join is the following:
UPDATE t1
SET t1.[column]=[value]
FROM table1 t1 INNER JOIN table2 t2 ON [join condition]
WHERE [conditions]
I'm having a problem. I need to update a table using plsql, but the condition in the where clause will depend upon conditions in a different table. For example
UPDATE table1
set column1 = ...
where table2.column = ...
This isn't what I have. However, I do need to know how to properly do this.
Here's how you use a JOIN in an UPDATE query:
UPDATE table1 AS t1
JOIN table2 AS t2 ON t1.somecol = t2.someothercol
SET t1.column1 = ...
WHERE t2.column = ...
You can write an update command as follows:
UPDATE employees e SET taxable = 'Y'
WHERE EXISTS (SELECT 1 FROM SALARIES s
WHERE s.employee_id = e.employee_id
AND s.salary > LOWER_LIMIT)
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 )