Oracle Sql如果存在,那么执行此操作即可

时间:2011-10-27 00:39:56

标签: 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中执行其他功能。

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

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

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

第二个问题 - 其次,如果这个url存在并且它被修改(我们可以在最后修改的头中看到这个并且我将这个值存储在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();

1 个答案:

答案 0 :(得分:8)

免责声明:我现在无法测试。你想要一个像“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的评论更正)