将SELECT数据复制到同一个表中的列中

时间:2015-01-09 18:31:04

标签: sql ms-access

如果有SELECT语句用于获取现有列并将其拆分为两个新列:

SELECT IIf(InStr(1,[First Name]," ")>0,Left([First Name],InStr(1,[First Name]," ")),[First Name]) AS Fname, 
IIf(InStr(1,[First Name]," ")>0,Mid([First Name],InStr(1,[First Name]," ")+1),NULL) AS Mname
FROM Pupils;

我已将 Fname Mname 列添加到表格中。如何将从SELECT返回的数据复制到同一个表中的新列中?

2 个答案:

答案 0 :(得分:1)

IIf查询中重复使用SELECT查询中的UPDATE个表达式。

UPDATE Pupils
SET
    Fname = IIf(
        InStr(1,[First Name]," ")>0,
        Left([First Name],InStr(1,[First Name]," ")),
        [First Name]
        ),
    Mname = IIf(
        InStr(1,[First Name]," ")>0,
        Mid([First Name],InStr(1,[First Name]," ")+1),
        NULL
        );

答案 1 :(得分:0)

使用加入更新

Update M
set M.NewCol1=D.Fname 
  M.NewCol2=D.Mname
from  MainTable M
JOIN
(
SELECT IIf(InStr(1,[First Name]," ")>0,Left([First Name],InStr(1,[First Name]," ")),[First Name]) AS Fname, 
IIf(InStr(1,[First Name]," ")>0,Mid([First Name],InStr(1,[First Name]," ")+1),NULL) AS Mname
FROM Pupils)D
ON M.col1 = D.col1