Hi i'm using this command to left join a table with another table that has a little more records but it keep printing NULL. I want that NULL to become 0.
SELECT * FROM TABLE1
Left JOIN Table2 ON TABLE1.ID=Table2.IDRel
UPDATE Table2 SET IDRel = 0 where IDRel = NULL
The update should not be needed.
As you said Table2 has a little more records that's the key thing here, that means that for any row from Table1 for which no matching IDRel value could be found that column (IDRel) will stay NULL.
Now you could for example use ISNULL(IDRel, 0) to replace null value with 0, but maybe an INNER JOIN instead of the LEFT JOIN could get you the right result throwing out all rows that could not be matched...
However... If you really were to update that column it would only work if you did the correct comparison against NULL (that is IS not =) that would mean changing your update query into:
UPDATE Table2 SET IDRel = 0 where IDRel IS NULL
IS the update even needed?
SELECT T1.*, coalesce(t2.IDREL,0) as IDREL
FROM TABLE1 T1
Left JOIN Table2 ON TABLE1.ID=Table2.IDRel
Use this query
UPDATE Table2 SET IDRel = 0 where IDRel is NULL
use IS NUll
SELECT * FROM TABLE1
Left JOIN Table2 ON TABLE1.ID=Table2.IDRel
UPDATE Table2 SET IDRel = 0 where IDRel IS NULL
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.
I have two different variations of update statements for a stored procedure. The top one does not work and the bottom one does.
Could any of you please provide insight as to why it doesn't?
UPDATE table1
SET outcome = (
SELECT outcome
FROM table2
WHERE table1.StatusID = table2.StatusID
AND table1.IDUser = table2.UserID
)
The one below works, even though I have exactly the same constraints.
UPDATE a
SET a.outcome = b.outcome
FROM table1 A
INNER JOIN table2 B ON A.IDUser = B.UserID AND A.StatusID = B.StatusID
The first update will fail, when there are more rows in table2 matching the join. The second update will pick an arbitrary value for outcome from the join and use that value in the update.
This change to the first update should work, or rather give the same result:
UPDATE table1
SET outcome = (
SELECT TOP 1 outcome
FROM table2
WHERE table1.StatusID = table2.StatusID
AND table1.IDUser = table2.UserID
)
Maybe this would be better than your existing update. This way you will have some control of which value will end up in outcome in table1:
UPDATE table1
SET outcome = (
SELECT MAX(outcome)
FROM table2
WHERE table1.StatusID = table2.StatusID
AND table1.IDUser = table2.UserID
)
It is normal that first query does not work the way you want because it is a wrong query.
Your first code has a main query and a sub query.
In your subquery, you join the tables and get a result set.
But in your main query, you set your every row with the returned result from sub query, since you have no where block. There should be a null value in that result set. This is the reason of you having null after update.
You must do the joining out of your subquery, exactly like you do in the second code.
UPDATE table1
SET outcome = (
SELECT TOP(1)outcome
FROM table2
WHERE table2.StatusID = table1.StatusID
AND table2.IDUser = table1.IDUser
)
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';
I have two tables that they share two fields (myfield1, myfield2) and one of the two tables has 2 other fields of interest.
Here is what I want to do:
1. Inner Join the two tables
2. Update a column (field1) in Table2 with either fields (afield or anotherfield) from the other table depending on afield is null or not.
The code below runs fine but the targeted set field (field1) doesn't get updated with anything.
Update Table2
Set field1 = (
CASE
WHEN os.afield is not null
THEN (os.afield)
Else os.anotherfield
End
)
from Table1 os
inner join Table2 fd
ON fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2;
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);
It's called a correlated subquery which is executed for each row in Table2. But you must be sure that subquery returns single or zero rows.
This query will update all rows in Table2 if you want to update only those rows which exist in Table1 you need a WHERE
Update Table2 fd
Set fd.field1 =
(select CASE WHEN os.afield is not null THEN (os.afield) Else os.anotherfield End
from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2)
where exists (
select 1 from Table1 os
where fd.myfield1= os.myfield1
AND fd.myfield2 = os.myfield2);