Upsert (update or insert) in Sybase ASE?

谁说胖子不能爱 提交于 2019-12-04 02:10:17

unfortunately, it is impossible to insert and update a table in one statement without using MERGE. which btw does exist in SQL as of SQL:2008, according to this article anyway, and supported by almost all major databases, except Sybase ASE and PostgreSQL.

PerformanceDBA

Sybase and DB2 are very IEC/ISO/ANSI SQL Standrd-compliant. MS a little less so.

Oracle is not very Standard-compliant at all (despite what the glossies say). More important, due to it limitations, the method they use to overcome them is to introduce Extensions to SQL (which are not required for the others DBMS, which do not have the limitations). Nice way of making sure that customers do not migrate away.

So the best advice for you is to learn the SQL Standard way of doing whatever you were doing on the Oracle side. And second (not first) learn about Sybases or DB2s (or whatever) Extensions.

"MERGE" and "UPSERT" do not exist in SQL, they exist in Oracle only. The bottom line is, you have to UPDATE and INSERT in two separate operations.

In SQL, UPDATE and INSERT apply to a single table; you may have quite complex FROM clauses.

For "MERGE", that is simply an:

INSERT target ( column_list ) -- we do have defaults
SELECT ( column_list )
    FROM source
    WHERE primary_key NOT IN ( SELECT primary_key FROM target )

Update is simply the complement:

UPDATE target SET ( target_column = source_column, ... )
    FROM source
    WHERE primary_key IN ( SELECT primary_key FROM target )

In the UPDATE it is easy to merge the WHERE conditions and eliminate the Subquery (I am showing it to you for explanation).

As I understand it, Oracle is abyssmal at executing Subqueries (Standard SQL). Which is why they have all these non-Standard "MERGE", etc., the purpose of which is to avoid the Standard Subquery syntax, which every other DBMS performs with ease.

Maybe it could work. Tested in ASA9.

insert into my_table (columns) on existing update values (values);

May be you could try to fake it with INSERT INTO and/or UPDATE FROM with some sub-queries but it will not be as convenient as Oracle does.

You wanna do this into code or data warehouse ? because you could also encapsulate all the SQL into a stored procedure if you want to hide the complexity of the queries.

user11673175

Merge exists in SAP ASE 15.7 upwards, as mentioned here and here

Replace / Upsert exists in SAP ASE 16.0 and up.

You'll need to update to access them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!