SQL IF EXISTS update else insert
SQL 資料存在就更新,不存在就新增一筆寫法
方法1:
IF EXISTS (SELECT * FROM Table1 WHERE Column1='value1')
UPDATE Table1 SET Column2='value2' WHERE Column1='value1'
ELSE
INSERT INTO Table1 VALUES ('value1','value2')
方法2:
UPDATE Table1 SET Column2='value2' WHERE Column1='value1'
IF @@ROWCOUNT=0
INSERT INTO Table1 VALUES (...)
方法1:
IF EXISTS (SELECT * FROM Table1 WHERE Column1='value1')
UPDATE Table1 SET Column2='value2' WHERE Column1='value1'
ELSE
INSERT INTO Table1 VALUES ('value1','value2')
方法2:
UPDATE Table1 SET Column2='value2' WHERE Column1='value1'
IF @@ROWCOUNT=0
INSERT INTO Table1 VALUES (...)
留言
張貼留言