我希望使用以下update \ insert方法来加快我的应用程序insert \ updates的速度:
UPDATE [Application_Type_Table] SET ApplicationType='Test Value' WHERE ID='1' IF @@ROWCOUNT=0 INSERT INTO [Application_Type_Table] VALUES ('Test Value')
我将如何使用sql参数来做到这一点?由于@@,将把rowcount函数作为参数。
sqlCommand.CommandText = _ "UPDATE [Application_Type_Table]" _ SET (SET ApplicationType=@TestValue" _ "WHERE ID=@RecordID IF @@ROWCOUNT=0 INSERT INTO [Application_Type_Table] VALUES (@TestValue)"
http://blogs.msdn.com/b/miah/archive/2008/02/17/sql-if-exists-update-else- insert.aspx
这种模式称为UPSERT。现代的方法是在SQL Server 2008中使用MERGE,例如
MERGE INTO Application_Type_Table AS target USING ( VALUES (@RecordID, @TestValue) ) AS source (ID, ApplicationType) ON target.ID = source.ID WHEN MATCHED THEN UPDATE SET ApplicationType = source.ApplicationType WHEN NOT MATCHED THEN INSERT (ID, ApplicationType) VALUES (ID, ApplicationType);