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);
Related
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'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]
)
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?
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"