带有 Oracle 触发器键列的 ODBC

发布于 2024-12-17 11:03:41 字数 967 浏览 0 评论 0原文

我正在尝试更新一些现有代码,这些代码应该通过 ODBC 将数据写入各种数据库(SQL、Access、Oracle),但我在使用 Oracle 时遇到了一些问题,正在寻找任何建议。

我已经使用触发器设置了我的 Oracle 数据库(在线基本教程,我想支持)。

CREATE TABLE TABLE1 (
    RECORDID NUMBER      NOT NULL PRIMARY KEY,
    ID       VARCHAR(40) NULL,
    COUNT    NUMBER      NULL

);
GO

CREATE SEQUENCE TABLE1_SEQ 
GO

CREATE or REPLACE TRIGGER TABLE1_TRG
BEFORE INSERT ON TABLE1
FOR EACH ROW
    WHEN (new.RECORDID IS NULL)
    BEGIN
        SELECT TABLE1_SEQ.nextval 
        INTO :new.RECORDID 
        FROM dual;
    end;
GO

然后,我使用 SELECT * FROM TABLE1 填充 DataTable。第一个问题是这个 DataTable 不知道 RecordId 列是自动生成的。如果我的表中有数据,那么我无法更改它,因为我收到错误

一旦类型为“Double”的 DataColumn 的自动增量发生变化,就无法更改 有数据。

如果我继续忽略这一点,那么我很快就会陷入困境。如果我创建一个新的 DataRow 并尝试插入它,我无法将 RecordID 设置为 DBNull.Value,因为它抱怨该列必须为非空 (NoNullAllowedException)。然而,我无法自己生成一个值,因为我不知道我真正应该使用什么值,并且不想通过使用下一个可用值来搞砸触发器。

关于如何在 ODBC 不抱怨的情况下插入数据有什么建议吗?

I'm trying to update some existing code that is supposed to write data to a variety of Databases (SQL, Access, Oracle) via ODBC, but I'm having a few problems with Oracle and am looking for any suggestions.

I've set my Oracle database up using a Trigger (basic tutorial online, which I'd like to support).

CREATE TABLE TABLE1 (
    RECORDID NUMBER      NOT NULL PRIMARY KEY,
    ID       VARCHAR(40) NULL,
    COUNT    NUMBER      NULL

);
GO

CREATE SEQUENCE TABLE1_SEQ 
GO

CREATE or REPLACE TRIGGER TABLE1_TRG
BEFORE INSERT ON TABLE1
FOR EACH ROW
    WHEN (new.RECORDID IS NULL)
    BEGIN
        SELECT TABLE1_SEQ.nextval 
        INTO :new.RECORDID 
        FROM dual;
    end;
GO

I then populate a DataTable using a SELECT * FROM TABLE1. The first problem is that this DataTable doesn't know that the RecordId column is auto-generated. If I have data in my table then I can't alter it because I get a error

Cannot change AutoIncrement of a DataColumn with type 'Double' once it
has data.

If I continue, ignoring this, then I quickly get stuck. If I create a new DataRow and try to insert it, I can't set RecordID to DBNull.Value because it complains that the column has to be non-null (NoNullAllowedException). I can't however generate a value myself, because I don't know what value I should be using really, and don't want to screw up the trigger by using the next available value.

Any suggestions on how I should insert data without ODBC complaining?

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

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

发布评论

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

评论(1

南冥有猫 2024-12-24 11:03:41

您的第一个问题似乎不是与 Oracle 数据库有关。 Oracle 中不存在“自动增量”列这样的东西。您确定该消息来自 Oracle 数据库吗?

使用 Oracle,您应该能够在插入时为主键提供任何虚拟值,并且触发器将覆盖它。

您提供的描述中也没有任何内容可以阻止您在 Oracle 中更新此值(因为您的触发器仅在插入时触发),除非您有对该键的外键引用。

It does not appear that your first problem is with an Oracle database. There is no such thing as an "Autoincrement" column in Oracle. Are you sure that message is coming from an Oracle database?

With Oracle, you should be able to provide any dummy value on insert for the primary key, and the trigger will overwrite it.

There is also nothing in your provided description that would prevent you from updating this value in Oracle (since your trigger is on insert only) unless you have foreign key references to the key.

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