在 Sybase ASE 中更新插入(更新或插入)?

发布于 2024-10-08 13:44:48 字数 174 浏览 4 评论 0 原文

我正在编写一个应用程序,将数据从 Oracle 移动到 Sybase,并且需要执行更新/插入操作。在 Oracle 中,我会使用 MERGE INTO,但它似乎在 Sybase 中不可用(无论如何,在 ASE 中也不可用)。我知道这可以通过多个语句来完成,但出于几个原因,我真的试图将其放入单个语句中。

有什么建议吗?

I'm writing an application to move data from Oracle to Sybase and need to perform update / insert operations. In Oracle, I'd use MERGE INTO, but it doesn't seem to be available in Sybase (not in ASE, anyway). I know this can be done with multiple statements, but for a couple of reasons, I'm really trying to get this into a single statement.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清晨说晚安 2024-10-15 13:44:49

也许它可以起作用。在 ASA9 中测试。

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

Maybe it could work. Tested in ASA9.

insert into my_table (columns) on existing update values (values);
南城旧梦 2024-10-15 13:44:49

也许您可以尝试使用 INSERT INTO 和/或 UPDATE FROM 以及一些子查询来伪造它,但它不会像 Oracle 那样方便。

您想将其写入代码或数据仓库吗?因为如果您想隐藏查询的复杂性,您还可以将所有 SQL 封装到存储过程中。

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.

べ繥欢鉨o。 2024-10-15 13:44:48

Sybase 和 DB2 非常符合 IEC/ISO/ANSI SQL 标准。 MS 稍微差一些。

Oracle 根本不符合标准(尽管注释上是这么说的)。更重要的是,由于它的局限性,他们用来克服它们的方法是引入 SQL 扩展(其他 DBMS 不需要这些扩展,因为其他 DBMS 没有这些限制)。确保客户不会迁移的好方法。

因此,对您来说最好的建议是学习 SQL 标准方法来完成您在 Oracle 方面所做的任何事情。其次(不是首先)了解 Sybase 或 DB2(或其他)扩展。

SQL中不存在“MERGE”和“UPSERT”,它们仅存在于Oracle中。最重要的是,您必须在两个单独的操作中进行更新和插入。

在SQL中,UPDATE和INSERT适用于单个表;您可能有相当复杂的 FROM 子句。

对于“MERGE”,这只是一个:

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

更新只是补充:

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

在 UPDATE 中,很容易合并 WHERE 条件并消除子查询(我向您展示它以供解释)。

据我了解,Oracle 在执行子查询(标准 SQL)方面非常糟糕。这就是为什么他们有所有这些非标准的“MERGE”等,其目的是避免标准子查询语法,而其他 DBMS 都可以轻松执行。

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.

固执像三岁 2024-10-15 13:44:48

不幸的是,如果不使用 MERGE,就不可能在一条语句中插入和更新表。顺便说一句,根据这篇文章,从 SQL:2008 开始,SQL 中确实存在,并且几乎所有主要数据库都支持,除了 Sybase ASE 和 PostgreSQL。

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.

坚持沉默 2024-10-15 13:44:48

合并存在于 SAP ASE 15.7 及以上版本中,如上所述 此处此处

替换/更新存在于SAP ASE 16.0 及更高版本

您需要更新才能访问它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文