如果有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
返回的数据复制到同一个表中的新列中?
答案 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