在Access中使用自动编号-INSERT语句 [英] Using Autonumbering in Access - INSERT statements

查看:233
本文介绍了在Access中使用自动编号-INSERT语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行INSERT语句时遇到麻烦,其中有一个自动编号作为PK字段.我有一个自动递增long作为主键,然后有4个类型为double的字段;但是Access(使用ADO)似乎需要insert语句有五个值.

I'm having trouble running an INSERT statement where there's an autonumber as the PK field. I have an Auto-incrementing long as the Primary Key, and then 4 fields of type double; and yet Access (using ADO) seems to want five values for the insert statement.

INSERT INTO [MY_TABLE] VALUES (1.0, 2.0, 3.0, 4.0);
>> Error: Number of query values and destinations fields are not the same.

INSERT INTO [MY_TABLE] VALUE (1, 1.0, 2.0, 3.0, 4.0);
>> Success!!

我如何使用自动编号来自动编号?

How do I use Autonumbering to actually autonumber?

推荐答案

如果不想为表中存在的所有列提供值,则必须指定要插入的列. (这是合乎逻辑的,否则访问或任何其他数据库应该如何知道您要为哪些列提供值)?

If you do not want to provide values for all columns that exists in your table, you've to specify the columns that you want to insert. (Which is logical, otherwise how should access, or any other DB, know for which columns you're providing a value)?

所以,您要做的是:

INSERT INTO MyTable ( Column2, Column3, Column4) VALUES ( 1, 2, 3 )

此外,请确保省略主键"列(这是自动编号字段). 然后,Access会自行将其设置为下一个值.

Also , be sure that you omit the Primary Key column (which is the autonumber field). Then, Access will set it to the next value by itself.

然后您可以通过执行

SELECT @@identity FROM MyTable

声明.

这篇关于在Access中使用自动编号-INSERT语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆