Delete Query with Conditions from Multiple Tables: Access 2010 - sql

I am trying to delete records from the "AC_DETAIL" Table that meet a certain criteria in the "AC_PROPERTY" Table; however, when I run the query I get the error "Could not delete from specified tables." I am using MS Access 2010 and fairly new to it. Anyone have an idea why this is wrong and how it can be fixed to work?
DELETE
AC_DETAIL.*
FROM AC_PROPERTY
INNER JOIN AC_DETAIL
ON (AC_PROPERTY.YEAR = AC_DETAIL.YEAR)
AND (AC_PROPERTY.NUM = AC_DETAIL.NUM)
WHERE (((AC_PROPERTY.YEAR)="2015")
AND ((AC_PROPERTY.REGION)="CT")
AND ((AC_DETAIL.SOURCE)="BPF")
AND ((AC_DETAIL.SCENARIO)="PRICES")
AND ((AC_PROPERTY.STATUS)="NO-PROD"));

You have mixed the DELETE and SELECT syntaxes, you can't explicitly join tables in a DELETE statement.
With MS Access 2000 I think you could use a workaround like that (I can't test it right now, so I'm not sure it's going to work):
DELETE AC_DETAIL.* FROM AC_DETAIL WHERE EXISTS (
SELECT 1 FROM AC_PROPERTY WHERE
AC_PROPERTY.YEAR = AC_DETAIL.YEAR AND
AC_PROPERTY.NUM = AC_DETAIL.NUM AND
AC_PROPERTY.YEAR = "2015" AND
AC_PROPERTY.REGION = "CT" AND
AC_DETAIL.SOURCE = "BPF" AND
AC_DETAIL.SCENARIO = "PRICES" AND
AC_PROPERTY.STATUS = "NO-PROD"
);
EDIT : If you have a field NUM as primary key in AC_DETAIL table then something like that should work:
DELETE FROM AC_DETAIL WHERE NUM IN (
SELECT AC_DETAIL.NUM
FROM AC_DETAIL
INNER JOIN AC_PROPERTY ON (AC_PROPERTY.YEAR = AC_DETAIL.YEAR) AND (AC_PROPERTY.NUM = AC_DETAIL.NUM)
WHERE (((AC_PROPERTY.YEAR)="2015")
AND ((AC_PROPERTY.REGION)="CT")
AND ((AC_DETAIL.SOURCE)="BPF")
AND ((AC_DETAIL.SCENARIO)="PRICES")
AND ((AC_PROPERTY.STATUS)="NO-PROD"));
);

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

Creating a query which checks for existence in one table from another based on multiple fields

I've created a table (tblUploadCompletions) which is used to paste a temporary dataset into. I then want to run a query against this table to see if certain fields already exist in another table. If they do, the query will not include them. If they don't, then the query will be used as the basis of some INSERT INTO code that I'll write later.
The code I'm currently running which works for identifying the existence of one field ("Ref") is:
SELECT tblUploadCompletions.case_id, tblUploadCompletions.ref, dim_brand.brand_prod_id, tblUploadCompletions.date_on_correspondance, tblUploadCompletions.action_completed_date, Dim_users.User_ID, tblUploadCompletions.action_type
FROM (tblUploadCompletions INNER JOIN Dim_users ON tblUploadCompletions.completed_by = Dim_users.user_name) INNER JOIN dim_brand ON (tblUploadCompletions.brand_01 = dim_brand.brand) AND (tblUploadCompletions.product_02 = dim_brand.product_02) AND (tblUploadCompletions.workstream = dim_brand.Workstream)
WHERE (((Exists
(SELECT 1
FROM all_actions
WHERE all_actions.ref = tblUploadCompletions.ref))=False) AND ((tblUploadCompletions.action_type)='File Request'));
I've tried a couple of methods to amend the "WHERE (((Exists" portion of the code to include an additional field but not had much luck. The additional field I'd need to match against is in another table so would need to include something like this in the sub query:
WHERE (((Exists
(SELECT 1
FROM all_actions INNER JOIN Dim_brands on all_actions.brand_prod_id = dim_brands.brand_prod_id
WHERE all_actions.ref = tblUploadCompletions.ref and dim_brands.stage = 'File Request'))=False) AND ((tblUploadCompletions.action_type)='File Request'));
Thanks in advance
You can try to use the code below
SELECT * FROM tblUploadCompletions
WHERE NOT EXISTS
(
SELECT 1 FROM tblSource WHERE tblSource .Id = tblUploadCompletions.Id
)

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

Acces key violations in append query

I'm trying to run an append query, but I keep getting key violations, I think DelledningID is the culprit, but not sure how to fix this, as I shouldn't be touching that or ProjektID.
INSERT INTO ProjektDelledning ( ProjektID, DelledningID, SaneringsmetKode, DatoOprettet, DatoOpdateret )
SELECT ProjektDelledning.ProjektID, ProjektDelledning.DelledningID, NySaneringsData.SaneringsmetodeKode AS SaneringsmetKode, IIf([ProjektDelledning].[DatoOprettet] Is Null,Date$()+" "+Time$(),[ProjektDelledning].[DatoOprettet]) AS DatoOprettet, IIf([ProjektDelledning].[SaneringsmetKode] Is Null,Date$()+" "+Time$(),[ProjektDelledning].[DatoOpdateret]) AS DatoOpdateret
FROM NySaneringsData LEFT JOIN ProjektDelledning ON NySaneringsData.DelledningsID = ProjektDelledning.DelledningID
WHERE (((Exists (SELECT * FROM ProjektDelledning WHERE ProjektDelledning.SaneringsmetKode = NySaneringsData.SaneringsmetodeKode AND ProjektDelledning.DelledningID = NySaneringsData.DelledningsID))=False));
How do I got about fixing this? I tried removing Projekt ID and DelledningID from the query, but that gives a validations error instead.
You are approaching this incorrectly. You cannot insert new records into the ProjektDelledning without a "valid" project_id. You don't have a valid project_id and you cannot steal it from existing records because you would then create duplicate records and hit the validation error.
assuming your NySaneringsData has only unique delledningid,
try updating the ProjektDelledning table. something like.
update
ProjektDelledning INNER JOIN ProjektDelledning ON NySaneringsData.DelledningsID = ProjektDelledning.DelledningID
set
ProjektDelledning.SaneringsmetKode = NySaneringsData.SaneringsmetKode ,
ProjektDelledning.DatoOprettet = nz(ProjektDelledning.DatoOprettet, now()),
ProjektDelledning.DatoOpdateret = nz(ProjektDelledning.DatoOpdateret, now())
WHERE ( if any further conditions needed);
if NySaneringsData has duplicate delledningsid then you need to makesure which record you want to take over.

How to avoid the "Cannot delete from specified tables." in MS Access

Here is the code that I am trying to run:
DELETE DISTINCTROW JHALL_REFERAL_ASSIGNMENTS.emp_id, JHALL_REFERAL_ASSIGNMENTS.ref_elem_id
FROM JHALL_REFERAL_ASSIGNMENTS
WHERE (((JHALL_REFERAL_ASSIGNMENTS.emp_id)=(select b.emp_id from JHALL_REFERAL_ELEMENT a, JHALL_REFERAL_ASSIGNMENTS b, BSI_MARTS_D_EMPLOYEE c
where C.FULL_NM = 'Employee'
and A.REF_NAME ='Max Premium of 5,000'
and A.REF_ELEM_ID = B.REF_ELEM_ID
and B.emp_id = C.EMPLOYEE_KEY
)) AND ((JHALL_REFERAL_ASSIGNMENTS.ref_elem_id)=(select a.ref_elem_id from JHALL_REFERAL_ELEMENT a, JHALL_REFERAL_ASSIGNMENTS b, BSI_MARTS_D_EMPLOYEE c
where C.FULL_NM = 'Employee'
and A.REF_NAME ='Max Premium of 5,000'
and A.REF_ELEM_ID = B.REF_ELEM_ID
and B.emp_id = C.EMPLOYEE_KEY
)));
Every time I try to run this in Access I get error 3086, "Cannot delete from specified tables." When trying to find information online I keep running into resolutions saying I should change the Unique Records field to "Yes" which I did but that did not solve my issue. I ran the same code (separating schema and table names with . instead of _) in Toad and it worked fine.
I reviewed several posts, including this one, to muddle through a similar delete. I used a query to distill the somewhat complex selection criteria down to a set of primary keys for the table targeted for record deletion.
I got the "Could not delete from specified tables" error and the "Specify the table containing the records you want to delete" error until I used:
delete distinctrow [Target_Table].*
from [Target_Table]
inner join [Criteria_Query]
on [Criteria_Query].Index_PK = [Target_Table].Index_PK
where ( [Criteria_Query].Index_PK = [Target_Table].Index_PK )
;
This worked in Access 2013.
This is really an infuriating problem as even the code below, quoted from above, results in a "Cannot delete from specified tables" error, if Criteria_Query is actually a query and not table.
delete distinctrow [Target_Table].*
from [Target_Table]
inner join [Criteria_Query]
on [Criteria_Query].Index_PK = [Target_Table].Index_PK
where ( [Criteria_Query].Index_PK = [Target_Table].Index_PK )
;
the solution is to first select the results of Criteria_Query into a table, tbl_Criteria_Query, and then use the table in the delete statement:
select *
into [tbl_Criteria_Query]
from [Criteria_Query]
;
delete distinctrow [Target_Table].*
from [Target_Table]
inner join [tbl_Criteria_Query]
on [tbl_Criteria_Query].Index_PK = [Target_Table].Index_PK
;
Select the IDs in a subquery and run delete on the table using in
Delete * from
tbl_Delete
Where ID in (
Select id form table1
left join ..
left join ...
)
Just Run The Visual Studio (in Run As Administrator Mode).
I had the same error when using an MS Access front end and SQL server back end. I found that if I make my primary keys the same in SQL as in the local table in Access the problem was solved.
The following has worked for me designview->propertysheet->General->set unique records to "yes"