MS Access 是否会抑制插入时的主键违规?
我正在将 MS Access 数据库重写为 SQL Server,并在 Access 中发现了一个奇怪的问题,我希望有人可以帮助解决。
我有一个表,我们将其称为“主”表,其中帐户上的主键已编入索引且不允许重复。看起来很简单,但我的问题是在插入数据时发生的。
我的 INSERT 查询是(为了简洁起见,字段数量受到限制)
INSERT INTO Main (Account, SentDate, Amount)
SELECT C.Account, C.SentDate, C.Amount
FROM
(CALLS C LEFT JOIN Bals B ON C.Account = B.ACCT_ID)
LEFT JOIN AggAnt A ON C.Account = A.Account
问题是这样的,如果我运行查询的 SELECT
部分,我会得到 2365 条记录,但是当我运行 INSERT< /code> 我得到 2364 条记录。所以我做了一些检查,发现一个帐户重复,记录之间的差异是发送日期和金额。但 Access 仅插入一条记录,并且不会抛出任何类型的错误消息或任何内容。查询中没有任何内容表明选择最近的日期等。
示例数据:
Account SentDate Amount
12345678 8/1/2011 123.00
23456789 8/1/2011 45678.00
34567890 8/1/2011 7850.00
45678912 8/1/2011 635.00
45678912 5/1/2011 982.00
56789123 8/1/2011 2639.00
在示例中,当我运行 INSERT 时,我有一个重复的帐户 45678912,我没有收到任何错误,并且从 8/1/ 获取记录2011年。
当这违反了表上的 PK 时,为什么 Access 不会抛出错误? Access 中是否存在选择一条记录并跳过另一条记录的怪癖?
我完全被这个问题难住了,所以任何帮助都会很棒。
I am in the process of re-writing an MS Access database to SQL server and have found an strange issue in Access that I am hoping someone can help with.
I have a table let's call it 'Main' with a Primary Key on the Account that is indexed and doesn't allow for duplicates. Seems simple enough but my issue is occurring when data is getting Inserted.
My INSERT query is (the number of fields have been limited for brevity)
INSERT INTO Main (Account, SentDate, Amount)
SELECT C.Account, C.SentDate, C.Amount
FROM
(CALLS C LEFT JOIN Bals B ON C.Account = B.ACCT_ID)
LEFT JOIN AggAnt A ON C.Account = A.Account
The issue is this, if I run the SELECT
portion of my query I get 2365 records but when I run the INSERT
I get 2364 records. So I did some checking and I found one Account is duplicated the difference between the records is the SentDate and the Amount. But Access is inserting only one of the records and not throwing any kind of error message or anything. There is nothing in the query that says select the most recent date, etc.
Sample Data:
Account SentDate Amount
12345678 8/1/2011 123.00
23456789 8/1/2011 45678.00
34567890 8/1/2011 7850.00
45678912 8/1/2011 635.00
45678912 5/1/2011 982.00
56789123 8/1/2011 2639.00
In the sample I have one account that is duplicated 45678912 when I run my INSERT, I get no errors and I get the record from 8/1/2011.
Why is Access not throwing an error when this violates the PK on the table? Is there some quirk in Access to select one record and just skip the other?
I am totally stumped by this issue so any help would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您如何运行查询?如果您使用的是
DoCmd.RunSQL
,请切换到使用 DAO 数据库对象的.Execute
方法,并使用dbFailOnError
。编辑:如果 Main 是指向 SQL Server 表的 ODBC 链接,我将检查
db.Execute strInsert、dbFailOnError
之后的错误收集 (DAO)How are you running the query? If you're using
DoCmd.RunSQL
, switch to using the.Execute
method of a DAO database object, and usedbFailOnError
.Edit: If Main is an ODBC link to a SQL Server table, I would examine the Errors Collection (DAO) after
db.Execute strInsert, dbFailOnError
在 HansUp 指示我检查 SetWarnings = false 的方向之后。我发现它埋在我的代码中,这就是为什么没有关于由于主键违规而未插入记录的警告消息。
需要注意的是,请确保您希望抑制这些消息。
After HansUp pointing me in the direction of checking for SetWarnings = false. I found it buried in my code which is why there was no warning message about the records not being inserted due to primary key violations.
A word of caution would be to make sure you want these messages suppressed.
是的,您可以在引擎级别控制此行为(如果使用 OLE DB,也可以在记录集级别)。
对于 OLE DB(例如 ADO),设置为
Jet OLEDB:Global Partial Bulk Ops< /代码>
:
请注意,默认情况下不允许部分完成批量操作。
Yes, you can control this behaviour at the engine level (also at the recordset level if using OLE DB).
For OLE DB (e.g. ADO) the setting is
Jet OLEDB:Global Partial Bulk Ops
:Note the default is to allow no partial completion of bulk operations.