简体   繁体   English

在ORACLE中使用usjng OCI(c ++)插入查询的问题

[英]Problem with insert query usjng OCI(c++) in ORACLE

Problem Statement:- I am inserting a record into Oracle if that record is already present(duplicate==>primary key is present) in database i want to update it with new one. 问题陈述:-如果数据库中已经存在该记录(重复键==>主键),我正在向Oracle中插入一条记录,我想用新记录更新它。

Currently to solve this while inserting the record if i get OCI_ERROR then i call 当前解决此问题,同时插入记录,如果我收到OCI_ERROR,然后我打电话

OCIErrorGet( (dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR); OCIErrorGet((dvoid *)errhp,(ub4)1,(text *)NULL,&errcode,errbuf,(ub4)sizeof(errbuf),OCI_HTYPE_ERROR);

Then i check errbuf if the value of the errror buf is ORA-00001 ==> unique constraint violated if it is present then i update the value 然后我检查errbuf是否errror buf的值是ORA-00001 ==>如果存在唯一约束,则违反了唯一约束,然后我更新该值

Is there way to do the same thing except searching for record in the database if that record is already present update it I do not want to do this because i will have to write a code for that 除了在数据库中搜索记录(如果该记录已经存在)之外,是否有其他方法可以做相同的事情,所以我不想这样做,因为我将不得不为此编写代码

Is there any specific error generated if the value is duplicated in ORACLE? 如果值在ORACLE中重复,是否会产生任何特定的错误?

any suggestions? 有什么建议么?

you could use the MERGE statement. 您可以使用MERGE语句。 Among other things, it allows a simple UPSERT (it actually allows the UPSERT of SETs of rows and not only a single row). 除了其他功能外,它还允许使用简单的UPSERT(实际上,它允许对SET的行进行UPSERT,而不仅仅是单行)。 Consider: 考虑:

SQL> CREATE TABLE TEST (
  2     ID NUMBER,
  3     a VARCHAR2(10),
  4     b VARCHAR2(10),
  5     CONSTRAINT pk_test PRIMARY KEY (ID)
  6  );

Table created
SQL> MERGE INTO TEST t
  2  USING (SELECT 1 ID, 'a' a, 'b' b FROM dual) new_row
  3     ON (t.id = new_row.id)
  4  WHEN MATCHED THEN
  5     UPDATE SET t.a = new_row.a,
  6                t.b = new_row.b
  7  WHEN NOT MATCHED THEN
  8     INSERT (ID, a, b) VALUES (new_row.id, new_row.a, new_row.b);

Done
SQL> SELECT * FROM TEST;

        ID A          B
---------- ---------- ----------
         1 a          b
SQL> MERGE INTO TEST t
  2  USING (SELECT 1 ID, 'x' a, 'y' b FROM dual) new_row
  3     ON (t.id = new_row.id)
  4  WHEN MATCHED THEN
  5     UPDATE SET t.a = new_row.a,
  6                t.b = new_row.b
  7  WHEN NOT MATCHED THEN
  8     INSERT (ID, a, b) VALUES (new_row.id, new_row.a, new_row.b);

Done
SQL> SELECT * FROM TEST;

        ID A          B
---------- ---------- ----------
         1 x          y

ie: You can insert and update using the same statement. 即:您可以使用同一条语句插入和更新。

Cheers, 干杯,

-- -
Vincent 文森特

您应该使用#include并使用命名空间oracle :: occi;

There are 2 approaches to this problem, and the best one depends on details you didn't provide. 有两种解决方法,最好的一种取决于您未提供的详细信息。 One way would be to use a Repository pattern, introducing a software layer that tracks objects, and manages the update/insert issue by comparing the object you're giving it to its internal store of objects. 一种方法是使用存储库模式,引入一个跟踪对象的软件层,并通过将您提供给它的对象与其内部对象存储进行比较来管理更新/插入问题。 The other (more procedural) method is just to query for an object with the given PK first, and if it exists, use an update, if not, do the insert. 另一种(更具过程性的)方法只是先查询具有给定PK的对象,如果存在,则使用更新,否则,执行插入操作。

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

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