开发者

SQLServer on existing update

开发者 https://www.devze.com 2023-03-26 00:40 出处:网络
We are planning to convert an application from Sybase SQL Anywhere to SQL Server. At SA we use a lot of \"on existing update\", that checks by primary key, if l开发者_JS百科ine exists.

We are planning to convert an application from Sybase SQL Anywhere to SQL Server.

At SA we use a lot of "on existing update", that checks by primary key, if l开发者_JS百科ine exists. If true, is update (in same way if I was executing update clause). If not, it inserts.

Does SQL Server supports something like this?


AFAIK there is no support for this in one command. And the best way I found was follow Jeremiah Clark's tip: try to update, then check for the number of affected records. If it was zero, then I insert:

UPDATE Table1 SET (...) WHERE Column1='SomeValue'
IF @@ROWCOUNT=0
    INSERT INTO Table1 VALUES (...)


If you will use SQL Server 2008 and later, you can use MERGE command. You can find description here:

http://technet.microsoft.com/en-us/library/bb510625(SQL.100).aspx

If you use older SQL, Jeremiah Clark's solution suggested by Erick Sasse will be ok.


SQL Server has all the features of an high-end RDBMS, Stored Procedures, Triggers, UDF and so on... as you probably know MS SQL Server was branched out exactly from Sybase so they do have common roots.

I am not sure I get your question completely, of course you can have a SQL Statement which contains some logic and does INSERT or UPDATE depending on some conditions or if a record was already found, TSQL is the SQL dialect of SQL Server and supports this and much more.

If you really have a specific question or doubt on a statement please show us your statement as you have it right now in SA.

0

精彩评论

暂无评论...
验证码 换一张
取 消