How to get delete query to work in MS Access - sql

I have set up a subquery to select my records and then the delete query to perform the action. When I run it, I get an error message saying:
Could not delete from specified tables
Here my SQL code in the delete query:
PARAMETERS UnitID Short;
DELETE DISTINCTROW qry_exp_comments_select.*
FROM qry_exp_comments_select;
And the called subquery:
PARAMETERS UnitID Short;
SELECT tbl_Comments.*, tbl_Activity.ActivityID
FROM tbl_BusUnits INNER JOIN (tbl_Activity INNER JOIN tbl_Comments ON tbl_Activity.ActivityID = tbl_Comments.ActivityID) ON tbl_BusUnits.UnitID = tbl_Activity.UnitID
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));
Why won't the query work? I have tried to set it up as the one in the following thread: MS ACCESS delete query syntax combined with inner join problems

In order to delete all the records in tbl_Comments and tbl_Activity all I had to do was change the relationships of the tables so that they have Cascade Delete activated.
Afterwards a simple delete query on the tbl_BusUnits also deleted all associated records in the other tables.
Code for the Delete Query as follows:
PARAMETERS UnitID Short;
DELETE DISTINCTROW tbl_BusUnits.UnitID
FROM tbl_BusUnits
WHERE (((tbl_BusUnits.UnitID)<>[UnitID]));

Related

Access delete query with an inner join and subquery

I am trying to run a delete query that contains a subquery and an inner join and get the error
Query must have at least one destination field.
I have read elsewhere that you must use either of the following.
DISTINCTROW
WHERE EXISTS
But I still get the error message. I have read How to delete in MS Access when using JOIN's? and MS ACCESS delete query syntax combined with inner join problems but can't get it to work.
Here is my query.
DELETE FROM tblUp
WHERE NOT EXISTS (
SELECT tblUp.request_id,
tblUp.gid,
MAX(tblUp.savings_year) AS MaxOfsavings_year,
LAST(tblUp.savings_month) AS LastOfsavings_month
FROM tblUp
INNER JOIN tbl_w ON tblUp.request_id = tblW.id
GROUP BY tblUp.request_id,
tblUp.gid,
tblW.Cde
HAVING (((tblW.Cde)="ML"))
ORDER BY tblUp.request_id,
tblUp.gid,
MAX(tblUp.savings_year),
LAST(tblUp.savings_month)
)

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

Concatenating fields

This is an MS Access (2010) script.
I am trying to concatenate 2 fields of a single table for 2 tables. Then I want to delete the associated record in one of the table if the concatenated field is equal in both tables (means this is a duplicate).
I know how to do that in VBA by looping through the records but I want to do that in SQL since the tables may quickly hold more than 50000 records which means the loop would go 2,500,000,000 times.
I though I could create a 2 SELECT statement in order to create the concatenated fields for both tables. The SELECT Statements will also display the ID of the underlying tables. Then I would delete the record in the appropriate table using the ID.
These are my Select statements:
SELECT [Tick] & [Div_ex_date] AS Expr2, tblBbgDivData.ID
FROM tblBbgDivData
GROUP BY [Tick] & [Div_ex_date], tblBbgDivData.ID;
And
SELECT [Security_Name] & [Div_ex_date] AS Expr1, tblArchiveBbgDivData.ID
FROM tblArchiveBbgDivData
GROUP BY [Security_Name] & [Div_ex_date], tblArchiveBbgDivData.ID;
This is my DELETE Statement:
DELETE tblArchiveBbgDivData.*
FROM (tblArchiveBbgDivData
INNER JOIN qselUniqueID_Archive ON tblArchiveBbgDivData.ID = qselUniqueID_Archive.ID)
INNER JOIN qselUniqueID_BbgDiv ON qselUniqueID_Archive.Expr1 = qselUniqueID_BbgDiv.Expr2
WHERE (((tblArchiveBbgDivData.ID)=[qselUniqueID_Archive].[ID])
AND ((qselUniqueID_Archive.Expr1)=[qselUniqueID_BbgDiv].[Expr2]));
When I hit Datasheet view, the relevant records are displayed but when I hit Run I get "Could not delete from specified tables". Any idea how I can change that?
Access does not work well with JOINs in a DELETE statement. You may be better off with an IN:
DELETE tblArchiveBbgDivData.*
FROM (tblArchiveBbgDivData
WHERE tblArchiveBbgDivData.ID IN
(SELECT qselUniqueID_Archive.ID
FROM qselUniqueID_Archive )
INNER JOIN qselUniqueID_BbgDiv
ON qselUniqueID_Archive.Expr1 = qselUniqueID_BbgDiv.Expr2
);
Note that your WHERE is redundant because you use the same expression in your JOIN syntax.

Avoiding join in MS Access delete 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
)