Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Inserting auto increment values through Identity Expand / Collapse
Author
Message
Posted Friday, October 4, 2013 12:25 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 16, 2014 1:38 AM
Points: 9, Visits: 46
Hi
I created a table

CREATE TABLE IdentityProducts(
productid int IDENTITY(1,1) NOT NULL,
productname nvarchar(40) NOT NULL,
categoryid int NOT NULL,
unitprice money NOT NULL)

I want to insert values into it with the belkow statement

insert into IdentityProducts(productname,categoryid,unitprice)
values
('shad',1,100)

when i insert the "productid" should get inserted automatically as it is defined with identity propert
but its not happening
even though i have made

SET IDENTITY_INSERT IdentityProducts ON

I am getting the following error
============
Msg 545, Level 16, State 1, Line 1
Explicit value must be specified for identity column in table 'IdentityProducts' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
==============

Post #1501430
Posted Friday, October 4, 2013 12:39 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:03 AM
Points: 1,062, Visits: 708
Hi,

It should work...

Run the below statement and then insert values..

SET IDENTITY_INSERT IdentityProducts OFF

insert into IdentityProducts(productname,categoryid,unitprice)
values
('shad',1,100)
Post #1501433
Posted Friday, October 4, 2013 12:44 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 6:24 PM
Points: 23,397, Visits: 32,246
shad.saleem41 (10/4/2013)
Hi
I created a table

CREATE TABLE IdentityProducts(
productid int IDENTITY(1,1) NOT NULL,
productname nvarchar(40) NOT NULL,
categoryid int NOT NULL,
unitprice money NOT NULL)

I want to insert values into it with the belkow statement

insert into IdentityProducts(productname,categoryid,unitprice)
values
('shad',1,100)

when i insert the "productid" should get inserted automatically as it is defined with identity propert
but its not happening
even though i have made

SET IDENTITY_INSERT IdentityProducts ON

I am getting the following error
============
Msg 545, Level 16, State 1, Line 1
Explicit value must be specified for identity column in table 'IdentityProducts' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
==============



If you turn IDENTITY_INSERT on for a table, SQL Server is expecting you to provide the values for that column rather than having the values generated automatically. As stated above, turn IDENTITY_INSERT off and try your code again.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1501436
Posted Friday, October 4, 2013 1:47 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 16, 2014 1:38 AM
Points: 9, Visits: 46

Thanks it worked
I re created table and renoved not null after identity property and inserted values it worked


CREATE TABLE IdentityProducts(
productid int IDENTITY(1,1),
productname nvarchar(40) NOT NULL,
categoryid int NOT NULL,
unitprice money NOT NULL)

insert into IdentityProducts(productname,categoryid,unitprice)
values
('shad',1,100)

this worked for me
Post #1501448
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse