How to set the default value of an Access 2003 field using SQL?

依然范特西╮ 提交于 2020-01-21 12:09:10

问题


How can I set the default value for a field using SQL in MS Access?

I tried this but got a syntax error:

CREATE TABLE HELLO
( MUN INTEGER  NOT NULL,
ADD   CHAR(50) DEFAULT'16 asd ST.'
)

回答1:


The word ADD is a keyword. Try this:

CREATE TABLE HELLO
( 
    MUN INTEGER  NOT NULL,
    [ADD] CHAR(50) DEFAULT '16 asd ST.'
)



回答2:


The DEFAULT and CHAR keywords are only supported when in the ACE/Jet engine's ANSI-92 Query Mode (and then only in SQL DDL). As Jose Basilio points out, ADD is a reserved word and must be escaped using square brackets. Also, you need a space between the DEFAULT word and its clause (as Jose has shown).

If you are executing the SQL in a Query object in the MS Access interface you will need to change from the default (ANSI-89 Query Mode) to ANSI-92 Query Mode. See: About ANSI SQL query mode.

If you are creating the table programmatically e.g. you are using DAO then try using a CurrentProject.Connection.Execute "Sql goes here" where CurrentProject.Connection is an ADO classic or other OLE DB connection to your data source.

P.S. Surely you wanted you column to be HELLO.Mum :)



来源:https://stackoverflow.com/questions/792059/how-to-set-the-default-value-of-an-access-2003-field-using-sql

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