在列中插入默认值(访问数据库)

发布于 2024-07-09 20:10:48 字数 373 浏览 7 评论 0原文

如何在 Access 中的表的列中插入默认值? 我使用这个指令:

ALTER TABLE Tabella ADD Campo Double DEFAULT 0
ALTER TABLE Tabella ADD Campo Double DEFAULT (0)
ALTER TABLE Tabella ADD Campo DEFAULT 0
ALTER TABLE Tabella ADD Campo DEFAULT (0)
ALTER TABLE Tabella ADD Campo SET DEFAULT 0
ALTER TABLE Tabella ADD Campo SET DEFAULT (0)

但所有这些都会导致错误。 我该怎么做才能做到这一点?

How can I do for inserting a Default Value into a column of a table in Access?
I use this instruction:

ALTER TABLE Tabella ADD Campo Double DEFAULT 0
ALTER TABLE Tabella ADD Campo Double DEFAULT (0)
ALTER TABLE Tabella ADD Campo DEFAULT 0
ALTER TABLE Tabella ADD Campo DEFAULT (0)
ALTER TABLE Tabella ADD Campo SET DEFAULT 0
ALTER TABLE Tabella ADD Campo SET DEFAULT (0)

but all of these cause error. How can I do for doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

究竟谁懂我的在乎 2024-07-16 20:10:48

来自 MSDN

可以执行DEFAULT语句
只能通过Access OLE DB
提供商和 ADO。 它将返回一个
如果通过使用错误消息
访问 SQL View 用户界面。

Sooo...告诉我们更多关于您正在做什么以及如何执行 SQL 的信息?

为什么不使用表格设计器?

From MSDN:

The DEFAULT statement can be executed
only through the Access OLE DB
provider and ADO. It will return an
error message if used through the
Access SQL View user interface.

Sooo... tell us more about what you're doing and how you're executing your SQL?

And why aren't you using the table designer?

來不及說愛妳 2024-07-16 20:10:48

您不需要插入新的默认值,您需要更改现有的默认值。

首先将 Access 切换到正常的 SQL 风格,如上面链接的答案所示,然后你可以说:

alter table Tabella alter column Campo Double DEFAULT 0

You do not insert a new default value, you need to alter the existing one.

First switch Access to a sane SQL flavour, as in the answer linked above, then you can say:

alter table Tabella alter column Campo Double DEFAULT 0
丶视觉 2024-07-16 20:10:48

我有这个典型的 Access 函数来向 Access 表添加新字段+默认值:。 如果表中尚不存在该字段,则会添加该字段。

Public Function addNewField( _
    m_tableName as string, _
    m_fieldName As String, _
    m_fieldType As Long, _      'check syntax of .createField method for values'
    m_fieldLength As Long, _    'check syntax of .createField method for values'
    Optional m_defaultValue As Variant = Null)

Dim myTable As DAO.TableDef
Dim myField As DAO.Field

On Error GoTo addNewField_error

Set myTable = currentDb.TableDefs(m_tableName)
Set myField = myTable.CreateField(m_fieldName, m_fieldType, m_fieldLength)

If Not IsNull(m_defaultValue) Then
    myField.DefaultValue = m_defaultValue
End If

myTable.Fields.Append myField

Set myTable = Nothing
Set myField = Nothing

Exit Function

addNewField_error:
If Err.Number = 3191 Or Err.Number = 3211 Then
    'The field already exists or the table is opened'
    'nothing to do but exit the function'
Else
    debug.print Err.Number & " - " & Error$
End If

End Function

I have this typical Access function to add new fields + default values to Access tables:. The field is added if it does not already exists in the table.

Public Function addNewField( _
    m_tableName as string, _
    m_fieldName As String, _
    m_fieldType As Long, _      'check syntax of .createField method for values'
    m_fieldLength As Long, _    'check syntax of .createField method for values'
    Optional m_defaultValue As Variant = Null)

Dim myTable As DAO.TableDef
Dim myField As DAO.Field

On Error GoTo addNewField_error

Set myTable = currentDb.TableDefs(m_tableName)
Set myField = myTable.CreateField(m_fieldName, m_fieldType, m_fieldLength)

If Not IsNull(m_defaultValue) Then
    myField.DefaultValue = m_defaultValue
End If

myTable.Fields.Append myField

Set myTable = Nothing
Set myField = Nothing

Exit Function

addNewField_error:
If Err.Number = 3191 Or Err.Number = 3211 Then
    'The field already exists or the table is opened'
    'nothing to do but exit the function'
Else
    debug.print Err.Number & " - " & Error$
End If

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文