Sql Server Identity seed on/off by code

Posted on 2010-11-17
Medium Priority
Last Modified: 2012-08-14
Hi Experts,

Just like to make you a bit busy.

I am just converting my software from MS-Access to Sql Server 2005 Express Edition. Have to convert all the my clients databases. I set all the create table script and then I import the data from MS-Access to Sql server using Import Export Wizard of Sql Server. Now I like to put identity specification to almost all the tables. Can you let me have the command to turn it on.

Right now I have to modify each table from designer and enable identity. For one time I like to develop a script and will apply the same to other client's database.

I cannot specify identity on at create table as I have to import the data.

Please let me have the possible solution.

Thanks & Best Regards

Jay Khatri
Question by:jaykhatri
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 57

Accepted Solution

Raja Jegan R earned 2000 total points
ID: 34161467
Hope you have created the Primary key records with IDENTITY property and while inserting records for your table including IDENTITY column, follow the below


INSERT into ur_table_name (IDENTITY_key_column, other_columns_list)
SELECT columns_list
FROM access_other_table


Do note that you can set IDENTITY INSERT property of only one table in a particular database to ON and hence set it to OFF before turning it ON for another table.

Author Comment

ID: 34161730
CREATE TABLE [dbo].[ac_nature2](
      [ac_natureid] [int] IDENTITY(1,1) NOT NULL,
      [ac_nature] [nvarchar](255) COLLATE SQL_Latin1_General_CP850_CS_AS NULL,
      [ac_natureid] ASC

insert into scdb.dbo.ac_nature2 select * from pragati...ac_nature
SET IDENTITY_INSERT scdb.dbo.ac_nature2 OFF

I am doing this

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

and I am getting this.
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34161766
>> An explicit value for the identity column in table 'scdb.dbo.ac_nature2' can only be specified when a column list is used and IDENTITY_INSERT is ON.

In order to avoid this error, you need to specify column names explicitly as mentioned in my comment

INSERT into ur_table_name (IDENTITY_key_column, col1, col2, other_columns_list)
SELECT IDENTITY_key_column, col1, col2, other_columns_list
FROM access_other_table
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.


Expert Comment

ID: 34162611
To know how the Identity property can be enabled in a table through SSMS designer or by T-SQL script please go through this detailed article:


Author Comment

ID: 34163354
Hi subhashpunia,

This is called something like explicit arrangement not the concrete solution provided by the language to achieve this.

If still I have to mention columns in your that example, its better to do mention columns explicitly as suggested by rrjegan17.

Am I right.

I haven't yet worked out over the suggested code. But once I will finish it with success. I will confirm it.

Thanks & Best Regards

Jay Khatri
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34169840
>> If still I have to mention columns in your that example, its better to do mention columns explicitly

Yes, that is the recommended approach whenever you use SET IDENTITY INSERT statements.

Author Comment

ID: 34170241
Hi rrjegan17,

Absolutely perfect solution, I never learned sql with book or any institute. What I learned is msdn and google and rest with my job. So know some stuff what an expert know and even don't know some stuff which even a Beginner may know.

Thanks alot, I am closing the ticket now.

Best Regards

Jay Khatri
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34170351
Welcome Jay and wish you good luck in your learning..

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 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