Вы находитесь на странице: 1из 2

How to create a new column in a select

query

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:
A
B
C
---------a1 b1 c
a2 b2 c
a3 b3 c

up vote 6
down vote
favorite
sql ms-access select insert
edited Sep 22 '10 at 13:30 asked Sep 22 '10 at 13:28
share|improve this question
egrunin Martin08
15.9k32370 4,82384678
add a comment

2 Answers
active oldest votes
select A, B, 'c' as C
from MyTable

answered Sep 22 '10 at 13:29


up vote 13
down vote
accepted

share|improve this answer


RedFilter
88.3k8152186
add a comment

up vote 0
down vote

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

Вам также может понравиться