Solved

Sql Server Identity seed on/off by code

Posted on 2010-11-17
8
484 Views
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
0
Comment
Question by:jaykhatri
  • 4
  • 3
8 Comments
 
LVL 57

Accepted Solution

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

SET IDENTITY INSERT ur_tableName ON;

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

SET IDENTITY INSERT ur_tableName OFF;

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.
0
 

Author Comment

by:jaykhatri
Comment Utility
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,
 CONSTRAINT [PK_ac_nature2] PRIMARY KEY CLUSTERED
(
      [ac_natureid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

SET IDENTITY_INSERT ac_nature2 ON
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.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
>> 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
0
 
LVL 6

Expert Comment

by:subhashpunia
Comment Utility
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:

http://blog.sqlauthority.com/2009/05/03/sql-server-add-or-remove-identity-property-on-column/
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jaykhatri
Comment Utility
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
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
>> 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.
0
 

Author Comment

by:jaykhatri
Comment Utility
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
0
 
LVL 57

Expert Comment

by:Raja Jegan R
Comment Utility
Welcome Jay and wish you good luck in your learning..
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now