Inserting 10 records into IDENTITY column with simple INSERT

Posted by Sarvesh under Sql Server on 9/3/2009 | Views : 2103 | Status : [Member] | Replies : 3
hi experts

In my last week interview, The question asked

question is

you have one table as 'table1' which comprises only one column as 'ColID' set as 'IDENTITY'.

Now, you have to insert 10 records with simple INSERT statement. How will you do ? (important: The IDENTITY value should be inserted automatically, not manually)

But, I don't know the answer. Can anybody pl give me the answer ?

thanks
sarvesh




Responses

Posted by: Pandians on: 9/3/2009 [Member] [MVP] Silver

Up
0
Down
Hi sarvesh

YES. We can do...

1. Table Sample:
CREATE TABLE Table1

(
ColID INT IDENTITY
)

2. Inserting 10 Records with simple INSERT:
INSERT Table1 DEFAULT VALUES

GO 10


I hope this is what you asking. If its not, Pl get back.
Cheers


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Sarvesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sarvesh on: 9/3/2009 [Member] Starter

Up
0
Down
S.. S.. S..

I tested the code you given. working perfectly. THANKS
thanks
sarvesh

Sarvesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SqlDeveloper on: 9/7/2009 [Member] Starter

Up
0
Down
I know the problem is resolved but we can set the identity_insert property of the table and insert identity values directly by a sample code like below

set identity_insert Table1 on


insert into Table1 (colid) values (3)

set identity_insert Table1 off



I hope that helps,
Eralper
http://www.kodyaz.com



Sarvesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response