0

Below is the table creation script

CREATE TABLE [dbo].[Employee](
    [empId] [int] IDENTITY(9,1) NOT NULL,
    [empName] VARCHAR(255) NOT NULL,
    [empOrgId] [int] NOT NULL,
) 

ALTER TABLE [dbo].[Employee] ADD  CONSTRAINT [DF__Employee__empOrgId]  DEFAULT ((13)) FOR [empOrgId]
GO

when i am trying to insert to tables with simplejdbcinsert

SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
simpleJdbcInsert .withTableName("Employee")
.usingGeneratedKeyColumns("empId");
SqlParameterSource params = new MapSqlParameterSource()
                .addValue("empName","john")
Number number = simpleJdbcInsert.executeAndReturnKey(params);
System.out.println("number"+number);

Note:I want empOrgId defaulted to 13 whenever value is not passed but getting below

Error message:

com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'empOrgId', table 'Employee'; column does not allow nulls. INSERT fails

reddy
  • 79
  • 1
  • 11
  • [Bad Habits to Kick : Declaring VARCHAR without (length)](https://sqlblog.org/2009/10/09/bad-habits-to-kick-declaring-varchar-without-length) It is very unlikely anyone's name will fit in your column `empName`, as it'll only allow a single character. – Thom A Apr 14 '23 at 11:10
  • If you run query in database directly then it set default value? – Dhaval Gajjar Apr 14 '23 at 12:14
  • 2
    Possible dup of https://stackoverflow.com/questions/20497985/why-does-springs-jdbc-templates-doesnt-use-the-tables-default-value – Dan Guzman Apr 14 '23 at 12:26
  • Does this answer your question? [Why does Spring's JDBC Templates doesn't use the tables default value](https://stackoverflow.com/questions/20497985/why-does-springs-jdbc-templates-doesnt-use-the-tables-default-value) – AlwaysLearning Apr 14 '23 at 13:41

0 Answers0