IDENTITY_INSERT and SQL Server Compact

h bob

The script below creates a table, temporarily turns off auto identity, inserts records whilst specifying the PK value, then re-enables auto identity.

DROP TABLE Foo;
GO

CREATE TABLE Foo (Id int IDENTITY (1,1) NOT NULL, 
                  Bar nvarchar(100) NOT NULL);
GO

ALTER TABLE Foo ADD CONSTRAINT FooConstraint PRIMARY KEY (Id);
GO

SET IDENTITY_INSERT Foo ON;
GO

INSERT INTO Foo(Id, Bar) VALUES (1, 'a');
GO
INSERT INTO Foo(Id, Bar) VALUES (2, 'b');
GO
INSERT INTO Foo(Id, Bar) VALUES (3, 'c');
GO

SET IDENTITY_INSERT Foo OFF;
GO

INSERT INTO Foo(Bar) VALUES ('d');
GO

Problem is that last insert - it complains that

A duplicate value cannot be inserted into a unique index. [ Table name = Foo,Constraint name = FooConstraint ]

This is unexpected. What am I doing wrong?

ErikEJ

You need to reset the seed like this

ALTER TABLE [Foo] ALTER COLUMN [Id] IDENTITY (4, 1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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 Java

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

From Dev

IDENTITY_INSERT ON not working - SQL Server 2008 R2

From Dev

IDENTITY_INSERT ON not working - SQL Server 2008 R2

From Dev

MS SQL Server does not let me set IDENTITY_INSERT to ON

From Dev

SET IDENTITY_INSERT using a linked server on dynamic SQL

From Dev

SET IDENTITY_INSERT ON List In Database SQL Server

From Dev

connecting to sql server compact

From Dev

How to get current identity number of specific table in sql server compact

From Dev

Use SQL Server Compact with ASP.NET Identity

From Dev

How to get current identity number of specific table in sql server compact

From Dev

Use SQL Server Compact with ASP.NET Identity

From Dev

How to SET IDENTITY_INSERT ON in SQL Server 2008 for multiple tables at once

From Dev

Weird error (Msg 8107) on SQL Server 2008 R2 for SET IDENTITY_INSERT

From Dev

Weird error (Msg 8107) on SQL Server 2008 R2 for SET IDENTITY_INSERT

From Dev

Insert an auto increment Key to SQL server Compact database

From Dev

Connect to Sql Server Compact Remotely

From Dev

Entity Framework 6 with SQL Server Compact 4.0

From Dev

Webmatrix csv import to SQL Server Compact

From Dev

SQL Server Compact Edition - Obtain create script

From Dev

what kind of developers use SQL Server Compact

From Dev

Converting Stored Procedure into a query (SQL Server Compact)?

From Dev

Can SqlBulkCopy be used with a SQL Server Compact database?

From Dev

Move record insert to SQL Server CE database into another thread? C# Compact Framework 3.5

From Dev

Data Transfer Between SQL Server and SQL Server Compact

From Dev

EF6 and multiple configurations (SQL Server and SQL Server Compact)

From Dev

Best way to export SQL Server database to sqlite (or SQL Server Compact)

From Dev

Use both, SQL Server Compact and SQL Server with Entity Framework simultaneously

Related Related

  1. 1

    SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

  2. 2

    SQL Server 2008 "IDENTITY_INSERT is ON" error on Insert

  3. 3

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

  4. 4

    IDENTITY_INSERT ON not working - SQL Server 2008 R2

  5. 5

    IDENTITY_INSERT ON not working - SQL Server 2008 R2

  6. 6

    MS SQL Server does not let me set IDENTITY_INSERT to ON

  7. 7

    SET IDENTITY_INSERT using a linked server on dynamic SQL

  8. 8

    SET IDENTITY_INSERT ON List In Database SQL Server

  9. 9

    connecting to sql server compact

  10. 10

    How to get current identity number of specific table in sql server compact

  11. 11

    Use SQL Server Compact with ASP.NET Identity

  12. 12

    How to get current identity number of specific table in sql server compact

  13. 13

    Use SQL Server Compact with ASP.NET Identity

  14. 14

    How to SET IDENTITY_INSERT ON in SQL Server 2008 for multiple tables at once

  15. 15

    Weird error (Msg 8107) on SQL Server 2008 R2 for SET IDENTITY_INSERT

  16. 16

    Weird error (Msg 8107) on SQL Server 2008 R2 for SET IDENTITY_INSERT

  17. 17

    Insert an auto increment Key to SQL server Compact database

  18. 18

    Connect to Sql Server Compact Remotely

  19. 19

    Entity Framework 6 with SQL Server Compact 4.0

  20. 20

    Webmatrix csv import to SQL Server Compact

  21. 21

    SQL Server Compact Edition - Obtain create script

  22. 22

    what kind of developers use SQL Server Compact

  23. 23

    Converting Stored Procedure into a query (SQL Server Compact)?

  24. 24

    Can SqlBulkCopy be used with a SQL Server Compact database?

  25. 25

    Move record insert to SQL Server CE database into another thread? C# Compact Framework 3.5

  26. 26

    Data Transfer Between SQL Server and SQL Server Compact

  27. 27

    EF6 and multiple configurations (SQL Server and SQL Server Compact)

  28. 28

    Best way to export SQL Server database to sqlite (or SQL Server Compact)

  29. 29

    Use both, SQL Server Compact and SQL Server with Entity Framework simultaneously

HotTag

Archive