Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Inserting auto increment values through Identity


Inserting auto increment values through Identity

Author
Message
shad.saleem41
shad.saleem41
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
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.
==============
Crying
Pulivarthi Sasidhar
Pulivarthi Sasidhar
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1447 Visits: 989
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)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
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.
==============
Crying


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.

Cool
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)
shad.saleem41
shad.saleem41
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search