更新查询在 SQL Server 中运行,但不在 Oracle 中运行

发布于 2024-09-30 18:42:30 字数 571 浏览 5 评论 0原文

我需要此更新查询在 SQL Server 和 Oracle 上运行。如果重要的话,我们的 Oracle 版本是 10.2。当我在 Oracle 中运行查询时,出现“ERROR ORA-00933:SQL 命令未正确结束”。我需要做什么才能让它在 Oracle 中运行?

UPDATE dbo.tableUpdate
SET fieldA = tt.fieldB
FROM dbo.tableTranslate tt
WHERE
    tt.fieldC = dbo.tableUpdate.fieldC
    AND
    tt.fieldD = dbo.tableUpdate.fieldA
    AND
    1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE 
            tblTrans.fieldC = dbo.tableUpdate.fieldC 
            AND 
            tblTrans.fieldD = dbo.tableUpdate.fieldA
)

I need this update query to run on both SQL Server and Oracle. Our Oracle version is 10.2 if that matters. When I run the query in Oracle I get "ERROR ORA-00933: SQL command not properly ended". What do I need to do to get this to run in Oracle?

UPDATE dbo.tableUpdate
SET fieldA = tt.fieldB
FROM dbo.tableTranslate tt
WHERE
    tt.fieldC = dbo.tableUpdate.fieldC
    AND
    tt.fieldD = dbo.tableUpdate.fieldA
    AND
    1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE 
            tblTrans.fieldC = dbo.tableUpdate.fieldC 
            AND 
            tblTrans.fieldD = dbo.tableUpdate.fieldA
)

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

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

发布评论

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

评论(3

最美不过初阳 2024-10-07 18:42:30

UPDATE...FROM 语法对 Oracle 无效。您将需要使用子查询,如下所示:

UPDATE dbo.tableUpdate t
SET t.fieldA = (SELECT tt.fieldB
                FROM dbo.tableTranslate tt
                WHERE tt.fieldC = t.fieldC
                AND tt.fieldD = t.fieldA
               )
WHERE 1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE tblTrans.fieldC = t.fieldC 
        AND tblTrans.fieldD = t.fieldA
        )

The UPDATE...FROM syntax is not valid for Oracle. You will need to use a subquery, like this:

UPDATE dbo.tableUpdate t
SET t.fieldA = (SELECT tt.fieldB
                FROM dbo.tableTranslate tt
                WHERE tt.fieldC = t.fieldC
                AND tt.fieldD = t.fieldA
               )
WHERE 1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE tblTrans.fieldC = t.fieldC 
        AND tblTrans.fieldD = t.fieldA
        )
苍白女子 2024-10-07 18:42:30

Oracle 中相关子查询的语法略有不同。

UPDATE dbo.tableUpdate
SET fieldA = (select tt.fieldB
FROM dbo.tableTranslate tt
WHERE
    tt.fieldC = dbo.tableUpdate.fieldC
    AND
    tt.fieldD = dbo.tableUpdate.fieldA)
    AND
    1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE 
tblTrans.fieldC = dbo.tableUpdate.fieldC 
AND 
tblTrans.fieldD = dbo.tableUpdate.fieldA)

The syntax for co-related sub query is a little different in Oracle.

UPDATE dbo.tableUpdate
SET fieldA = (select tt.fieldB
FROM dbo.tableTranslate tt
WHERE
    tt.fieldC = dbo.tableUpdate.fieldC
    AND
    tt.fieldD = dbo.tableUpdate.fieldA)
    AND
    1 = (
        SELECT COUNT(tblTrans.fieldD) 
        FROM dbo.tableTranslate tblTrans
        WHERE 
tblTrans.fieldC = dbo.tableUpdate.fieldC 
AND 
tblTrans.fieldD = dbo.tableUpdate.fieldA)
苏大泽ㄣ 2024-10-07 18:42:30

您将需要完全重写 Oracle 查询。有些东西在 Oracle 10.2 中不起作用:

  1. update 句子中的 from 子句(您需要为此编写一个子选择)
  2. dbo 架构,除非您在 Oracle 中实际上有一个名为 dbo 的用户拥有您要更新的表

You will need to rewrite completely your query for Oracle. Some stuff that won't work in Oracle 10.2:

  1. from clause in update sentence (you will need to write a subselect for this)
  2. dbo schema, unless you actually have a user named dbo in oracle owning the table you are trying to update
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文