Delete entire row across two tables (inner join?) - sql

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';

Related

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]
)

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?

How to delete a common row from multiple tables using 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;

Error in merging two tables using Oracle10g?

I am using oracle 10g. I tried to merge two tables. At that time i got the following error...
ora-30926 unable to get a stable set of rows in the source tables.
The following is my query
merge into mt_test_dest t
using (select distinct d.dest_id dest_id,
d.c_id,
nvl(tt.destination, tt.destination) destination
from my_dest_extra d
join mt_test_dest tt
on d.c_id = tt.dest_cid
join my_dest dml
on dml.dest_id = d.dest_id
where tt.effectivedate <> to_date('12-12-2999', 'dd-mm-yyyy')) src
on (t.dest_cid = src.c_id)
when matched then
update set t.dest_id = src.dest_id, t.destination = src.destination;
Can someone help me out in this issue?
The most likely cause for the error is that your source query contains multiple rows with the same C_ID. If this happens, you have two or more rows competing to update the same data in the target table (since this column is used as the only join condition). Oracle detects this and throws an ORA-30926 error.

Trying SQL table update matching on string field

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.
I only have a string field that matches between the two tables.
tblJP AND tblMaster
I need to update tblJP.LangString with tblMaster.Long_text when
tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'
Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.
A simple update with an INNER JOIN should do the trick.
UPDATE tblJP
SET tblJP.LangString = tblMaster.Long_Text
FROM tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE tblMaster.Lang = 'jp'
WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.
You could also use MERGE
MERGE INTO tblJP
USING (SELECT *
FROM tblMaster
WHERE Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
UPDATE SET LangString = SOURCE.Long_Text;
In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.