[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Initiliaize IDENTITY field

Posted on 2005-04-24
4
Medium Priority
?
156 Views
Last Modified: 2010-03-19
Hi

   I want to initialize  an IDENTITY field in a table. Could someone give me a script to do it.

Thanks a lot
0
Comment
Question by:b3cf
  • 2
2 Comments
 
LVL 28

Accepted Solution

by:
rafrancisco earned 100 total points
ID: 13855543
If you want to manually set the value of an IDENTITY field in a table when inserting records, you simply set the IDENTITY_INSERT for the table to ON, like this:

CREATE TABLE Products(
ID INT IDENTITY PRIMARY KEY,
Name VARCHAR(40))
GO

SET IDENTITY_INSERT Products ON
GO

INSERT INTO Products VALUES (100, 'Product 1')
GO

SET IDENTITY_INSERT Products OFF
GO
0
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13855548
But if you want to reset the starting value for your IDENTITY column, you use the DBCC CHECKIDENT command.

DBCC CHECKIDENT (Products, RESEED, 1000)
GO

This example forces the current identity value in the Products table to a value of 1000.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question