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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3769702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:37:36  来源:igfitidea点击:

How to create a new column in a select query

sqlms-accessselectinsert

提问by Martin08

In MS Access, I want to insert a new column into the returned result of a select query. The new column has the same value for every row. For example, my select returns columns A, B and I want C to be the new column created by the select query:

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

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

回答by RedFilter

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

回答by Chenthil

SELECT field1, field2, 'example' AS newfield FROM TABLE1 This will add a column called "newfield" to the output, and its value will always be "example".

SELECT field1, field2, 'example' AS newfield FROM TABLE1 这将向输出添加一个名为“newfield”的列,其值将始终为“example”。

回答by onedaywhen

It depends what you wanted to do with that column e.g. here's an example of appending a new column to a recordset which can be updated on the client side:

这取决于您想对该列做什么,例如,这是将新列附加到可以在客户端更新的记录集的示例:

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