[SQL] IF EXISTS update/ insert 語法
判定當資料是否存在時,update/insert 語法
================================================================
IF EXISTS (SELECT * FROM TableName WHERE Column='SomeValue')
UPDATE TableName SET (...) WHERE Column='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
================================================================
UPDATE TableName SET (...) WHERE Column='SomeValue'
IF @@ROWCOUNT=0
INSERT INTO TableName VALUES (...)
IF EXISTS (SELECT * FROM TableName WHERE Column='SomeValue')
UPDATE TableName SET (...) WHERE Column='SomeValue'
ELSE
INSERT INTO Table1 VALUES (...)
================================================================
UPDATE TableName SET (...) WHERE Column='SomeValue'
IF @@ROWCOUNT=0
INSERT INTO TableName VALUES (...)
留言
張貼留言