SQL SET DEFAULT not working in MS Access

╄→гoц情女王★ 提交于 2019-12-17 14:04:25

问题


Possible Duplicate:
DEFAULT clause in ALTER TABLE statement resulting in syntax error

I am trying to execute the following statement using a SQL query within MS Access;

ALTER TABLE [table] ALTER COLUMN [column] SET DEFAULT 'default value'

However, I get a dialog displaying the error Syntax error in ALTER TABLE statement.

And when I click OK it highlights the word DEFAULT. I also tried the following statement;

ALTER TABLE [table]
ADD CONSTRAINT [Default] DEFAULT 'default value' FOR [column]

And I get another error Syntax error in CONSTRAINT clause.

What is the correct syntax for setting a default value in MS Access? The db file is Access 2003 format.


回答1:


Support for DEFAULT was included in Access DDL with Jet 4 (Access 2000). However it can only be used in DDL executed from an ADO connection.

This worked with Access 2007.

CurrentProject.Connection.Execute "ALTER TABLE MyTable " & _
    "ALTER COLUMN field2 SET DEFAULT ""foo"";"

Note if your db file is Access 97 or earlier, you won't be able to set a field DEFAULT value from DDL.




回答2:


It seems, there would be Constraint issue with your column. Although following DDL statement is the correct way.

ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'SANDNES'

Reference



来源:https://stackoverflow.com/questions/14057085/sql-set-default-not-working-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!