我知道我们很少,我们这些穷人使用 iSeries for DB2/AS400,但我希望有人能回答这个简单的问题。有什么方法可以在不使用两行 SQL 的情况下从插入语句返回标识值?我被迫在 C# 中使用内联 SQL 来执行插入,然后我需要使用为插入生成的标识稍后进行某些操作。简单地说,我需要 iSeries DB2 等价于 Oracle 的“返回”。 IE。,
INSERT INTO AwesomeTable (column1, column2, etc.)
VALUES (value1, value2, etc.)
RETURNING something;
任何人?提前致谢。
编辑:除非有人知道我可以在一个 IBM.Data.DB2.iSeries.iDB2Command(不是存储过程)中执行两行 SQL 的方法,否则我想在一行 SQL 中执行所有这些操作
最佳答案
我不确定 iSeries,但以下适用于 db2v8.1:
考虑“ID”是您的身份列的名称。以下 stmt 将返回新生成的 id(与插入 stmt 插入的相同):
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));
希望这可以帮助 :)
关于sql - iSeries DB2 - 有没有办法从插入语句中选择标识值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1109621/