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

发布于 2024-07-23 20:07:35 字数 451 浏览 3 评论 0原文

问题陈述:- 如果数据库中已存在该记录(重复==>主键存在),我将向 Oracle 中插入一条记录 我想用新的更新它。

目前,为了在插入记录时解决此问题,如果我收到 OCI_ERROR,则调用

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

然后我检查 errbuf 错误 buf 的值是否为 ORA-00001 ==> 如果存在,则违反唯一约束,然后我更新该值

除了在数据库中搜索记录(如果该记录已存在)之外,还有其他方法可以做同样的事情吗? 我不想这样做,因为我必须为此编写代码,

如果该值在 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.

Currently to solve this while inserting the record if i get OCI_ERROR then i call

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

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?

any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2024-07-30 20:07:35

您可以使用 MERGE 语句。 除此之外,它还允许简单的 UPSERT(它实际上允许对行集进行 UPSERT,而不仅仅是单行)。 考虑:

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

即:您可以使用相同的语句插入和更新。

干杯,

--
文森特

you could use the MERGE statement. Among other things, it allows a simple UPSERT (it actually allows the UPSERT of SETs of rows and not only a single row). 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

i-e: You can insert and update using the same statement.

Cheers,

--
Vincent

別甾虛僞 2024-07-30 20:07:35

有两种方法可以解决此问题,最好的方法取决于您未提供的详细信息。 一种方法是使用存储库模式,引入一个跟踪对象的软件层,并通过将您提供的对象与其内部对象存储进行比较来管理更新/插入问题。 另一种(更程序化)方法是首先查询具有给定 PK 的对象,如果存在,则使用更新,如果不存在,则进行插入。

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.

千秋岁 2024-07-30 20:07:35

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

you should use #include and using namespace oracle::occi;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文