SCOPE_IDENTITY() returning NULL

1,703 views
Skip to first unread message

Gili

unread,
Jul 2, 2014, 2:22:14 AM7/2/14
to 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

unread,
Jul 2, 2014, 4:09:25 AM7/2/14
to 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

unread,
Jul 2, 2014, 11:56:11 AM7/2/14
to 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

unread,
Jul 3, 2014, 3:04:36 AM7/3/14
to 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

unread,
Jul 3, 2014, 10:48:24 AM7/3/14
to 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

unread,
Jul 3, 2014, 10:56:35 AM7/3/14
to 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

unread,
Jul 3, 2014, 10:57:44 AM7/3/14
to h2-da...@googlegroups.com
Makes sense. Thanks!

Gili

Gili

unread,
Sep 5, 2014, 12:52:15 AM9/5/14
to 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

unread,
Sep 5, 2014, 1:02:31 AM9/5/14
to 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

unread,
Sep 5, 2014, 1:39:06 AM9/5/14
to 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

unread,
Sep 22, 2014, 5:35:12 PM9/22/14
to h2-da...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages