简体   繁体   English

检查表是否存在,如果不将另一个表用于“ SELECT”语句

[英]Checking if table exists if not use another for a 'SELECT' statement

I am still very new to SQL. 我对SQL还是很陌生。 I am working on a system which uses Derby database in development and Oracle in production. 我正在开发一个在开发中使用Derby数据库,在生产中使用Oracle的系统。 I want to have an SQL Statement which works in both. 我想有一个在两个中都可以使用的SQL语句。 Here is my code: 这是我的代码:

SELECT rma.crspdt AS bic_crspndt,
               rma.issr   AS bic_issr
        FROM   rma
        WHERE  (rma.tp = 'Issued' OR rma.tp = 'Received')
        AND    rma.rmasts = 'Enabled'
        AND    rma.svcnm = 'swift.fin') r
       INNER JOIN (SELECT 1 ID FROM SYSIBM.SYSDUMMY1 UNION ALL
                   SELECT 2 ID FROM SYSIBM.SYSDUMMY1) dummy ON (dummy.id = 1 AND r.bic_crspndt IS NOT NULL)
                                                   OR (dummy.id = 2 AND r.bic_issr IS NOT NULL)

I am using here 'SYSIBM.SYSDUMM1' table. 我在这里使用“ SYSIBM.SYSDUMM1”表。 Oracle has an exact alternative table for 'SYSIBM.SYSDUMM1' named 'DUAL'. Oracle有一个名为“ DUAL”的“ SYSIBM.SYSDUMM1”的确切替代表。 The problem is that when I run my code in development (derby) this code works fine but in production (oracle) I get an error saying something like unknown table. 问题是,当我在开发(derby)中运行我的代码时,此代码可以正常工作,但在生产(oracle)中,则出现错误,提示诸如未知表之类的内容。

What I want to do is that in my code do an IF-ELSE/CASE-WHEN or something like this to check in runtime if 'SYSIBM.SYSDUMMY1' table exists and if it does not exist (like in oracle) then I want to use 'DUAL' table. 我想做的是在我的代码中执行IF-ELSE / CASE-WHEN或类似的操作以在运行时检查“ SYSIBM.SYSDUMMY1”表是否存在以及是否不存在(例如在oracle中),所以我想使用“ DUAL”表。 I am very new to SQL and would like some help in this matter. 我对SQL非常陌生,希望对此有所帮助。

You don't say which Oracle version you are using. 您没有说您正在使用哪个Oracle版本。 In Oracle 12c there is the SQL Translation Framework. 在Oracle 12c中有SQL转换框架。 With this example you could set up a translation such that SYSIBM.SYSDUMMY1 is translated to DUAL. 在此示例中,您可以设置一个转换,以将SYSIBM.SYSDUMMY1转换为DUAL。 I've seen demonstrations but haven't used it personally. 我看过示范,但没有亲自使用。 I suggest the Oracle docs (as usual) for information - https://docs.oracle.com/database/121/DRDAA/sql_transl_arch.htm#DRDAA131 . 我建议像往常一样使用Oracle文档作为参考-https: //docs.oracle.com/database/121/DRDAA/sql_transl_arch.htm#DRDAA131

您不仅可以创建一个DUAL表

There are some problem in your code from Oracle point of view which I can think of. 从Oracle的角度来看,您的代码中存在一些我可以想到的问题。 So from comments what I get it that you are not able to use Dual. 因此,从评论中我知道您不能使用Dual。 Dual exists in Oracle. Oracle中存在双重。 So try running select 1 from dual and if it doesn't work, your query is not running in oracle for sure. 因此,请尝试select 1 from dual运行select 1 from dual ,如果它不起作用,则肯定不能在oracle中运行查询。 Apart from it there are couple of more problem with your query. 除此之外,您的查询还有更多其他问题。

  1. Using where before inner join. 使用内部联接之前的位置。
  2. Extra closing braces for r 额外关闭括号为r

Based of above input, this query might work for you if you are running it in Oracle . 根据上述输入,如果您正在Oracle中运行此查询,则该查询可能对您Oracle Replace dual with sysibm.sysdummy if you are not using Oracle. 如果不使用Oracle,则用sysibm.sysdummy替换dual。

Note: You should use proper join syntax( INNER JOIN ). 注意:您应该使用正确的INNER JOIN语法( INNER JOIN )。 I wasn't able to figure out joining condition hence I am using comma to join. 我无法弄清楚加入条件,因此我使用逗号加入。

SELECT rma.crspdt AS bic_crspndt,
       rma.issr AS bic_issr
FROM rma r,
  (SELECT 1 ID FROM dual UNION ALL
   SELECT 2 ID FROM dual) dummy
WHERE ( (dummy.id = 1
         AND r.bic_crspndt IS NOT NULL)
       OR (dummy.id = 2
           AND r.bic_issr IS NOT NULL) 
     )
  AND (rma.tp = 'Issued'
       OR rma.tp = 'Received')
  AND rma.rmasts = 'Enabled'
  AND rma.svcnm = 'swift.fin'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM