我有一个备份工具,还原工作。这是我的表:
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")
在恢复类中,我想在表中查找主键,如果存在,则更新该行,如果没有,则插入一行。
现在问题是我需要从类中传递参数(它们不存在于任何表中),我该怎么做?你的建议是什么?使用preparedstatment还是什么?如何?请举例说明或链接到来源。
更多信息: 我正在使用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在更新的情况下工作,但如果它是一个插入案例,它将起作用。在更新的情况下,它不会抛出任何错误它只是冻结,直到我从DB删除记录,然后它插入它,所以没有更新!你在这段代码中发现了什么问题吗?
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)");
谢谢!
答案 0 :(得分:3)
最后这个工作!,所以这里是SQL命令的查询:
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)
这是使用pstmt的java代码:
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 a.corpus_name = incoming.corpus_name , a.corpusdesc = incoming.corpusdesc , a.corpusimageids = incoming.corpusimageids , a.rocf1 = incoming.rocf1 , a.rocf2 = incoming.rocf2 , a.rocf3 = incoming.rocf3 , a.rocc1 = incoming.rocc1 , a.rocc2 = incoming.rocc2 , a.rocc3 = incoming.rocc3 , a.corpusactive = incoming.corpusactive , a.corpusrunfrequency = incoming.corpusrunfrequency , a.corpuslastrun = incoming.corpuslastrun , a.corpuslastsync = incoming.corpuslastsync , a.rocsettingid = incoming.rocsettingid , a.corpusaffinity = incoming.corpusaffinity , a.corpusterms = incoming.corpusterms , a.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)");
你在问题部分有创建表,希望它可以帮助某人! :)
答案 1 :(得分:1)
如果您使用的是Oracle,那么您需要关注MERGE
语句 - INSERT ... ON DUPLICATE KEY
在Oracle中不起作用。
我不知道这是否是唯一的问题,但您发布的MERGE
中存在一些语法错误:
MERGE INTO dev_corpus a
USING (SELECT * FROM dual WHERE (corpusid=?, corpus_name=?, corpuslastsync=?)
^^^^^^^^^^^^^^^^^^^^^^ (1)
ON (a.corpusid = incoming.corpusid )
WHEN MATCHED THEN
UPDATE SET (a.corpus_name = incoming.corpus_name AND a.corpuslastsync = incoming.corpuslastsync )
^^^^^^^^^^^^^^^^^^ (2)
WHEN NOT MATCHED THEN
INSERT (a.corpusid, a.corpus_name, a.corpuslastsync)
VALUES (incoming.corpusid, incoming.corpus_name, incoming.corpuslastsync)
问题(1):你有一个未闭括号,加上多个WHERE
条件应该用AND
分隔,而不是逗号。这是修正后的版本,缩进,因此不需要水平滚动:
USING (
SELECT * FROM dual
WHERE (corpusid=? AND corpus_name=? AND corpuslastsync=?))
在这种情况下,围绕WHERE
条件的括号是可选的,所以这也可以使用:
USING (
SELECT * FROM dual
WHERE corpusid=? AND corpus_name=? AND corpuslastsync=?)
问题(2):SET
后面的括号,这里需要逗号而不是AND
。它应该看起来像这样(如上所述,我缩进了它,因此可以在没有水平滚动的情况下读取它)
UPDATE SET
a.corpus_name = incoming.corpus_name,
a.corpuslastsync = incoming.corpuslastsync
这应该处理任何语法错误,但当然不能保证您的结果符合预期:)
最后,正如我在评论中提到的,使用测试值(而不是参数)在SQLPlus或SQL Developer中尝试。以这种方式调试会更容易,更容易。如果MERGE
行为正确,您可以将其移动到Java代码并对其进行参数化。祝你好运!
更新后问题的附录
首先,您正在使用SQL Developer来解决这个问题的好消息!无视我上面的最后一段:)
其次,您的位置包括AND corpuslastsync = null
。检查null时,您需要IS NULL
,而不是= NULL
:
... AND corpuslastsync IS NULL
最后,我非常确定您最近尝试的具体错误是由于此行中的别名造成的:
INSERT (a.corpusid, a.corpus_name, a.corpuslastsync)
请改为尝试:
INSERT (corpusid, corpus_name, corpuslastsync)
Oracle知道这里命名的列属于dev_corpus
表,别名为a
。