**using update with a select command is good cohise
but better way ms sql's new MERGE command
and also merge command must be closed with a semicolon(;) .
I share simple command syntax below..
in Detail you can look the document.
plus msdn document is here..
Syntax of MERGE statement is as following:
MERGE [ TOP ( expression ) [ PERCENT ] ] [ INTO ] target_table [ WITH () ] [ [ AS ] table_alias] USING ON [ WHEN MATCHED [ AND ] THEN ] [ WHEN NOT MATCHED [ BY TARGET ] [ AND ] THEN ] [ WHEN NOT MATCHED BY SOURCE [ AND ] THEN ] [ ] [ OPTION ( [ ,...n ] ) ] ;
--sample usage..
MERGE TargetTable AS stm USING (SELECT StudentID,StudentName FROM ResourceTable) AS sd ON stm.ID = sd.ID WHEN MATCHED AND stm.Marks > 250 THEN DELETE WHEN MATCHED THEN UPDATE SET stm.Marks = stm.Marks + 25 WHEN NOT MATCHED THEN INSERT(ID,Marks) VALUES(sd.ID,25); GO
No comments:
Post a Comment