iSeries DB2 - 有没有办法从插入语句中选择标识值?

发布于 2024-07-26 20:28:53 字数 479 浏览 7 评论 0原文

我知道我们很少,我们这些可怜的人使用 iSeries for DB2/AS400,但我希望有人能回答这个简单的问题。 有没有办法从插入语句返回标识值而不使用两行 SQL? 我被迫在 C# 中使用内联 SQL 来执行插入,然后我需要使用为插入生成的标识来执行稍后的操作。 简而言之,我需要 iSeries DB2 相当于 Oracle 的“RETURNING”。 即,

INSERT INTO AwesomeTable (column1, column2, etc.)
    VALUES (value1, value2, etc.)
    RETURNING something;

有人吗? 提前致谢。

编辑:除非有人知道我可以在一个 IBM.Data.DB2.iSeries.iDB2Command (不是存储过程)中执行两行 SQL 的方法,否则我想在一行 SQL 中完成这一切

I know we're rare, us poor folk that are using iSeries for DB2/AS400, but I'm hoping someone can answer this simple question. Is there any way to return the identity value from an insert statement without using two lines of SQL? I'm being forced to use inline SQL in C# to perform an insert, and then I need to use the identity generated for the insert for something later on. Simply put, I need the iSeries DB2 equivalent of Oracle's "RETURNING." I.e.,

INSERT INTO AwesomeTable (column1, column2, etc.)
    VALUES (value1, value2, etc.)
    RETURNING something;

Anyone? Thanks in advance.

EDIT: Unless someone knows of a way I can execute two lines of SQL in one IBM.Data.DB2.iSeries.iDB2Command (not a stored proc), I would like to do this all in one line of SQL

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

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

发布评论

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

评论(3

假装不在乎 2024-08-02 20:28:53

我不确定 iSeries,但以下内容适用于 db2v8.1:

考虑“ID”是您的身份列的名称。 以下 stmt 将返回新生成的 id(与 insert stmt 插入的 id 相同):

SELECT ID FROM FINAL TABLE (
    INSERT INTO AwesomeTable (column1, column2, etc.)
            VALUES (value1, value2, etc.)    
    )

我在 publib 站点上找到的一些解释:(我用它作为参考来测试上面的查询)

     /* The following SELECT statement references an INSERT statement in its
           FROM clause.  It inserts an employee record from host variables into
           table company_b.  The current employee ID from the cursor is selected
           into the host variable new_id.  The keywords FROM FINAL TABLE
           determine that the value in new_id is the value of ID after the
           INSERT statement is complete.

           Note that the ID column in table company_b is generated and without
           the SELECT statement an additional query would have to be made in
           order to retreive the employee's ID number.
        */
        EXEC SQL SELECT ID INTO :new_id
                 FROM FINAL TABLE(INSERT INTO company_b
                 VALUES(default, :name, :department, :job, :years, :salary, 
                        :benefits, :id));

希望这可以帮助 :)

I am not sure of iSeries, but the following worked on db2v8.1:

Consider 'ID' is the name of your identity column. The following stmt will return the newly generated id (the same one that gets inserted by the insert stmt):

SELECT ID FROM FINAL TABLE (
    INSERT INTO AwesomeTable (column1, column2, etc.)
            VALUES (value1, value2, etc.)    
    )

Some explanation I found on the publib site: (I used it for reference to test my query above)

     /* The following SELECT statement references an INSERT statement in its
           FROM clause.  It inserts an employee record from host variables into
           table company_b.  The current employee ID from the cursor is selected
           into the host variable new_id.  The keywords FROM FINAL TABLE
           determine that the value in new_id is the value of ID after the
           INSERT statement is complete.

           Note that the ID column in table company_b is generated and without
           the SELECT statement an additional query would have to be made in
           order to retreive the employee's ID number.
        */
        EXEC SQL SELECT ID INTO :new_id
                 FROM FINAL TABLE(INSERT INTO company_b
                 VALUES(default, :name, :department, :job, :years, :salary, 
                        :benefits, :id));

Hope this helps :)

抠脚大汉 2024-08-02 20:28:53

您需要使用 IDENTITY_VAL_LOCAL 标量函数。 来自 IBM 文档

IDENTITY_VAL_LOCAL
非确定性函数
返回最近分配的
标识列的值。

例子:

CREATE TABLE EMPLOYEE
    (EMPNO INTEGER GENERATED ALWAYS AS IDENTITY,
     NAME CHAR(30),
     SALARY DECIMAL(5,2),
     DEPT SMALLINT)

INSERT INTO EMPLOYEE
    (NAME, SALARY, DEPTNO)
    VALUES('Rupert', 989.99, 50)

SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1

You need to use the IDENTITY_VAL_LOCAL scalar function. From the IBM documentation:

IDENTITY_VAL_LOCAL is a
non-deterministic function that
returns the most recently assigned
value for an identity column.

Example:

CREATE TABLE EMPLOYEE
    (EMPNO INTEGER GENERATED ALWAYS AS IDENTITY,
     NAME CHAR(30),
     SALARY DECIMAL(5,2),
     DEPT SMALLINT)

INSERT INTO EMPLOYEE
    (NAME, SALARY, DEPTNO)
    VALUES('Rupert', 989.99, 50)

SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1
番薯 2024-08-02 20:28:53

这是一个示例:

CREATE TABLE AUTOINC (                                       
   AUTO91 INTEGER       GENERATED ALWAYS AS IDENTITY,          
   SCDS91 CHAR(35)      NOT NULL DEFAULT '',                   
   MCLD91 DECIMAL(3,0)  NOT NULL DEFAULT 0,                    
   CONSTRAINT PK_AUTOINC PRIMARY KEY(AUTO91));

// 注意自动增量字段所在的默认关键字。

insert into AUTOINC Values( default ,'SYSC' , 0 )

// 并使用该函数返回最后一个标识列值。

// 注意:仅获取第一行。

select **IDENTITY_VAL_LOCAL**() from AUTOINC **fetch first row only**

Here's an example :

CREATE TABLE AUTOINC (                                       
   AUTO91 INTEGER       GENERATED ALWAYS AS IDENTITY,          
   SCDS91 CHAR(35)      NOT NULL DEFAULT '',                   
   MCLD91 DECIMAL(3,0)  NOT NULL DEFAULT 0,                    
   CONSTRAINT PK_AUTOINC PRIMARY KEY(AUTO91));

// Notice the default keyword where the auto increment field is.

insert into AUTOINC Values( default ,'SYSC' , 0 )

// And using the function to return the last identity column value.

// Note: fetch first row only.

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