SCOPE_IDENTITY() returning NULL

瀏覽次數:1,741 次
跳到第一則未讀訊息

Gili

未讀,
2014年7月2日 凌晨2:22:142014/7/2
收件者:h2-da...@googlegroups.com
Hi,

I am invoking the following statements using Flyway:

INSERT INTO permission VALUES();
INSERT INTO users (version, last_modified, insert_permission_id) VALUES (0, NOW(), SCOPE_IDENTITY());

permission has a single IDENTITY column. I am expecting SCOPE_IDENTITY() to return the inserted ID in the second statement. Any ideas why this is not happening?

I am using H2 1.3.176.

Thanks,
Gili

Noel Grandin

未讀,
2014年7月2日 凌晨4:09:252014/7/2
收件者:h2-da...@googlegroups.com


On 2014-07-02 08:22 AM, Gili wrote:
> permission has a single IDENTITY column. I am expecting SCOPE_IDENTITY() to return the inserted ID in the second
> statement. Any ideas why this is not happening?
>

Do you have a standalone test case?

This is working for me:

create table permission(id identity);
create table users(version int, permission_id int, foreign key (permission_id) references permission(id));
insert into permission values();
insert into users values (0, scope_identity());

cowwoc

未讀,
2014年7月2日 上午11:56:112014/7/2
收件者:h2-da...@googlegroups.com
Hi Noel,

Let me know if this works for you:

CREATE TABLE permission (id IDENTITY);
CREATE TABLE users (id IDENTITY, version INT NOT NULL, last_modified
TIMESTAMP NOT NULL,
insert_permission_id BIGINT NOT NULL);
INSERT INTO permission VALUES();
INSERT INTO users (version, last_modified, insert_permission_id) VALUES
(0, NOW(), SCOPE_IDENTITY());

PS: Replacing SCOPE_IDENTITY() with IDENTITY() fixes the problem but
it's not clear why.

And a follow-up question: SCOPE_IDENTITY() says "changes [...] within
Java functions are ignored". What are "Java functions" in this context?
I am only aware of triggers.

Thanks,
Gili

Noel Grandin

未讀,
2014年7月3日 凌晨3:04:362014/7/3
收件者:h2-da...@googlegroups.com
SCOPE_IDENTITY() appears to only reflect sequence values from the current statement.

I'll update the docs to this effect.

cowwoc

未讀,
2014年7月3日 上午10:48:242014/7/3
收件者:h2-da...@googlegroups.com
Thank you.

I assume SCOPE_IDENTITY() was added for compatibility with other
databases. Is this behavior (only reflecting sequence values) consistent
with that? Or do you need to open a bug report to improve its
compatibility? (I'm happy to simply use IDENTITY() but others might care)

Gili

Noel Grandin

未讀,
2014年7月3日 上午10:56:352014/7/3
收件者:h2-da...@googlegroups.com
I don't know since it predates my involvement with H2, but I'm guess it was added for compat with SQL-Server.
see: http://msdn.microsoft.com/en-us/library/ms190315.aspx

Based on that description, it seems to be working largely correctly.

cowwoc

未讀,
2014年7月3日 上午10:57:442014/7/3
收件者:h2-da...@googlegroups.com
Makes sense. Thanks!

Gili

Gili

未讀,
2014年9月5日 凌晨12:52:152014/9/5
收件者:h2-da...@googlegroups.com
On second thought I think this issue should be reopened.

  1. The microsoft documentation doesn't say this function is limited to sequences. I think it should work on all tables.
  2. I need a way to get the AUTO_INCREMENT column of a row I just inserted, but my triggers are causing IDENTITY() to return the wrong value. All the forums recommend using SCOPE_IDENTITY() for this purpose, but it doesn't work for H2.
Is there a workaround I can use in the meantime?

Thanks,
Gili

Gili

未讀,
2014年9月5日 凌晨1:02:312014/9/5
收件者:h2-da...@googlegroups.com
To be clear, I believe my definition of SCOPE_IDENTITY() is a function that returns the same value as http://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getGeneratedKeys()

Gili

Gili

未讀,
2014年9月5日 凌晨1:39:062014/9/5
收件者:h2-da...@googlegroups.com
Noel, I think you were wrong about this only working for sequences.

I ran into http://stackoverflow.com/a/12319823/14731 which states that SET resets the value of SCOPE_IDENTITY() and provides a workaround (which worked for me).

Is there an open bug report for the fact that SET should not reset SCOPE_IDENTITY()?

Gili

On Friday, September 5, 2014 12:52:15 AM UTC-4, Gili wrote:

Gili

未讀,
2014年9月22日 下午5:35:122014/9/22
收件者:h2-da...@googlegroups.com
回覆所有人
回覆作者
轉寄
0 則新訊息