如何避免“无法从指定的表中删除”。在MS Access中

时间:2012-07-09 19:55:31

标签: sql ms-access runtime-error sql-delete

以下是我尝试运行的代码:

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

每次我尝试在Access中运行它时,都会收到错误3086,“无法从指定的表中删除”。当试图在线查找信息时,我一直在遇到决议,说我应该将“唯一记录”字段更改为“是”,但我没有解决我的问题。我在Toad中运行了相同的代码(将模式和表名与.分隔,而不是_),并且运行正常。

7 个答案:

答案 0 :(得分:8)

我回顾了几个帖子,包括这个帖子,以扼杀类似的删除。我使用查询将有些复杂的选择标准提炼为一组主键,用于记录删除的目标表。

我得到了#34;无法从指定的表中删除"错误和"指定包含您要删除的记录的表格#34;错误,直到我使用:

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

这在Access 2013中有效。

答案 1 :(得分:3)

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
;

答案 2 :(得分:3)

在子查询中选择ID,然后使用

在表上运行delete
Delete * from 
tbl_Delete

Where ID in (

Select id form table1
left join ..
left join ...

)

答案 3 :(得分:0)

我知道这是旧的,但可能有人会发现这个link有用

以下内容对我有用designview->propertysheet->General->set unique records to "yes"

答案 4 :(得分:-1)

由于您的表的主键是自动编号,我建议您阅读此链接以了解如何重置它。 http://support.microsoft.com/kb/812718

答案 5 :(得分:-1)

只需运行Visual Studio(在管理员模式下运行)。

答案 6 :(得分:-2)

使用MS Access前端和SQL Server后端时出现相同的错误。我发现如果我在SQL中使主键与在Access中的本地表中相同,则问题就解决了。