Update table with multiple joins - sql

I am not really a SQL guy so any help would be appreciated. I am trying to update a table when fields match on one column and do not on another:
UPDATE dleads
SET mag_name = magazines.mag_name
FROM magazines
WHERE dleads.umc = magazines.umc and
dleads.mag_name <> magazines.mag_name
I can get the SELECT to work, but not the UPDATE.

Your query is not robust in handling NULL values.
You could use this instead:
UPDATE dleads SET mag_name = magazines.mag_name FROM magazines WHERE dleads.umc = magazines.umc AND COALESCE(dleads.mag_name, '') <> COALESCE(magazines.mag_name, '');

Your query looks like it's doing exactly what you describe:
UPDATE dleads d
SET mag_name = m.mag_name
FROM magazines m
WHERE d.umc = m.umc
AND d.mag_name <> m.mag_name;
The most common thing being overlooked here would be NULL values. If mag_name can be NULL in either table, and you want to update anyway, use the NULL-safe IS DISTINCT FROM:
UPDATE dleads d
SET mag_name = m.mag_name
FROM magazines m
WHERE d.umc = m.umc
AND d.mag_name IS DISTINCT FROM m.mag_name;
Aside: You mention "multiple joins" in the title, but I only see a single join in your query.

Related

Update SQL query by comparing two table

How to update query in sql column by comparing two tables ? This might be duplicated question, but yet still cannot solve my problem. Any help would be appreciated.
What i've tried so far, but error
UPDATE b SET b.STAMP = b.STAMP + 10 FROM TB_FWORKERSCH b,TB_FWORKERCN a
WHERE a.ISSDATE>='20150401' AND a.UKEY=b.UKEY2 and b.STAMP=0 AND b.IG_SUMINS!=0
DB2 Database
DB2 doesn't allow a JOIN or FROM for an UPDATE clause (this is also not specified in the SQL standard).
You can achieve what you want with a co-related sub-query:
UPDATE tb_fworkersch b
SET stamp = stamp + 10
WHERE EXISTS (SELECT 1
FROM tb_fworkercn a
WHERE a.issdate >= '20150401'
AND a.ukey = b.ukey2)
AND b.stamp = 0
AND b.ig_sumins <> 0
Try this:
MERGE INTO TB_FWORKERSCH b
USING TB_FWORKERCN a
ON a.UKEY=b.UKEY2
AND a.ISSDATE>='20150401' AND b.STAMP=0 AND b.IG_SUMINS<>0
WHEN MATCHED
THEN UPDATE SET b.STAMP = b.STAMP + 10;

Postgres no results from linked tables where linked values are null

I have this query:
update CA_San_Francisco as p
set geo = u.geo
from parcels_union u
where u.street_number = p.street_number
and u.street_name = p.street_name
and u.street_type = p.street_type
and u.street_direction = p.street_direction
and u.street_unit = p.street_unit
However it does not update any rows where both fields are null. In other words, if there is no value in street_direction for both tables, I get no result even though they are both the same - both null values.
I get that something can't be = Null. So how do I get all of the results?
Thanks, Brad
You could check if the field is NULL and if it is then change it to something you can check for.
For instance, you can change your Where clause to the following
where coalesce(u.street_number,'') = coalesce(p.street_number,'')
and coalesce(u.street_name,'') = coalesce(p.street_name,'')
and coalesce(u.street_type,'') = coalesce(p.street_type,'')
and coalesce(u.street_direction,'') = coalesce(p.street_direction,'')
and coalesce(u.street_unit,'') = coalesce(p.street_unit,'')
But if there are multiple rows that have NULL in these columns then you will get unexpected assignments in your update...

SQL Server: select only if nested select returns something

I am trying to set the following query so that it only selects any data from the DaysMov table (A) if the nested Select finds a matching date in the Calendar table (B).
Currently in such a case it just returns nothing for "dayMov" but still selects the other data from table A.
Can someone tell me how I can achieve this, e.g. by using Where or Case ?
SELECT
(
SELECT B.calendar_DT
FROM Calendar B
WHERE B.year_id = #selYear AND B.month_of_year = A.calMonth AND B.day_of_week = A.calDay AND (B.day_of_month BETWEEN A.calFrom AND A.calTill)
) AS dayMov,
A.countries,
A.regions,
'variable' AS validity,
A.name,
A.desc,
'' AS mode
FROM DaysMov A
WHERE A.mode = ''
Many thanks for any help with this, Tim.
You should be able to do the same thing with an inner join, like this:
SELECT
B.calendar_DT AS dayMov,
A.countries,
A.regions,
'variable' AS validity,
A.name,
A.desc,
'' AS mode
FROM DaysMov A
INNER JOIN Calendar B ON B.year_id = #selYear AND B.month_of_year = A.calMonth AND B.day_of_week = A.calDay AND (B.day_of_month BETWEEN A.calFrom AND A.calTill)
WHERE A.mode = ''
This query will not produce a row when Calendar does not have a match on the specified condition. Assuming that the query from your post has worked (except that it produced some rows that you did not want) the query above shouldn't produce additional rows, because the subquery on the same condition returned at most a single result.

ORA-00904 Invalid Identifier - Update Statement With Two Tables

I'm working with PeopleSoft Campus Solutions, and we need to update about 22,000 rows of data. This is data between the tables of ACAD_PLAN_VW and ACAD_PROG. Students are listed on both, so their IDs match between the two.
Basically what we are trying to do is say that when the ID, academic career, student career number, effective sequence, and effective date match, AND where the academic plan (their degree, as stored on the ACAD_PLAN_VW) is a specific value, update the ACAD_PROG on the ACAD_PROG table to X value.
I tried to do some very interesting combinations of FROM statements, constantly getting errors. After some researching, I found out that SQLTools doesn't really like FROM statements within UPDATE statements, so I rewrote it to just make the connections manually. I'm assuming I'm doing this right, unless I need to reword it.
The statement I have is:
UPDATE PS_ACAD_PROG SET PS_ACAD_PROG.ACAD_PROG = 'UGDS'
WHERE PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
AND PS_ACAD_PLAN_VW.ACAD_PLAN = 'DSTDS'
Theoretically, I would assume that this would update any student who has those connections. However, the error that I'm currently getting is as follows:
ORA-00904: "PS_ACAD_PLAN_VW"."ACAD_PLAN": invalid identifier
I have, as of yet, been unable to figure out the issue. I do have the correct access to view and update those fields, and the field does indeed exist.
Oracle doesn't know it should use the PS_ACAD_PLAN_VW table. Somehow you should reference it.
For example can you try this way?
UPDATE (
select
PS_ACAD_PROG.ACAD_PROG,
PS_ACAD_PLAN_VW.ACAD_PLAN
from
PS_ACAD_PROG,
PS_ACAD_PLAN_VW
where
PS_ACAD_PLAN_VW.EMPLID = PS_ACAD_PROG.EMPLID
AND PS_ACAD_PLAN_VW.ACAD_CAREER = PS_ACAD_PROG.ACAD_CAREER
AND PS_ACAD_PLAN_VW.STDNT_CAR_NBR = PS_ACAD_PROG.STDNT_CAR_NBR
AND PS_ACAD_PLAN_VW.EFFSEQ = PS_ACAD_PROG.EFFSEQ
AND PS_ACAD_PLAN_VW.EFFDT = PS_ACAD_PROG.EFFDT
)
SET
ACAD_PROG = 'UGDS'
WHERE
ACAD_PLAN = 'DSTDS'
Try to use the table which is not getting identified,by keeping in where clause so that you can use the select statement.That will help identifying the tables.
The following implementation should work:
UPDATE PS_ACAD_PROG
SET ACAD_PROG = 'UGDS'
WHERE ROWID IN(SELECT PROG.ROWID
FROM PS_ACAD_PROG PROG
, PS_ACAD_PLAN_VW PLAN
WHERE PLAN.EMPLID = PROG.EMPLID
AND PLAN.ACAD_CAREER = PROG.ACAD_CAREER
AND PLAN.STDNT_CAR_NBR = PROG.STDNT_CAR_NBR
AND PLAN.EFFSEQ = PROG.EFFSEQ
AND PLAN.EFFDT = PROG.EFFDT
AND PLAN.ACAD_PLAN = 'DSTDS');

multi part identifier could not be bound sql

I get multipart can not be bound error on following query
update nfltx
set
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
but if i remove b from boxno message and all i do not get the error . What is the reason behind this. Thank You using sql server 2008
A table alias specified in a FROM clause cannot be used as a qualifier in SET column_name. This is not valid:
update nfltx
set
b.boxno = a.boxno,
b.message = a.message,
b.nameboxno = a.nameboxno,
b.namemsg = a.namemsg,
b.phoneboxno = a.phoneboxno,
b.phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
To make it work, remove the b. alias from the column name.
update nfltx
set
boxno = a.boxno,
message = a.message,
nameboxno = a.nameboxno,
namemsg = a.namemsg,
phoneboxno = a.phoneboxno,
phonemsg = a.phonemsg
FROM ofltx a JOIN nfltx b
ON a.ls_fullnam = b.ls_fullnam
Raj
What is the reason behind this?
An UPDATE (and DELETE, INSERT) can affect one, and only one, table. You've already identified which table you want to affect here:
update nfltx
Therefore, it doesn't make sense to allow an alias for the left hand side of assignments in the SET clause. They must be columns belonging to the previously identified table.
If the same table is included in the FROM clause multiple times (and it's the table you wish to update), you would need to provide an alias to indicate which instance of the table is to be updated - but you'd provide it (once) in the UPDATE clause rather than in the SET clause.
just use
update b
instead of
update nfltx
Man you guys make things too difficult for those that are learning.