以下是可能的方式:
1)用 '存在'方法
IF EXISTS(SELECT 1 FROM Table1 WHERE Name = @Name)
UPDATE Table1 SET Status = @Status WHERE Name = @Name
ELSE
INSERT INTO Table1 (Name, Status) VALUES(@Name, @Status)
2)使用MERGE
MERGE Table1 AS TARGET
USING (SELECT @Name, @Status FROM Table1) AS Source (Name, Status)
ON (TARGET.Name = Source.Name)
WHEN MATCHED
THEN UPDATE SET Status = Source.Status
WHEN NOT MATCHED
THEN INSERT (Name, Status) VALUES (Source.Name, Source.Status)
3)未經編譯的查詢(構建查詢)
--Unable to post on S.O.F due to security limitations
雖然所提出的方法是第一&第二(第三是不必要)。