如何在选择查询中创建新列

问题描述 投票:0回答:4

在 MS Access 中,我想在选择查询的返回结果中插入一个新列。新列的每一行都具有相同的值。例如,我的 select 返回列 A、B,我希望 C 成为由 select 查询创建的新列:

A   B   C
----------
a1  b1  c
a2  b2  c
a3  b3  c
sql ms-access select insert
4个回答
88
投票
select A, B, 'c' as C
from MyTable

13
投票
SELECT field1, 
       field2,
       'example' AS newfield
FROM TABLE1

这将在输出中添加一个名为“newfield”的列,其值将始终为“example”。


1
投票

这取决于您想对该列执行什么操作,例如以下是向记录集追加新列的示例,该记录集可以在客户端更新:

Sub MSDataShape_AddNewCol()

  Dim rs As ADODB.Recordset
  Set rs = CreateObject("ADODB.Recordset")
  With rs
    .ActiveConnection = _
    "Provider=MSDataShape;" & _
    "Data Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Tempo\New_Jet_DB.mdb"
    .Source = _
    "SHAPE {" & _
    " SELECT ExistingField" & _
    " FROM ExistingTable" & _
    " ORDER BY ExistingField" & _
    "} APPEND NEW adNumeric(5, 4) AS NewField"

    .LockType = adLockBatchOptimistic

    .Open

    Dim i As Long
    For i = 0 To .RecordCount - 1
      .Fields("NewField").Value = Round(.Fields("ExistingField").Value, 4)
      .MoveNext
    Next

    rs.Save "C:\rs.xml", adPersistXML

  End With
End Sub

0
投票
SELECT 'column_value' (replace column_value with actual value) AS column_name
FROM your_table;
© www.soinside.com 2019 - 2024. All rights reserved.