sql - Oracle Sql 如果存在则做这个否则做那个

标签 sql oracle

    String s1 = "create table " +tableName+
    "(id number NOT NULL PRIMARY KEY, " +
    "url varchar(1000) NOT NULL, " +
    "urlHash varchar(1000) NOT NULL, " +
    "contentHash varchar(1000), " +
    "modDate date, " +
    "contentLocation varchar(1000), " +
    "status integer, " +
    "lastCrawlDate date) ";

String s3 = "create sequence " +sequenceName+ " start with 1 increment by 1 nomaxvalue";

    stmt=conn.createStatement();
    stmt.executeUpdate(s1);
    stmt.executeUpdate(s3);

ps = conn.prepareStatement (
          "INSERT INTO testing (id, url, urlHash, contentHash, modDate, contentLocation, status, lastCrawlDate) VALUES(test_seq.nextval,?,?,?,?,?,?,?)");


          ps.setString (1, url);
          ps.setString (2, urlHash);
          ps.setString (3, contentHash);
          ps.setString (4, modDate);
          ps.setString (5, contentLocation);
          ps.setLong (6, status);
          ps.setString (7, lastCrawlDate); 

我在这里做的是创建一个表和一个自动递增的序列。然后我使用准备好的语句插入到 oracle 数据库中。该表包含大约 20,000 个条目的大量数据。

第一个问题:- 所以现在我要做的是如果我需要将任何 url 和其他相应的数据添加到该表中,我必须在表中搜索以查看该 url 是否存在于该表中。如果不存在,则将此 url 添加到表和其他相应数据中。那么我怎样才能在 oracle sql 中实现这个 if exists then do this else do that 功能。

对于第一个问题,我可以在 url 或 urlHash 上触发选择查询以查看 url 是否存在,如果不存在则添加它

rs = stmt.executeQuery("SELECT urlHash FROM " +tableName+ " where urlHash ='urlHash' "); 
          while (rs.next()) {
              String last = rs.getString("urlHash");
              }

然后比较值,如果不比较则添加它。 我不认为这是我应该走的路。什么是解决第一个问题的最快方法..

第二个问题- 其次,如果这个 url 存在并且它被修改了(我们可以在最后修改的 header 中看到这个,我将这个值存储在 modDate 中)然后用其他相应的数据更新 url。

所以是这种问题

if URL does not Exists {
Add to the oracle table and other data
} else if(url got modified by checking the modDate) {
update the url into oracle database and other data
}

根据 pilcrow 解决方案更新:- 我在这里尝试将字符串日期类型转换为日期类型,但出现错误,因为索引 8 处缺少 IN 或 OUT 参数。为什么会这样?

    ps =  conn.prepareStatement(
                        "MERGE INTO testing " +
                        "USING (  SELECT ? AS url, " +                 // We will maybe add this record
                        "                ? AS urlHash, " +
                        "                ? AS contentHash, "+
                        "         TO_DATE(?, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') AS modDate, "+
                        "         ? AS contentLocation, "+
                        "         ? AS status, "+
                        "      TO_DATE(?, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') AS lastCrawlDate "+
                        "           FROM dual ) maybe "+
                        "   ON (maybe.urlHash = testing.urlHash) "+
                        "         WHEN MATCHED THEN "+
                           // We only need update the fields that might have changed
                        "       UPDATE SET testing.contentHash     = maybe.contentHash, "+
                        "                  testing.modDate         = maybe.modDate, "+
                        "                  testing.contentLocation = maybe.contentLocation, "+
                        "                  testing.status          = maybe.status, "+
                        "                  testing.lastCrawlDate   = maybe.lastCrawlDate "+
                           // But only if the new record is more recent
                        "        WHERE TO_CHAR(testing.modDate, 'YYYY-MM-DD'T'HH24:MI:SS'Z'') < TO_CHAR(maybe.modDate, ''YYYY-MM-DD'T'HH24:MI:SS'Z''') "+
                        "         WHEN NOT MATCHED THEN "+
                           // Insert new URL record
                        "   INSERT VALUES (test_seq.nextval, maybe.url, maybe.urlHash, maybe.contentHash, maybe.modDate, maybe.contentLocation, maybe.status, maybe.lastCrawlDate)");


ps.setString (1, "http://www.computergeeks.com");
          ps.setString (2, "ahsasoiowiewie");
          ps.setString (3, "sgasjwhwueybdbfndf");
          ps.setString (4, "2011-07-28T23:54:14Z");
          ps.setString (5, "c://");
          ps.setLong (6, 0);
          ps.setString (7, "2011-07-28T23:54:14Z"); 
          ps.executeUpdate();
          ps.close();

最佳答案

免责声明:我现在无法对此进行测试。您想要类似“UPSERT”的东西,其逻辑仅在 modDate 较新时更新。

在 Oracle 中,MERGE 应该可以做到这一点:

MERGE INTO testing
USING (  SELECT ? AS url,                 -- We will maybe add this record
                ? AS urlHash,
                ...
                ? AS lastCrawlDate
           FROM dual ) maybe
   ON (maybe.urlHash = testing.urlHash)
WHEN MATCHED THEN
   -- We only need update the fields that might have changed
       UPDATE SET testing.contentHash     = maybe.contentHash,
                  testing.modDate         = maybe.modDate,
                  testing.contentLocation = maybe.contentLocation,
                  testing.status          = maybe.status,
                  testing.lastCrawlDate   = maybe.lastCrawlDate
   -- But only if the new record is more recent
        WHERE testing.modDate < maybe.modDate
WHEN NOT MATCHED THEN
   -- Insert new URL record
   INSERT VALUES (test_seq.nextval, maybe.url, maybe.urlHash, ...);

顺便提一下,您似乎遗漏了一些对testing 表的约束(例如,urlurlHash 看起来像他们至少应该是独一无二的)

(更新:根据 ruakh 的评论进行更正)

关于sql - Oracle Sql 如果存在则做这个否则做那个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7910539/

相关文章:

php - 如何通过连接两个表来显示所有结果和空值?

java - 如何从游标中获取记录

sql - 如何从一个存储的proc中从SYS_REFCURSOR中获取数据,并在另一个过程中使用它?

c# - .net 应用程序的 oracle 连接超时

sql - 最大化 from 子句中的联接并最小化 where 子句中的联接 : true?

sql - SQL Server 如何知道锁定 View 对象?

oracle - Oracle 11g 标准版上的 AUDIT 命令

sql - Instr - 最后一个字符的最后索引

php - 执行 DELETE 查询时备份 MySQL

php - 将 2 个查询连接到 1 个