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

时间:2010-09-22 13:28:52

标签: sql ms-access select insert

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

A   B   C
----------
a1  b1  c
a2  b2  c
a3  b3  c

3 个答案:

答案 0 :(得分:45)

select A, B, 'c' as C
from MyTable

答案 1 :(得分:5)

SELECT field1,        场2,        '例子'AS newfield   FROM TABLE1 这将在输出中添加一个名为“newfield”的列,其值将始终为“example”。

答案 2 :(得分: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