An explicit value for the identity column in table 'product' can only be specified when a column list is used and IDENTITY_INSERT is ON

This is most common error in sql development with identity columns. If you read error it specifies that if you want to pass explicit value in identity column  , please mention all column name.

Below I will give quick implementation

/******First Solution*******/
create table product
(
            [id] int identity primary key,
            [name] nvarchar(50),
            [description] nvarchar(50)
)
Go
Set IDENTITY_INSERT product off
Go
insert into product values
(1,'tv','52 inches '),
(2,'laptops','16 inches '),
(3,'desktop','32 inches ')
Go
select * from product

/******Second Solution*******/
create  table productsales
(
            id int  primary key identity,
            unitprice int,
            quantitysold int
)
Go
Set IDENTITY_INSERT productsale off
Go
insert into productsales
(id ,unitprice ,quantitysold)
values
(1,100,5)

No comments:

Post a Comment