Update a table column from the column of another table in MsAccess - sql

In Ms Access 2010 I have two identical tables, say A and B.
The structure of each table is
key | text_column1 | text_column2
I have to update the rows of table A such that if for a given row in A the two text columns are empty, then the value from table B must be given.
Looking around, I tried
UPDATE A as a
SET a.text_column1 = (SELECT b.text_column1 FROM B AS b WHERE b.key = a.key),
a.text_column2 = (SELECT b.text_column2 FROM B AS b WHERE b.key = a.key)
WHERE a.text_column1 IS NULL and a.text_column2
Needless to say that the previous query doesn't work. I got error 3073 (2010)

You can use Access's UPDATE .. JOIN syntax:
UPDATE A as a
INNER JOIN B as b
ON a.Key = b.Key
SET a.text_column1 = b.text_column1,
a.text_column2 = b.text_column2
WHERE a.text_column1 IS NULL
AND a.text_column2 IS NULL;
Another option is to use Dlookup:
UPDATE A
SET text_column1 = Dlookup("text_column1", "B", "Key = "& A.Key),
text_column2 = Dlookup("text_column2", "B", "Key = "& A.Key)
WHERE a.text_column1 IS NULL
AND a.text_column2 IS NULL;

Try this
UPDATE A
SET A.text_column1 = (SELECT b.text_column1 FROM B AS b WHERE b.key = A.key),
A.text_column2 = (SELECT b.text_column2 FROM B AS b WHERE b.key = A.key)
WHERE A.text_column1 IS NULL and A.text_column2 IS NULL;

Related

update multiple rows with joins

I have this query in postgresql:
select *
from A s
join B m on (s.id=m.id)
where m.key=4 and s.ran=some_input_from_user
This gives me all the rows that I need to update.
I want to set A.value to be 90 for all these rows.
It doesn't look like a standart update query
if I do...
Update A set value=90 where.....
then I can't do the join.
any ideas how to do it?
This is the basic update syntax for PostgreSQL where you are updating based on a join to another table:
update A s
set
value = 90
from B m
where
s.id = m.id and
m.key = 4 and
s.ran = some_input_from_user
The trick is you never use the alias in the lvalue for the set commands. In other words, value = 90 is not s.value = 90. It seems minor, but I'm pretty sure it will prevent your query from working. The rationale is if you are updating table A (alias s) then any fields you are updating are, de-facto, from table A -- no need to alias them, and to allow aliases would almost imply you could update something other than A with this statement, which you cannot.
You can definitely use them in the rvalues, so this would certainly be okay (if it were your desire to update A based on B):
update A s
set
value = m.salary * s.commission
from B m
where
s.id = m.id and
(s.value is null or
s.value != m.salary * s.commission)
Here is the query:
update a set value = 90
where exists (
select 1 from b
where a.id = b.id and b.key=4
and a.ran=some_input_from_user);
The above query will eliminate the requirement of reading table a twice.
Also you can use this query:
update a set value = 90
where a.id in
(select b.id from b
where a.id = b.id and b.key = 4
and a.ran=some_input_from_user);
TRY THIS
UPDATE A
SET A.VALUE = 90
from A
join B m on (A.id=m.id)
where m.key=4 and s.ran=some_input_from_user

Update with two table ,I had a example for one row,but I must update 168432 rows,used sqlite

I want to update the sqlite table, the following is the example for updating one row:
update tpecroad set tnode = (SELECT b.nodeid FROM "TPECRoad" as a
join tpecnode as b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry and a.pk =1)
where pk=1
but there are 168432 rows to be updated, is there any faster way to update the large amount of the data?
It's like changed a.pk=1~168432 and pk=1~168432
Thanks a lots!!
update tpecroad as c
set tnode = ( SELECT b.nodeid
FROM "TPECRoad" as a join tpecnode as b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry and a.pk = c.pk);
If I understand the question right, this query may help:
update tpecroad a set tnode = (
select b.nodeid from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
)
where exists (
select 1 from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
);
Edit: If a.pk = b.pk must verify to perform the update, the query will look as follows:
update tpecroad a set tnode = (
select b.nodeid from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
and a.pk = b.pk
)
where exists (
select 1 from tpecnode b
where pointn(a.geometry,numpoints(a.geometry)) = b.geometry
and a.pk = b.pk
);
Try this:
Update tpecroad a
set tnode = (SELECT b.nodeid
FROM tpecnode as b
where pointn(tpecroad.geometry,numpoints(tpecroad.geometry)) = b.geometry)

Updating a table

I have two tables: A, B.
Here's what I want to do (this is obviously not valid SQL):
UPDATE A a, B b SET a.pic = b.pic WHERE a.my_id = b.my_id
i.e. when the column my_id matches in tables A and B, I want to copy the pic column from B to A.
What's the right way of doing this?
The correct postgres query:
UPDATE A a
SET pic = b.pic
FROM B b
WHERE a.my_id = b.my_id;
You're only really updating A (and you can only update one table in an an UPDATE statement anyways)
UPDATE A a SET a.pic = ( SELECT b.pic FROM B b WHERE a.my_id = b.my_id)

Update on join in Sqlite

I have two two tables A and B in which both P columns are common and I need to use update command in table B only when both p values are same and C column from table A is given
What I am trying is:
update B
set P =100
where B.P=A.P
and A.C=60
But it's giving me error no such column A.P
You are updating table B and do not have reference to table A, so sqlite just does not know where to look for. Try this:
UPDATE B
SET P = 100
WHERE B.P IN (SELECT A.P
FROM A
WHERE A.C = 60)
You can do it like this
Update B set P = 100 WHERE B.P = (Select P from A WHERE C = 60)

convert if-else statement inside a cursor in a set-based approach

I have a script containing a cursor with if-else statement, but it takes too much times to browse the table. (a table with 79000 rows takes 1h).
So i need to convert it in a set-based approach.
The if statement is
IF (
SELECT count (b.key)
FROM general..ean a,
general..mainframe b,
general..hope c
WHERE a.ean = #ean
AND a.c_suppression = '0'
AND a.key = b.key
AND b.key = c.key
AND c.canal = #canal
) = 0
where #ean and #canal are value retrieved in each row with the cursor. The table browsed is tmp_day_house_info_corporate.
So i need to retrieve all rows from tmp_day_house_info_corporate, for which #info and #canal in the if statement retrieve 0.
Thank you for any help.
SELECT *
FROM tmp_day_house_info_corporate
WHERE not exists(
SELECT b.key
FROM general..ean a,
general..mainframe b,
general..hope c
WHERE a.ean = tmp_day_house_info_corporate.ean
AND a.c_suppression = '0'
AND a.key = b.key
AND b.key = c.key
AND c.canal = tmp_day_house_info_corporate.canal
)
Count is highly ineffective when checking if record exists, you would be much better with not exists
IF NOT EXISTS (
SELECT *
FROM general..ean a,
general..mainframe b,
general..hope c
WHERE a.ean = #ean
AND a.c_suppression = '0'
AND a.key = b.key
AND b.key = c.key
AND c.canal = #canal
)
If this is till to slow show your full query and maybe it will be possible to make it set-based.