There are probably better ways, but this worked for me:
Update t1
set t1.ListKey = t2.ListKey
From Jet.LookupListItemEntity_Default t1
join (Select ListKey, Id from Jet.LookupListEntity_default) as t2
on t1.ListId = t2.id
Greg
Update t1
set t1.ListKey = t2.ListKey
From Jet.LookupListItemEntity_Default t1
join Jet.LookupListEntity_default t2 on t1.ListId = t2.id
Related
I have an sql update statement:
update table1 set col1='val' where id in (select t2.id
from table2 t2
inner join table1 t1 on t1.id = t2.id
where t1.col2='val2' and t2.col3='val3');
Is there a better way to write it? I am thinking somehow not to use join since I have the table in the update construction.
Maybe:
update table1 set col1='val' where id in (select t2.id
from table2 t2
where t2.col3='val3')
and t1.col2='val2';
Is it better the last query?
Maybe with exists() ?
UPDATE table1 s
SET s.col1='val'
WHERE EXISTS(SELECT 1 FROM table2 t
WHERE s.id = t.id AND someCondition)
I have table1 and table2. They have the same columns and the column ID is the one that i can use to connect the tables.
How can i run foreach statment that will update row Name in table1 with the value for column Name in table2?
I need this so i can fix the column Name in Table1 because it is incorect , and the good values for it are in table2
I tried using a single update statement but it takes forever to execute because both tables are with over 600 000 rows
update
table1 t1
set
(
t1.name
) = (
select
t2.name
from
table2 t2
where
t2.id = t1.id
and
rownum = 1
)
where exists (
select
null
from
table2 t2
where
t2.id = t1.id
);
For this query:
update table1 t1
set t1.name = (select t2.name from table2 t2 where t2.id = t1.id and rownum = 1)
where exists (select 1
from table2 t2
where t2.id = t1.id
);
You want an index on table2(id, name).
A simple inner join should work this.
UPDATE T1
SET T1.NAME = T2.NAME
FROM MyTable T1
INNER JOIN MyOtherTable T2
ON T1.ID = T2.ID
I have the following simple SQL update:
update t2 set col = 2
from t2, t1
where t2.id = t1.id
and t1.name = 'test';
But all the rows in t2 are updated! i.e. not just the one with the id where name is 'test', how do I achieve this in Postgres?
You've got too much from from t1,t2
do this:
update t2 set col = 2
from t1
where t2.id = t1.id
and t1.name = 'test';
If you must have t2 in the from alias it and join all three tables in the where..
update t2 as t2_up set col = 2
from t2 as t2_from , t1
where t2_from.id = t1.id
and t2_up.id = t2_from.id
and t1.name = 'test';
I'm guessing that you really mean:
update t2
set col = 2
from t2 join
t1
on t1.id = t2.id
where t1.name = 'test';
The condition t1.id = t1.id doesn't really make much sense in a query. It is equivalent to t1.id is not null. Hence, all rows in t2 would get updated due to the cross join.
I have to update table(table3) with data from two other tables.
These two tables (table1,table2) are both describing single object, therefore table1.id equals table2.id for that single object.
Table 3 is a many-to-many mapping table for that object and another one which is not important here. Into that mapping table I need to put some data from table1 and table2.
I am trying to update table3 with correlated query, but it takes a very long time to complete. I ran this query on group of ~500.000 records and it runs for over an hour now.
Am I doing something wrong here?
UPDATE table3 t3
SET (t3.some_value1, t3.other_value1, t3.some_value2, t3.other_value2) =
( SELECT t1.some_value1, t1.other_value1, t2.some_value2, t2.other_value2
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id
WHERE t3.fk = t1.id)
WHERE EXISTS (SELECT 1 FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t3.fk = t1.id);
SELECT * FROM table3;
SQL Fiddle
Use this:
UPDATE
(SELECT
T3.SOME_VALUE1 TARGET1,
T3.OTHER_VALUE1 TARGET2,
T3.SOME_VALUE2 TARGET3,
T3.OTHER_VALUE2 TARGET4,
T1.SOME_VALUE1 SOURCE1,
T1.OTHER_VALUE1 SOURCE2,
T2.SOME_VALUE2 SOURCE3,
T2.OTHER_VALUE2 SOURCE4
FROM
TABLE1 T1
JOIN TABLE2 T2
ON T1.ID = T2.ID
JOIN T3
ON T3.FK = T1.ID)
SET
TARGET1 = SOURCE1,
TARGET2 = SOURCE2,
TARGET3 = SOURCE3,
TARGET4 = SOURCE4;
If you face ORA-01779 cannot modify a column which maps to a non key-preserved table then try using UPDATE /*+ BYPASS_UJCV */ or add a unique index on T1.ID, T2.ID, T3.FK
The answer provided by #SriniV uses a technique called an Updatable Join View. In later versions of Oracle, this can be replaced with a MERGE:
MERGE INTO t3
USING (
SELECT t1.id
t1.some_value1,
t1.other_value1,
t2.some_value2,
t2.other_value2
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE 1=1
AND t3.fk = t1.id
) a ON (
t3.fk = a.id
)
WHEN MATCHED THEN UPDATE
SET t3.some_value = a.some_value,
t3.other_value1 = a.other_value1
t3.some_value2 = a.some_value2
t3.other_value2 = a.other_value2
I am trying to use a inner join with an update statement with a subquery ... can you help me out with the sytax please --- and also how do you use the AS clause for alias in sql server???
the following is what i am trying to do :
Update Table1
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)
any idea??
If you need to use a subquery to perform the UPDATE you can do it this way:
UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
SELECT id, value
FROM table2
) t2
ON t1.id = t2.id
One way is to alias the table:
update t1
set table1.value1 = t2.value1
from table1 as t1
join table2 as t2
on t1.id = t2.t1_id
You should try
UPDATE table1 SET t1.value1 = t2.value2
FROM table1 t1
INNER JOIN table2 t2
ON t1.field1 = t2.field2
UPDATE Table1 t1
INNER JOIN (
SELECT id, value
FROM table2
) t2 USING(id)
SET t1.value = t2.value