Specify "NEXT VALUE" for INSERT statement using identity column in SQL Server

John Fowler

Consider the following table and SQL from Microsoft's INSERT documentation that deals with IDENTITY columns:

CREATE TABLE dbo.T1 (column_1 int IDENTITY, column_2 VARCHAR(30));
GO

INSERT T1 (column_2) VALUES ('Row #2');

The INSERT statement does not specify column_1 as a column of the table, and SQL Server auto-populates the next value for that identity column. This is the normal way identity columns are handled.

How can I have the same behavior, while also specifying the column name?

For example, I'm looking for something like:

INSERT INTO T1 (column_1, column_2) 
VALUES (NEXT VALUE, 'Row #3');
GO

I don't believe NEXT VALUE works here, but is there something that does work? Is there a key token or function that will indicate that the identity column should be used?

Note: the reason I ask is that the framework I'm using requires all columns to be specified in the column list.

Code Different

If you are on SQL Server 2012 and later, you can use sequence. But you must remove the IDENTITY property from Column1 first. This can only be done by copy-and-rename a new table.

CREATE SEQUENCE Column1_Sequence
    AS int
    START WITH 0;

CREATE TABLE T1
(
    Column1     int DEFAULT (NEXT VALUE FOR Column1_Sequence) PRIMARY KEY
,   Column2     nvarchar(30)
)

After that, you can insert data into the table in 2 ways:

INSERT INTO T1 (Column1, Column2)
    SELECT      NEXT VALUE FOR Column1_Sequence
            ,   'Row #2'

INSERT INTO T1 (Column2)
    SELECT      'Hello world'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert SQL Statement into SQL Server column

From Dev

Insert less than max value in identity column in SQL server

From Dev

Insert statement SQL Server

From Java

How to turn IDENTITY_INSERT on and off using SQL Server 2008?

From Dev

SET IDENTITY_INSERT using a linked server on dynamic SQL

From Dev

Insert into table with select statement plus additional column in SQL Server 2008

From Dev

Insert into table with select statement plus additional column in SQL Server 2008

From Dev

C# Error Inserting into SQL Server Identity Column Using SqlClient

From Dev

SQL Server identity column and imports

From Dev

SQL Server identity column error

From Dev

SQL Server Custom Identity Column

From Dev

Insert statement into SQL Server db

From Dev

Insert statement into SQL Server db

From Dev

what is identity column in sql server ? and what are the types of identity in sql server?

From Dev

How can return identity column value from table and insert other table in SQL Server?

From Dev

How to insert data into sql server database when the primary key column already set default identity(1, 1)

From Dev

Hibernate insert failing when embedded key contains identity column on SQL Server

From Dev

IDENTITY_INSERT and SQL Server Compact

From Dev

using CASE to select column for SET in UPDATE statement IN SQL SERVER

From Dev

SQL insert into statement using select

From Dev

Sql statement using Insert and Case

From Dev

How to insert record in existing column using SQL Server

From Dev

Insert byte into sql server varbinary(max) column using pymssql

From Dev

Using user.identity.Name in SQL statement

From Dev

SQL SERVER How do you insert multiple rows using Insert into select statement

From Dev

SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

From Dev

SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

From Dev

Specify a computed column in SQL Server 2012 as an average

From Dev

How to specify a column name in a SQL Server function?

Related Related

  1. 1

    Insert SQL Statement into SQL Server column

  2. 2

    Insert less than max value in identity column in SQL server

  3. 3

    Insert statement SQL Server

  4. 4

    How to turn IDENTITY_INSERT on and off using SQL Server 2008?

  5. 5

    SET IDENTITY_INSERT using a linked server on dynamic SQL

  6. 6

    Insert into table with select statement plus additional column in SQL Server 2008

  7. 7

    Insert into table with select statement plus additional column in SQL Server 2008

  8. 8

    C# Error Inserting into SQL Server Identity Column Using SqlClient

  9. 9

    SQL Server identity column and imports

  10. 10

    SQL Server identity column error

  11. 11

    SQL Server Custom Identity Column

  12. 12

    Insert statement into SQL Server db

  13. 13

    Insert statement into SQL Server db

  14. 14

    what is identity column in sql server ? and what are the types of identity in sql server?

  15. 15

    How can return identity column value from table and insert other table in SQL Server?

  16. 16

    How to insert data into sql server database when the primary key column already set default identity(1, 1)

  17. 17

    Hibernate insert failing when embedded key contains identity column on SQL Server

  18. 18

    IDENTITY_INSERT and SQL Server Compact

  19. 19

    using CASE to select column for SET in UPDATE statement IN SQL SERVER

  20. 20

    SQL insert into statement using select

  21. 21

    Sql statement using Insert and Case

  22. 22

    How to insert record in existing column using SQL Server

  23. 23

    Insert byte into sql server varbinary(max) column using pymssql

  24. 24

    Using user.identity.Name in SQL statement

  25. 25

    SQL SERVER How do you insert multiple rows using Insert into select statement

  26. 26

    SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

  27. 27

    SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

  28. 28

    Specify a computed column in SQL Server 2012 as an average

  29. 29

    How to specify a column name in a SQL Server function?

HotTag

Archive