How to delete a common row from multiple tables using SQL - sql

I understand you can delete from multiple tables using joins. But, I'm not sure how to do that. I tried using this statement and it didn't work either.
delete trm, val
from MCS.stg_mdcr_trmntn_rpt trm, MCS.stg_mdcr_vldtn_rpt val
where trm.import_proc_id = 156;

What's wrong with
delete from MCS.stg_mdcr_trmntn_rpt
where import_proc_id = 156;
delete from MCS.stg_mdcr_vldtn_rpt
where import_proc_id = 156;
commit;

Related

Access: Delete Query asks for a parameter

I try to run the following DELETE query:
DELETE T_PROPOS.*, T_PROPOS.Projekt, T_PROPOS.[PSP-Element], T_PROPOS.Typ, T_PROPOS.Jahr, T_PROPOS.Wert, T_PROPOS.Datum
FROM T_PROPOS
WHERE NOT (((T_PROPOS.Projekt)=[T_PROPOS_Ohne_Duplikate].[Projekt]) AND ((T_PROPOS.[PSP-Element])=[T_PROPOS_Ohne_Duplikate].[PSP-Element]) AND ((T_PROPOS.Typ)=[T_PROPOS_Duplikate_löschen].[Typ]) AND ((T_PROPOS.Jahr)=[T_PROPOS_Duplikate_löschen].[Jahr]) AND ((T_PROPOS.Wert)=[T_PROPOS_Duplikate_löschen].[Wert]) AND ((T_PROPOS.Datum)=[T_PROPOS_Duplikate_löschen].[Datum]));
But when I run the query Access asks me for the parameters of every condition. So I need to give values to [T_PROPOS_Ohne_Duplikate].[Projekt] etc. But I want to delete all rows from T_PROPOS which are not in T_PROPOS_Ohne_Duplikate. How can I make Access compare the two tables?
Since you refer to columns from two unreferenced tables, consider two NOT EXISTS clauses. Also DELETE * FROM is equivalent to DELETE FROM in MS Access.
DELETE
FROM T_PROPOS main
WHERE NOT EXISTS
(SELECT 1 FROM [T_PROPOS_Ohne_Duplikate] o_dup
WHERE main.Projekt = o_dup.[Projekt]
AND main.[PSP-Element] = o_dup.[PSP-Element])
AND NOT EXISTS
(SELECT 1 FROM [T_PROPOS_Duplikate_löschen] l_dup
WHERE main.Typ = l_dup.[Typ]
AND main.Jahr = l_dup.[Jahr]
AND main.Wert = l_dup.[Wert]
AND main.Datum = l_dup.[Datum]);
Use this below simple query. Not sure if it works in Access.
delete from T_PROPOS where Projekt not in
(select Projekt from T_PROPOS_Ohne_Duplikate);

What's wrong with my DELETE query

I've two tables with same columns. I'm trying to delete rows from table1 '600_LONDON_NUMBER' which are in table2 '600_LONDON_NUMBER1'. Below is my query but when I run it, MS Access says "Could not delete from the specified Tables". Please Help
DELETE [600_LONDON_NUMBER].*
FROM 600_LONDON_NUMBER INNER JOIN 600_LONDON_NUMBER1
ON ([600_LONDON_NUMBER].GFCID = [600_LONDON_NUMBER1].GFCID) AND ([600_LONDON_NUMBER].CUSTBaseNO = [600_LONDON_NUMBER1].[CUST Base NO]);
P.S. When I run the SELECT Statement for the same query, it retrives the data without any Issue. I've also checked that data is not readonly I can delete using simple DELETEquery.
you may use EXISTS to solve it
DELETE 600_LONDON_NUMBER.* FROM 600_LONDON_NUMBER
WHERE EXISTS (
SELECT 1 FROM 600_LONDON_NUMBER1
WHERE [600_LONDON_NUMBER].GFCID = [600_LONDON_NUMBER1].GFCID) AND
[600_LONDON_NUMBER].CUSTBaseNO = [600_LONDON_NUMBER1].[CUSTBaseNO]
)

Deleting rows based on multiple Joins and where statements in SQL

I have this below SQL statement that I am running on an Oracle database that I'd ideally like to put inside a stored procedure to be called through Java statements. However, I'd like to get the query working first. I have a few tables which have data and my query should satisfy a few conditions through Inner Joins before executing the Delete statement. So, here's the first query I had constructed. I keep getting "SQL command not properly ended" error:
Delete from ur
from ATESTuser_roles ur
Inner Join ATESTresource_roles rr
on ur.role_id = rr.role_id
Inner Join ATESTRESOURCES r
on rr.resource_id=r.RESOURCE_ID
where r.name='TestName'
and ur.user_id = '1401'
I also tried this version and it still dint work. I got a "ATESTRESOURCES"."RESOURCE_ID": invalid identifier error for this:
Delete from ATESTuser_roles
where ATESTuser_roles.role_id = ATESTresource_roles.role_id
and ATESTresources.name='TestName'
and ATESTresource_roles.resource_id=ATESTRESOURCES.RESOURCE_ID
and ATESTuser_roles.user_id = '1401'
I have a feeling I am missing something small but significant, and that I am missing some syntax, so any help is much appreciated.
P.S: I don't know how else to describe the relationship between the tables than the conditions in the query. However, if it is not clear, I can add additional information (don't want to turn this too wordy).
Many thanks!
In Oracle, you can't join multiple tables in a delete like that. One option would be something like
Delete ATESTuser_roles ur
where exists( select 1
from ATESTresource_roles rr
join ATESTRESOURCES r
on rr.resource_id=r.RESOURCE_ID
where ur.role_id = rr.role_id
and r.name='TestName'
and ur.user_id = '1401' )
An alternative solution would be to declare ON DELETE CASCADE in the ATESTuser_roles table. When this is done all you need to do is to run this query:
DELETE FROM ATESTuser_roles
WHERE user_id = '1401'
Because of ON DELETE CASCADE the DB will remove any tuples in ATESTresource_roles where the foreign key (user_id) is equal 1401.
ON DELETE CASCADE example

SQL, How to delete rows from related tables using a query?

I have the following tables
vehicle (veh_num(PK), veh_desc)
log (log_num(PK), veh_num(FK), log_date, log_complaint)
log_line (log_num(PK,FK), logline_num(PK), emp_id(FK), logline_date,logline_action)
part (part_code(PK), logline_num(FK), log_num(FK), part_desc)
signout (signout_num(PK), part_code(FK), emp_id(FK), log_num(FK), signout_date)
I want to run a query which will delete all the records in the vehicle table with for instance, veh_num = "EK458" and also delete rows which are related to the veh_num in the other tables.
I have started with the following query,
DELETE FROM signout WHERE EXISTS
(select * from vehicle,log,log_line,part
where
vehicle.veh_num = 'EK458' AND
vehicle.veh_num = log.veh_num AND
log.log_num = log_line.log_num AND
log_line.log_num = part.log_num AND
part.part_code = signout.part_code);
This query deletes all the associated values of veh_num = "EK458" in the signout table, however, I want a query which will delete the rows from the all the tables which are related to veh_num.
Thanks in advance
I think what you want to do is having the delete cascade into other tables.
Take a look at this How do I use cascade delete with SQL Server?
Read this link http://beginner-sql-tutorial.com/sql-integrity-constraints.htm
and have you tried join instead of subquery in query?

Delete entire row across two tables (inner join?)

I'm trying to delete a row of data across two tables. I have the following code which isn't working:
DELETE
FROM PROCESS_OWNER.ARTIFACTS
JOIN PROCESS_OWNER.ARTIFACT_METADATA
ON ARTIFACTS.ARTIFACT_ID = ARTIFACT_METADATA.ARTIFACT_ID
WHERE ARTIFACT_LABEL = 'getTest'
I get the error message:
"SQL command not properly ended"
Would really appreciate some help as I am struggling to get to grips with Oracle.
You can't do it with a join. DELETE FROM has to target a single table. You could do this:
DELETE FROM PROCESS_OWNER.ARTIFACT_METADATA WHERE ARTIFACT_ID = (SELECT ARTIFACT_ID FROM PROCESS_OWNER.ARTIFACTS WHERE ARTIFACT_LABEL = 'getTest');
DELETE FROM PROCESS_OWNER.ARTIFACTS WHERE ARTIFACT_LABEL = 'getTest';