ORACLE中使用usjng OCI(c++)插入查询出现问题
问题陈述:- 如果数据库中已存在该记录(重复==>主键存在),我将向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 MERGE 语句。 除此之外,它还允许简单的 UPSERT(它实际上允许对行集进行 UPSERT,而不仅仅是单行)。 考虑:
即:您可以使用相同的语句插入和更新。
干杯,
--
文森特
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:
i-e: You can insert and update using the same statement.
Cheers,
--
Vincent
有两种方法可以解决此问题,最好的方法取决于您未提供的详细信息。 一种方法是使用存储库模式,引入一个跟踪对象的软件层,并通过将您提供的对象与其内部对象存储进行比较来管理更新/插入问题。 另一种(更程序化)方法是首先查询具有给定 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.
您应该使用 #include 并使用名称空间 oracle::occi;
you should use #include and using namespace oracle::occi;