I have a table in SQL Server 2012 that uses a Sequence to generate the primary key using a default constraint.
CREATE TABLE [vfm].[ChangeEvent](
[ChangeEventId] [int] NOT NULL,
[Description] [varchar](100) NOT NULL,
[LegalEffectiveDate] [date] NOT NULL,
[StatusCode] [char](1) NOT NULL,
CONSTRAINT [PK_ChangeEvent] PRIMARY KEY CLUSTERED
(
[ChangeEventId] ASC
)
ALTER TABLE [vfm].[ChangeEvent] ADD CONSTRAINT [DF_ChangeEvent_1] DEFAULT
(NEXT VALUE FOR [vfm].[SEQ_ChangeEventId]) FOR [ChangeEventId]
I'm using NamedParameterJdbcTemplate in Spring 5.x to insert a new record into the table.
StringBuilder sql = new StringBuilder()
.append("insert into vfm.ChangeEvent(Description, LegalEffectiveDate, StatusCode)")
.append(" values (:description, :legalEffectiveDate, :status)");
SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("description", changeEvent.getDescription())
.addValue("legalEffectiveDate", changeEvent.getLegalEffectiveDate())
.addValue("status", "S");
KeyHolder keyHolder = new GeneratedKeyHolder();
namedParameterJdbcTemplate.update(sql.toString(), parameters, keyHolder, new String[] { "ChangeEventId" });
From what I've seen so far, my KeyHolder comes back empty, GENERATED_KEYS = null. Is it possible to configure the .update call to retrieve the generated ChangeEventId? Or am I stuck because the table is using a sequence object?
I believe this is possible with Oracle, but I haven't seen any answers for SQL Server. The SQL Server questions I've seen have used identity columns, not sequences.