我有一个备份实用程序,在恢复部分工作。这是我的桌子:
CREATE TABLE "SBOOKS"."DEV_CORPUS"
( "CORPUSID" NUMBER(9,0) NOT NULL ENABLE,
"CORPUS_NAME" VARCHAR2(768 BYTE) NOT NULL ENABLE,
"CORPUSLASTSYNC" DATE,
PRIMARY KEY ("CORPUSID")
在还原类中,我想在表中查找主键,如果存在则更新该行,如果不存在则插入一行。
现在的问题是我需要从类中传递参数(它们不存在于任何表中),我该怎么做?你的建议是什么?使用preparedstatement还是什么?如何?请提供示例或来源链接。
更多信息:我正在使用 oracle sql developer 和 java netbeans。
编辑: 我在命令窗口中尝试这个:
MERGE INTO dev_corpus a
USING (SELECT corpusid, corpus_name, corpusdesc, corpusimageids, rocf1, rocf2, rocf3, rocc1, rocc2, rocc3, corpusactive, corpusrunfrequency, corpuslastrun, corpuslastsync, rocsettingid, corpusaffinity, corpusterms, corpusdomain FROM dual WHERE corpusId = 1000156 AND corpus_name = 'sahar' AND corpusdesc = 'sahaaaaa' AND corpusimageids IS NULL AND rocf1 IS NULL AND rocf2 IS NULL AND rocf3 IS NULL AND rocc1 IS NULL AND rocc2 IS NULL AND rocc3 IS NULL AND corpusactive IS NULL AND corpusrunfrequency IS NULL AND corpuslastrun IS NULL AND corpuslastsync IS NULL AND rocsettingid IS NULL AND corpusaffinity IS NULL AND corpusterms IS NULL AND corpusdomain IS NULL) incoming
ON (a.corpusid = incoming.corpusid )
WHEN MATCHED THEN
UPDATE SET corpusid = incoming.corpusid , corpus_name = incoming.corpus_name , corpusdesc = incoming.corpusdesc , corpusimageids = incoming.corpusimageids , rocf1 = incoming.rocf1 , rocf2 = incoming.rocf2 , rocf3 = incoming.rocf3 , rocc1 = incoming.rocc1 , rocc2 = incoming.rocc2 , rocc3 = incoming.rocc3 , corpusactive = incoming.corpusactive , corpusrunfrequency = incoming.corpusrunfrequency , corpuslastrun = incoming.corpuslastrun , corpuslastsync = incoming.corpuslastsync , rocsettingid = incoming.rocsettingid , corpusaffinity = incoming.corpusaffinity , corpusterms = incoming.corpusterms , corpusdomain = incoming.corpusdomain
WHEN NOT MATCHED THEN
INSERT (corpusid, corpus_name, corpusdesc, corpusimageids, rocf1, rocf2, rocf3, rocc1, rocc2, rocc3, corpusactive, corpusrunfrequency, corpuslastrun, corpuslastsync, rocsettingid, corpusaffinity, corpusterms, corpusdomain)
VALUES (incoming.corpusid, incoming.corpus_name, incoming.corpusdesc, incoming.corpusimageids, incoming.rocf1, incoming.rocf2, incoming.rocf3, incoming.rocc1, incoming.rocc2, incoming.rocc3, incoming.corpusactive, incoming.corpusrunfrequency, incoming.corpuslastrun, incoming.corpuslastsync, incoming.rocsettingid, incoming.corpusaffinity, incoming.corpusterms, incoming.corpusdomain)
这是真正的桌子:
CREATE TABLE "SBOOKS"."DEV_CORPUS"
( "CORPUSID" NUMBER(9,0) NOT NULL ENABLE,
"CORPUS_NAME" VARCHAR2(768 BYTE) NOT NULL ENABLE,
"CORPUSDESC" VARCHAR2(4000 BYTE),
"CORPUSIMAGEIDS" VARCHAR2(768 BYTE),
"ROCF1" FLOAT(63),
"ROCF2" FLOAT(63),
"ROCF3" FLOAT(63),
"ROCC1" FLOAT(63),
"ROCC2" FLOAT(63),
"ROCC3" FLOAT(63),
"CORPUSACTIVE" NUMBER(3,0),
"CORPUSRUNFREQUENCY" NUMBER(3,0),
"CORPUSLASTRUN" DATE,
"CORPUSLASTSYNC" DATE,
"ROCSETTINGID" NUMBER(3,0),
"CORPUSAFFINITY" NUMBER(3,0),
"CORPUSTERMS" VARCHAR2(4000 BYTE),
"CORPUSDOMAIN" NUMBER(3,0),
PRIMARY KEY ("CORPUSID")
向我抛出此错误:
Error at Command Line:2 Column:644
Error report:
SQL Error: ORA-00904: "CORPUSDOMAIN": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
.
我不知道我错过了什么。你有什么想法吗?
谢谢!
EDIT2: 最后这在命令赢中起作用:
MERGE INTO dev_corpus a
USING (SELECT 1000156 corpusid, 'sss2' corpus_name, 'sahaaaaaar' corpusdesc, null corpusimageids, null rocf1, null rocf2, null rocf3, null rocc1, null rocc2, null rocc3, null corpusactive, null corpusrunfrequency, null corpuslastrun, null corpuslastsync, null rocsettingid, null corpusaffinity, null corpusterms, null corpusdomain FROM dual) incoming
ON (a.corpusid = incoming.corpusid )
WHEN MATCHED THEN
UPDATE SET corpus_name = incoming.corpus_name , corpusdesc = incoming.corpusdesc , corpusimageids = incoming.corpusimageids , rocf1 = incoming.rocf1 , rocf2 = incoming.rocf2 , rocf3 = incoming.rocf3 , rocc1 = incoming.rocc1 , rocc2 = incoming.rocc2 , rocc3 = incoming.rocc3 , corpusactive = incoming.corpusactive , corpusrunfrequency = incoming.corpusrunfrequency , corpuslastrun = incoming.corpuslastrun , corpuslastsync = incoming.corpuslastsync , rocsettingid = incoming.rocsettingid , corpusaffinity = incoming.corpusaffinity , corpusterms = incoming.corpusterms , corpusdomain = incoming.corpusdomain
WHEN NOT MATCHED THEN
INSERT (corpusid, corpus_name, corpusdesc, corpusimageids, rocf1, rocf2, rocf3, rocc1, rocc2, rocc3, corpusactive, corpusrunfrequency, corpuslastrun, corpuslastsync, rocsettingid, corpusaffinity, corpusterms, corpusdomain)
VALUES (incoming.corpusid, incoming.corpus_name, incoming.corpusdesc, incoming.corpusimageids, incoming.rocf1, incoming.rocf2, incoming.rocf3, incoming.rocc1, incoming.rocc2, incoming.rocc3, incoming.corpusactive, incoming.corpusrunfrequency, incoming.corpuslastrun, incoming.corpuslastsync, incoming.rocsettingid, incoming.corpusaffinity, incoming.corpusterms, incoming.corpusdomain)
但这在 java 类 nooot 中适用于更新情况,但如果它是插入情况,它将起作用。在更新的情况下,它不会抛出任何错误,它只会冻结,直到我从数据库中删除记录,然后插入它,所以没有更新!。你发现这段代码有什么问题吗?
preparedStatement = dbConnection.prepareStatement("MERGE INTO dev_corpus a " +
"USING (SELECT ? corpusid, ? corpus_name, ? corpusdesc, ? corpusimageids, ? rocf1, ? rocf2, ? rocf3, ? rocc1, ? rocc2, ? rocc3, ? corpusactive, ? corpusrunfrequency, ? corpuslastrun, ? corpuslastsync, ? rocsettingid, ? corpusaffinity, ? corpusterms, ? corpusdomain FROM dual) incoming " +
"ON (a.corpusid = incoming.corpusid ) " +
"WHEN MATCHED THEN " +
"UPDATE SET corpus_name = incoming.corpus_name , corpusdesc = incoming.corpusdesc , corpusimageids = incoming.corpusimageids , rocf1 = incoming.rocf1 , rocf2 = incoming.rocf2 , rocf3 = incoming.rocf3 , rocc1 = incoming.rocc1 , rocc2 = incoming.rocc2 , rocc3 = incoming.rocc3 , corpusactive = incoming.corpusactive , corpusrunfrequency = incoming.corpusrunfrequency , corpuslastrun = incoming.corpuslastrun , corpuslastsync = incoming.corpuslastsync , rocsettingid = incoming.rocsettingid , corpusaffinity = incoming.corpusaffinity , corpusterms = incoming.corpusterms , corpusdomain = incoming.corpusdomain " +
"WHEN NOT MATCHED THEN " +
"INSERT (corpusid, corpus_name, corpusdesc, corpusimageids, rocf1, rocf2, rocf3, rocc1, rocc2, rocc3, corpusactive, corpusrunfrequency, corpuslastrun, corpuslastsync, rocsettingid, corpusaffinity, corpusterms, corpusdomain) " +
"VALUES (incoming.corpusid, incoming.corpus_name, incoming.corpusdesc, incoming.corpusimageids, incoming.rocf1, incoming.rocf2, incoming.rocf3, incoming.rocc1, incoming.rocc2, incoming.rocc3, incoming.corpusactive, incoming.corpusrunfrequency, incoming.corpuslastrun, incoming.corpuslastsync, incoming.rocsettingid, incoming.corpusaffinity, incoming.corpusterms, incoming.corpusdomain)");
谢谢!