简体   繁体   English

在MS Access 2003中使用SQL命令添加列时出错

[英]Error adding column using SQL command in MS Access 2003

I want to add a column to my EMP_2 table. 我想在我的EMP_2表中添加一列。 The new EMP_PCT column will store a percentage as a number, for example 20% as 20.0 . 新的EMP_PCT列将存储百分比作为数字,例如20%作为20.0

I tried to input this method. 我试着输入这个方法。 But it gave a syntax error in the ALTER TABLE statement. 但它在ALTER TABLE语句中给出了语法错误。

Why is it not working? 为什么不起作用? Below is the SQL I am using: 下面是我正在使用的SQL:

ALTER TABLE EMP_2
ADD EMP_PCT NUMBER(4,2);

Access DDL data type names can be challenging to sort out. 访问DDL数据类型名称可能很难理清。 NUMBER actually creates a field as double precision float. NUMBER实际上创建了一个双精度浮点字段。 But if you try to include field size, precision, or scale in parentheses after NUMBER , you will get a syntax error. 但是,如果在NUMBER之后尝试在括号中包括字段大小,精度或比例,则会出现语法错误。 The following statement creates a double field whether you execute it from ADO or DAO: 无论您是从ADO还是DAO执行,以下语句都会创建一个双字段:

ALTER TABLE MyTable ADD COLUMN EMP_PCT NUMBER;

The next statement adds a decimal data type column to MyTable with precision = 4 and scale = 2, which means it will hold up to 2 digits to the left of the decimal point and 2 to the right. 下一个语句将一个十进制数据类型列添加到MyTable其中precision = 4,scale = 2,这意味着它将在小数点左边保留最多2位数,在右边保存2。

CurrentProject.Connection.Execute "ALTER TABLE MyTable ADD COLUMN EMP_PCT DECIMAL (4,2);"

I used CurrentProject.Connection to execute the DDL statement because it is an ADO object, and Access SQL only allows you to create a decimal field with ADO. 我使用CurrentProject.Connection来执行DDL语句,因为它是一个ADO对象,而Access SQL只允许您使用ADO创建一个十进制字段。 The statement will trigger a syntax error if you attempt to execute it from DAO, like with CurrentDb.Execute or from the Access query designer. 如果您尝试从DAO执行该语句,则该语句将触发语法错误,如使用CurrentDb.Execute或从Access查询设计器执​​行。 See Field type reference - names and values for DDL, DAO, and ADOX for more information. 有关详细信息请参阅字段类型引用 - DDL,DAO和ADOX的名称和值

As @ErikE explained, Access' decimal type is problematic, especially with Access 2003 which you're using. 正如@ErikE解释的那样,Access'十进制类型存在问题,特别是对于您正在使用的Access 2003。 Consider whether you might be able to use a currency type field instead. 考虑您是否可以使用货币类型字段。 It avoids decimal buginess and offers faster performance due to the way the db engine handles the fixed number of decimal places. 由于db引擎处理固定小数位数的方式,它避免了十进制错误并提供更快的性能。

First, I recommend that you not use the Decimal data type in Access 2003 because there are bugs with it around sorting (wrong order) and aggregating (sums truncating the fractional portion). 首先,我建议你不要在Access 2003中使用Decimal数据类型,因为它有关于排序(错误的顺序)和聚合(总和截断小数部分)的错误。 In Access 2007 the aggregate problem is solved but not the sorting (though you can supposedly fix this in Access 2007 by putting an index on the column). 在Access 2007中,聚合问题已解决,但不是排序(尽管您可以通过在列上放置索引来解决这个问题)。

As for your script, there are two obvious problems: 至于你的脚本,有两个明显的问题:

  1. You must use ADD COLUMN ColumnName not ADD ColumnName 您必须使用ADD COLUMN ColumnName而不是ADD ColumnName

  2. The correct data type is DECIMAL , because NUMBER creates a double-precision float instead, and doesn't allow parentheses after it specifying any kind of size. 正确的数据类型是DECIMAL ,因为NUMBER会创建一个双精度浮点数,并且在指定任何类型的大小后不允许使用括号。 ( Maybe NUMERIC would work as a synonym of DECIMAL but I don't know.) 也许 NUMERIC可以作为DECIMAL的同义词,但我不知道。)

So this should work for you: 所以这对你有用:

ALTER TABLE EMP_2 ADD COLUMN EMP_PCT DECIMAL(4,2);

According to HansUp and other sources, this cannot be submitted through DAO (as in CurrentDb.Execute ) but must be done via ADO ( CurrentProject.Connection.Execute ). 根据HansUp和其他消息来源,这不能通过DAO提交(如在CurrentDb.Execute ),但必须通过ADO( CurrentProject.Connection.Execute )完成。

Apparently, there is a way to get this SQL working but it requires a database settings change: 显然,有一种方法可以使这个SQL工作,但它需要更改数据库设置:

The decimal data type isn't supported in the default Jet 4.0 mdb file. 默认的Jet 4.0 mdb文件不支持十进制数据类型。 You have to use the SQL Server compatibility syntax (ANSI 92) setting to use the decimal data type in the SQL Window. 您必须使用SQL Server兼容性语法(ANSI 92)设置在SQL窗口中使用十进制数据类型。

Click on the menu, Tools > Options. 单击菜单,工具>选项。 Click on the Tables/Query tab. 单击“表/查询”选项卡。 Mark the check box for "This database" in the SQL Server compatibility syntax (ANSI 92) section. 在SQL Server兼容性语法(ANSI 92)部分中选中“此数据库”复选框。 This mode will affect the entire db, including queries with wildcards, so you may want to try this on a copy of your db. 此模式将影响整个数据库,包括带有通配符的查询,因此您可能希望在数据库的副本上尝试此操作。

If you still cannot get things working, you might consider this method ( thanks to Philippe Grondier ): 如果你仍然无法正常工作,你可以考虑这种方法( 感谢Philippe Grondier ):

Dim TD As DAO.TableDef
Dim F As DAO.Field

Set TD = CurrentDb.TableDefs("TableName")
Set F = TD.CreateField("FieldName", dbDecimal, 4)
F.DecimalPlaces = 2
F.DefaultValue = 0

TD.Fields.Append F

For reference, here are some related Microsoft.com help pages: 作为参考,以下是一些相关的Microsoft.com帮助页面:

try this, 试试这个,

ALTER TABLE EMP_2 
ADD COLUMN EMP_PCT NUMBER(4,2);

Or else try using this URL, 或者尝试使用此URL,

AddingFields AddingFields

试试这个

   ALTER TABLE EMP_2 ADD (EMP_PCT NUMBER (4,2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM