I have this weird problem, I have created a table with four column, and set one column as primary key with int type. I DID NOT set the primary key column as identity, but when I try to insert data in the table I get this error:
Cannot insert explicit value for identity column in table 'Table1' when IDENTITY_INSERT is set to OFF.
I tried to insert without specifying the primary key column (assuming it IS set to IDENTITY(x,y)!), in this this case it doesn’t gives any error, but it doesn’t insert any data neither!
creating the ‘table1’:
USE [NEWVEDJA]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table1](
[id] [int] NOT NULL,
[name] [nvarchar](50) NULL,
[code] [int] NULL,
[exp_1] [nvarchar](max) NULL,
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
and inserting into ‘table1’
INSERT INTO [PARTIAL_NEWVEDJA].[dbo].[Table1]
([id]
,[name]
,[code]
,[exp_1])
VALUES
(1,N'سازمان',null,null), (2,N'معاونت',null,null);
GO
thanks a lot in advance.
With this code, the table is created with ID as PK without the IDENTITY property. All is well.
With this code, you are inserting data into [PARTIAL_NEWVEDJA].[dbo].[Table1], which is a table in another DB. Looks like IDENTITY property is enabled in that table for ID column.
Can you try inserting data into the same table that you have created with your CREATE statement?
Raj