开发者

Transfer Data from One table to Another Table based on a condition

I have a table name SOURCETABLE with fields RollID, SMSID, FName, Lname. The Destination table is DestTable with field ID, FirstName , LastName. My task is to creat DestTable from the data of SOURCETABLE with following condition:

If RollID = NULL
   ID = SMSID
else
   ID = RollID
end if

Fname will go to Firstname a开发者_如何学Gond Lname will go to LastName

What statement I should write?


This should do it:

INSERT INTO DestTable (ID, FirstName, LastName)
   SELECT COALESCE(RollId,SMSID), FName, LName
   FROM SourceTable

You can read about what COALESCE does here:

http://msdn.microsoft.com/en-us/library/ms190349.aspx


    INSERT INTO  DestTable (Id, FirstName, LastName)
    SELECT CASE WHEN ISNULL(RollId,'')='' THEN SMSId ELSE RollId END, 
    FName, LName FROM SOURCETABLE
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜