Trying to delete from a table where there are matching records in other tables.
I've tried different variations of this, but this one returns:
SQL Error [42601]: [SQL0199] Keyword INNER not expected. Valid tokens: USE SKIP WAIT WITH FETCH LIMIT ORDER WHERE OFFSET.
It's basically a cross library / cross database, but can't get DB2 to play along. The Select works just fine, if I replace the delete with SELECT *
DELETE a
FROM INHOUSE.ANDREWCAT a
INNER JOIN ERPLIB.SRBPRG b ON
a.PSPRDC = b.PGPRDC
INNER JOIN ERPLIB.SRBRSD c
ON
b.PGIRGP = c.RDSRTY
AND c.RDTOFI = a.EPNUM AND c.RDSRTY = c.RDWHAT
AND a.EPNUM = 'REM104'
DB2 does not support the syntax you ware using.
Instead:
DELETE INHOUSE.ANDREWCAT a
WHERE EXISTS (SELECT 1
FROM ERPLIB.SRBPRG b JOIN
ERPLIB.SRBRSD c
ON b.PGIRGP = c.RDSRTY
WHERE a.PSPRDC = b.PGPRDC AND
c.RDTOFI = a.EPNUM AND
c.RDSRTY = c.RDWHAT AND
a.EPNUM = 'REM104'
);
Related
I am getting an error on the query I'm running to get a count in MariaDB. This is the error:
Error Code: 1060. Duplicate column name 'id_number'
And this is my SQL code:
SELECT COUNT(*) as count FROM (
SELECT * FROM ((cr.customers
INNER JOIN (progress_notes_details
INNER JOIN progress_notes ON progress_notes_details.progress_note_id = progress_notes.id_number)
ON customers.id_number = progress_notes.c_id)
INNER JOIN open_balances ON progress_notes_details.id_number = open_balances.progress_notes_detail_id)
INNER JOIN
customer_payer_xref ON customers.id_number = customer_payer_xref.c_id
WHERE
(((progress_notes_details.qb_isbillable) IS NULL
OR (progress_notes_details.qb_isbillable) <> 1)
AND ((progress_notes_details.date_of_visit) BETWEEN coverage_start AND coverage_end)
AND ((progress_notes_details.dynamics_status) = 3)
AND ((customer_payer_xref.payer_id) = 23)
AND ((customer_payer_xref.primary_secondary_account_type) = 1))
) AS qdat
Can this be resolved via aliases? If so, it's unclear to me where to add them. In the main query? In the subquery?
Also, to clarify, I just inherited this code - and yes, it's bracket-happy.
Alias customers table as c and use it as c.id_number at one of the places it will remove that duplicate error as this is the only table you are using multiple times with idnumber hence duplicate
Remove the outer query:
SELECT COUNT(*)
FROM ((cr.customers . . .
Clearly, you have tables with the same column name. This causes a problem with SELECT *.
All the parentheses are probably not needed for the JOINs as well.
I saw this SQL Update statement in a trigger and am unsure if the update works accurately - based on looking at where the table alias is and the update table syntax.
The syntax doesn't give any error on execution, and updates the record correctly when executing on random samples on my test DB.
However, on a larger PROD DB with more records, is there a possibility that the update fails or skips altogether? There were reports that random records did not have the SAMPLE.ISCOMPOSITESAMPLE field set.
Questionable syntax
UPDATE SAMPLE SET
SAMPLE.SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
SAMPLE.ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
Syntax I am familiar with (similar to above but intentionally not optimised for comparison)
UPDATE C SET
SAMPLETYPE = (SELECT DESCRIPTION FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT COMPOSITESAMPLE FROM SAMPLETYPE WHERE SAMPLETYPENO = C.SAMPLETYPENO)
FROM SAMPLE C
INNER JOIN INSERTED T ON C.SAMPLENO = T.SAMPLENO
The query is correct and will work. However, there are two things that I would fix:
TheUPDATE SAMPLE does update the table whose alias is C. This is documented as correct and something that really irks me, because aliases should be respected. You should use the alias for the update.
The correlated subqueries are not using fully qualified column names.
So, using correlated subqueries, I would recommend:
UPDATE S
SET SAMPLETYPE = (SELECT ST.DESCRIPTION FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO),
ISCOMPOSITESAMPLE = (SELECT ST.COMPOSITESAMPLE FROM SAMPLETYPE ST WHERE ST.SAMPLETYPENO = S.SAMPLETYPENO)
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO ;
You can also write this -- probably more efficiently -- using a LEFT JOIN:
UPDATE S
SET SAMPLETYPE = ST.DESCRIPTION,
ISCOMPOSITESAMPLE = ST.COMPOSITESAMPLE
FROM SAMPLE S INNER JOIN
INSERTED I
ON S.SAMPLENO = I.SAMPLENO LEFT JOIN
SAMPLETYPE ST
ON ST.SAMPLETYPENO = S.SAMPLETYPENO;
I have three tables(A, B, and C) and would like to update null values in A.appointment_id with values in C.tc_appointment_id. Table A and C can be joined via table B. Blue arrows represent joins, the red arrow represents the update I am trying to achieve.
I am able to join the three tables together and have tried to modify my select statement into an update. My successful select statement and unsuccessful update follow.
--Working select
select A.tc_ASN_id,A.appointment_id, B.appt_obj_id, B.appointment_id, C.appointment_id, C.tc_appointment_id from B
join A on B.appt_obj_id = A.asn_id
join C on C.appointment_id = B.appointment_id
where C.appt_status < '9' and A.appointment_id is null;
--Update attempt that ends with SQL Error: ORA-00933: SQL command not properly ended
update asn set appoinmtent_id = ilm_appointments.tc_appointment_id
join ilm_appointment_objects on ilm_appointment_objects.appt_obj_id = asn.asn_id
join ilm_appointments on ilm_appointments.appointment_id = ilm_appointment_objects.appointment_id
where ilm_appointments.appt_status < '9' and asn.appointment_id is null;
The expected result is to update all null values for A.appointment_id to be updated with the values from C.tc_appointment_id.
UPDATE...JOIN syntax is not supported in Oracle. You could use a correlated subquery instead.
Consider:
UPDATE A
SET A.appointment_id = (
SELECT C.tc_appointment_id
FROM B
INNER JOIN C ON C.appointment_id = B.appointment_id
WHERE B.appt_obj_id = A.asn_id
)
WHERE A.appointment_id IS NULL;
Beware that the subquery must return a unique record, otherwise you will get an error like "subquery returned more than one row". Given your sample data this seems to be the OK.
Sorry if the title is unclear. Basically I'm trying to select certain records from multiple tables then update a certain column value for the returned records.
T-SQL Implementation
UPDATE
CUSTOMERS
SET
LIKES_US = 'Y'
FROM
RESTAURANT REST INNER JOIN CUSTOMERS CUST ON REST.LINK_ID = CUST.LINK_ID
WHERE
REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL
Oracle
UPDATE
(SELECT CUST.LIKES_US
FROM CUSTOMERS CUST INNER JOIN RESTAURANT REST ON CUST.LINK_ID=REST.LINK_ID
WHERE REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL) NEW_CUST
SET
NEW_CUST.LIKES_US = 'Y';
I am tried doing the same thing in Teradata as I did in Oracle but I get the following error:
Executed as Single statement. Failed [3707 : 42000] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword between the 'UPDATE' keyword and '('.
Elapsed time = 00:00:00.003
STATEMENT 1: Unknown failed.
I looked online for the solution but had no luck.
Have you tried the following syntax with Teradata:
UPDATE CUSTOMERS C1
FROM (SELECT C2.LINK_ID
FROM CUSTOMERS C2
INNER JOIN RESTAURANTS R2
ON C2.LINK_ID = R2.LINK_ID
WHERE R2.REST_TYPE = 'DINER'
AND C2.LIKES_US IS NULL) D1
SET LIKES_US = 'Y'
WHERE C1.LINK_ID = C2.LINK_ID
I think that in this specific case, the below query will perform a little better since it needs one less join.
UPDATE C
FROM CUSTOMERS C, RESTAURANTS R
SET LIKES_US = 'Y'
WHERE
C.LINK_ID = R.LINK_ID
AND R.REST_TYPE = 'DINER'
AND C.LIKES_US IS NULL
I'm having difficulty with Oracle 10g syntax for a correlated UPDATE. I am processing this code in a PL/SQL procedure.
I would approach the problem in SQL Server as so:
UPDATE table_a a
SET a.prov_id=pn.prov_id,
a.step=1
from (
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
) pn
The equivalent Oracle syntax is:
UPDATE table_a a
SET a.prov_id=(
SELECT p.prov_id
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
),
a.step=1
WHERE EXISTS (
SELECT *
FROM note n
INNER JOIN provider p ON n.author_id=p.user_id
where n.enc_id=a.enc_id
AND TRUNC(n.note_time)=a.attr_date
)
Does this in fact run the sub-query twice? Is there a more-abbreviated syntax than this?
As to whether the sub-query runs twice, use EXPLAIN PLAN.
I like to use the merge command instead of update for these correlated updates, something like: (Not tested, if you want a tested answer, please provide DDL and insert statements.)
merge into table_a TRGT
using (select P.prov_id, N.enc_id, trunc(n.note_time) as trunc_note_time
from note N
inner join provider P ON N.author_id=P.user_id) SRC
on (TRGT.enc_id = SRC.enc_id and TRGT.attr_date = SRC.trunc_note_time)
when matched then update set prov_id = SRC.prov_id
, step = 1
Sometimes you can update an inline view, something like:
update (select A.prov_id, A.step, P.prov_id as p_prov_id
from note N
inner join provider P on N.author_id=p.user_id
inner join table_a A
on N.enc_id=A.enc_id
and trunc(N.note_time)=A.attr_date)
set prov_id = p_prov_id
, step = 1
The inline view version won't always work. Can't find infor on the error, but essentially the inline view needs to have a unique key that Oracle can tie back to the tables in question.