MSAccess 中重命名列的 DDL 是什么?

发布于 2024-08-17 18:05:59 字数 235 浏览 7 评论 0原文

在 MS Access 中重命名列的 DDL 是什么?类似于:

alter table myTable rename col1 to col2

这不适用于 MSAccess 2000 格式数据库。我正在将 OLEDB 或 ADO.NET 与 MSAccess 2000 格式数据库一起使用,但如果能提供有关语法的任何提示或有关如何以其他方式使用 ADO.NET 实现此目的的建议,我将不胜感激。

What is the DDL to rename a column in MS Access? Something along the lines of:

alter table myTable rename col1 to col2

which does not work for MSAccess 2000 format databases. I'm using OLEDB or ADO.NET with a MSAccess 2000 format db but would be grateful of any hint at the syntax or a suggestion as to how to achieve this using ADO.NET in some other way.

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

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

发布评论

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

评论(6

谷夏 2024-08-24 18:05:59

我不相信您可以做到这一点,除非附加新列,从现有列更新,然后删除“旧”列。

然而,在 VBA 中它非常简单:

Set db = CurrentDb
Set fld = db.TableDefs("Table1").Fields("Field1")
fld.Name = "NewName"

I do not believe you can do this, other than by appending a new column, updating from the existing column and then deleting the 'old' column.

It is, however, quite simple in VBA:

Set db = CurrentDb
Set fld = db.TableDefs("Table1").Fields("Field1")
fld.Name = "NewName"
把人绕傻吧 2024-08-24 18:05:59

我在家,目前无法对此进行测试,但我认为这应该可行。此网站有相关信息。

ALTER TABLE thetable ALTER COLUMN fieldname fieldtype

编辑 我对此进行了一些测试,奇怪的是,您无法重命名我可以找到的列。 ALTER COLUMN 语法仅允许更改类型。使用 SQL,似乎有必要删除该列,然后将其添加回来。我想数据可以保存在临时表中。

alter table test drop column i;
alter table test add column j integer;

I am at home and can't test this at the moment, but I think this should work. This site has information about it.

ALTER TABLE thetable ALTER COLUMN fieldname fieldtype

Edit I tested this a bit and oddly enough, you can't rename a column that I can find. The ALTER COLUMN syntax only allows for changing type. Using SQL, it seems to be necessary to drop the column and then add it back in. I suppose the data could be saved in a temporary table.

alter table test drop column i;
alter table test add column j integer;
任谁 2024-08-24 18:05:59

我的解决方案,简单但有效:

Dim tbl as tabledef
set tbl = currentdb.TableDefs("myTable")
tbl.fields("OldName").name = "Newname" 

My solution, simple but effective:

Dim tbl as tabledef
set tbl = currentdb.TableDefs("myTable")
tbl.fields("OldName").name = "Newname" 
二智少女猫性小仙女 2024-08-24 18:05:59

Fionnuala 使用 DDL 提供的答案是

ALTER TABLE [your table] ADD COLUMN [your newcolumn] Text(250)
UPDATE [your table] SET [your table].[newcolumn] = [your table].[old column]
ALTER TABLE [your table] DROP COLUMN [oldcolumn]

注意,显然您可以为新列指定任何列类型,Text(250) 仅用于说明

The answer provided by Fionnuala using DDL is

ALTER TABLE [your table] ADD COLUMN [your newcolumn] Text(250)
UPDATE [your table] SET [your table].[newcolumn] = [your table].[old column]
ALTER TABLE [your table] DROP COLUMN [oldcolumn]

Note that obviously you can specify any column type for your new column, and Text(250) is just for illustration

口干舌燥 2024-08-24 18:05:59

在 VBA 中,您可以执行以下操作来重命名列:

Dim acat As New ADOX.Catalog
Dim atab As ADOX.Table
Dim acol As ADOX.Column
Set acat = New ADOX.Catalog
acat.ActiveConnection = CurrentProject.Connection
Set atab = acat.Tables("yourTable")
For Each acol In atab.Columns
    If StrComp(acol.Name, "oldName", vbTextCompare) = 0 Then
        acol.Name = "newName"
        Exit For
    End If
Next acol

In VBA you can do this to rename a column:

Dim acat As New ADOX.Catalog
Dim atab As ADOX.Table
Dim acol As ADOX.Column
Set acat = New ADOX.Catalog
acat.ActiveConnection = CurrentProject.Connection
Set atab = acat.Tables("yourTable")
For Each acol In atab.Columns
    If StrComp(acol.Name, "oldName", vbTextCompare) = 0 Then
        acol.Name = "newName"
        Exit For
    End If
Next acol
音盲 2024-08-24 18:05:59

我之前研究过这个问题,但没有任何 DDL 语句可以为您执行此操作。唯一的方法是添加新列,复制数据并删除旧列。

I've looked into this before and there is no DDL statement that can do this for you. Only method is to add a new column, copy the data and remove the old column.

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