儘管我使用mySQL(現在),但我不想要任何數據庫特定的SQL。ANSI SQL問題 - 如何插入或更新記錄(如果它已存在)?
我想插入一條記錄,如果它不存在,並更新一個字段,如果它確實存在。我想使用ANSI SQL。
表看起來是這樣的:
create table test_table (id int, name varchar(16), weight double) ;
//test data
insert into test_table (id, name, weight) values(1,'homer', 900);
insert into test_table (id, name, weight) values(2,'marge', 85);
insert into test_table (id, name, weight) values(3,'bart', 25);
insert into test_table (id, name, weight) values(4,'lisa', 15);
If the record exists, I want to update the weight (increase by say 10)
我結束了使用INSERT ..在重複密鑰更新 – morpheous 2010-04-26 22:27:16
MS SQL Server 2005不支持MERGE語法。只有SQL Server 2008和更高版本支持它。 – 2011-10-17 03:01:44
看起來像SQL服務器2005測試版有它,但不是最終版本。 2008年得到它。參考:http://geekswithblogs.net/SabotsShell/archive/2005/08/20/50706.aspx – Alex 2012-11-14 15:37:30