2015-01-09 54 views
0

如果有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; 

我已經添加了列FNAMEMname到桌上。如何將從SELECT返回的數據複製到同一張表的新列中?

回答

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 
1

重用在UPDATE查詢從SELECT查詢那些IIf表達式。

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 
     ); 
相關問題