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]
)
Related
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);
I have a table and a query, qryAktuelleSchülerbildercontains some of the records in tblSchülerbilder. My goal is to delete all records from tblSchülerbilder which do not exist in qryAktuelleSchülerbilder.
I can get those records with
SELECT tblSchülerbilder.*, qryAktuelleSchülerbilder.ID
FROM tblSchülerbilder
LEFT JOIN qryAktuelleSchülerbilder
ON tblSchülerbilder.ID = qryAktuelleSchülerbilder.ID
WHERE qryAktuelleSchülerbilder.ID Is Null;
When I now change over to a delete-query, the design-view in Access looks fine (I don't really know, what it does to the SQL-view at this point), it shows tblSchülerbilder.* with Delete: From and ID of qryAktuelleSchülerbilder with Delete: Where and Criteria: Is Null. But when I execute the query, it gives me Specify the table containing the records you want to delete. (Error 3128).
I don't know where to put tblSchülerbilder again, since it is already set as delete from. The online article is of no help, since I have already specified tblSchülerbilder.* in the select statement.
You can do it with NOT IN:
DELETE FROM tblSchülerbilder
WHERE ID NOT IN (SELECT ID FROM qryAktuelleSchülerbilder)
or with NOT EXISTS:
DELETE FROM tblSchülerbilder AS t
WHERE NOT EXISTS (
SELECT 1 FROM qryAktuelleSchülerbilder AS q
WHERE q.ID = s.ID
)
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"));
);
I am trying to create a delete query to remove records from one table, based on whether or not one of the field exists in another master table. The situation is that I am importing new records into a database, but I want to remove the records that have already been imported, i.e. that already have an account in the master table. The field I need to join on, however is not equal: it is prefixed with a constant three letter code XYZ.
tbl_to_import.Account master_table.Account
123456 XYZ.123456
345678 XYZ.345678
To avoid using a join in the delete query I tried the following:
Delete tbl_to_import.*
From tbl_to_import
Where Exists( Select master_table.Account From master_table
Where master_table.Account = ("XYZ."& tbl_to_import.Account) ) = True;
However, the query gets hung up in Access. I'm not sure what I'm doing incorrectly. I don't get an error message, but the query runs without producing anything and I eventually stop it. In this situation, tbl_to_import has 2,700 records and master_table has 50,000 records. Additionally, I am connecting to the master_table via ODBC.
Originally, I constructed two queries using a join to perform the delete. tbl_to_import.Account has a primary key called ID. One query, qry_find_existing_accounts, located the ID numbers in tbl_to_import for which there exists a corresponding account in master_table.Account:
SELECT DISTINCTROW tbl_to_import.ID AS DELETEID
FROM tbl_to_import LEFT JOIN master_table
ON ("XYZ."& tbl_to_import.Account) = master_table.Account
WHERE ((("XYZ." & [Account])=[master_table].[Account]));
Then I used this query to construct the delete query:
DELETE DISTINCTROW tbl_to_import.*, tbl_to_import.ID
FROM tbl_to_import RIGHT JOIN qry_find_existing_accounts
ON tbl_to_import.ID =qry_find_existing_accounts.DELETEID
WHERE (((tbl_to_import.ID)=[qry_find_existing_accounts].[DELETEID]));
The query qry_find_existing_accounts worked fine; however, when I tried to run the second query to delete, I got the error: Could not delete from specified tables. Typically, when I get this error, it is because I have not selected unique records only, however, I used DISTINCTROW in both queries.
Any ideas what I am doing wrong and how I can accomplish what I need to do?
I'd go with a simpler nested SQL statement:
Delete tbl_to_import.*
From tbl_to_import
Where "XYZ." & tbl_to_import.Account In
(Select master_table.Account From master_table);
This should be fairly fast, especially if your Account fields are indexed.
I think you can simplify the query; delete based on the ID, where the IDs are in the query:
DELETE * FROM tbl_to_import
WHERE tbl_to_import.ID IN (
SELECT DISTINCT [DELETED] FROM qry_find_existing_accounts
)
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"