473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

sql server -- identity problem

MS SQL Server 2000.
My case is: I have the table T with primary key calling __recid int
without identity property. This table includes a lot of records (about
1000000). I need to convert __recid's data type to IDENTITY.

As you know sql sentence: "alter table T alter column __recid int
IDENTITY not null" does not work with not-empty tables.
I use the SQL Enterprise Manager which can convert the field __recid
into identity but I need another way to solve the problem, probably I
should use TSQL. I don't know...

So I need your help.

regards
grzes
Jul 20 '05 #1
5 9833
gj*@o2.pl (grzes) wrote in news:e02bcd3.0411170538.5ab97764
@posting.google.com:
MS SQL Server 2000.
My case is: I have the table T with primary key calling __recid int
without identity property. This table includes a lot of records (about
1000000). I need to convert __recid's data type to IDENTITY.

As you know sql sentence: "alter table T alter column __recid int
IDENTITY not null" does not work with not-empty tables.
I use the SQL Enterprise Manager which can convert the field __recid
into identity but I need another way to solve the problem, probably I
should use TSQL. I don't know...


Easiest method is to create a new table with an IDENTITY column and copy
the rows from the old table into it. Then rename the tables appropriately
so that the new table has the old table's name.

Regards,
Lyle
Jul 20 '05 #2
Please note that you will have to "set identity insert" to be able to
populate the identity column.

Instead of a new table, you MAY be able to get away with

- rename the old key column
- add the new identify
- set identity insert on
- update the identity column
- set identity insert off
- delete original key column

PLEASE NOTE: I have not actually tried this, but it should work in theory.

"Lyle H. Gray" <gr**@no.spam.cs.umass.edu.invalid> wrote in message
news:Xn**********************************@130.81.6 4.196...
gj*@o2.pl (grzes) wrote in news:e02bcd3.0411170538.5ab97764
@posting.google.com:
MS SQL Server 2000.
My case is: I have the table T with primary key calling __recid int
without identity property. This table includes a lot of records (about
1000000). I need to convert __recid's data type to IDENTITY.

As you know sql sentence: "alter table T alter column __recid int
IDENTITY not null" does not work with not-empty tables.
I use the SQL Enterprise Manager which can convert the field __recid
into identity but I need another way to solve the problem, probably I
should use TSQL. I don't know...


Easiest method is to create a new table with an IDENTITY column and copy
the rows from the old table into it. Then rename the tables appropriately
so that the new table has the old table's name.

Regards,
Lyle

Jul 20 '05 #3
David Rawheiser (ra*******@hotmail.com) writes:
Please note that you will have to "set identity insert" to be able to
populate the identity column.

Instead of a new table, you MAY be able to get away with

- rename the old key column
- add the new identify
- set identity insert on
- update the identity column
- set identity insert off
- delete original key column

PLEASE NOTE: I have not actually tried this, but it should work in theory.


It doesn't:

CREATE TABLE test (a int NOT NULL PRIMARY KEY)
go
INSERT test (a) VALUES (123)
go
ALTER TABLE test ADD b int IDENTITY
go
SET IDENTITY_INSERT test ON
go
UPDATE test SET b = a
go

yields:

(1 row(s) affected)
(1 row(s) affected)

Server: Msg 8102, Level 16, State 1, Line 1
Cannot update identity column 'b'.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Thank you, I was rushed, so I didn't take time to research it, before
spouting off.

It still works in 'theory', just not in 'practice' - that whole reality
thing rears its ugly head.

I guess we need to submit an identity_update enhancement.

"Erland Sommarskog" <> wrote in message
news:Xn*********************@127.0.0.1...
David Rawheiser (ra*******@hotmail.com) writes:
Please note that you will have to "set identity insert" to be able to
populate the identity column.

Instead of a new table, you MAY be able to get away with

- rename the old key column
- add the new identify
- set identity insert on
- update the identity column
- set identity insert off
- delete original key column

PLEASE NOTE: I have not actually tried this, but it should work in
theory.


It doesn't:

CREATE TABLE test (a int NOT NULL PRIMARY KEY)
go
INSERT test (a) VALUES (123)
go
ALTER TABLE test ADD b int IDENTITY
go
SET IDENTITY_INSERT test ON
go
UPDATE test SET b = a
go

yields:

(1 row(s) affected)
(1 row(s) affected)

Server: Msg 8102, Level 16, State 1, Line 1
Cannot update identity column 'b'.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #5
David Rawheiser (ra*******@hotmail.com) writes:
Thank you, I was rushed, so I didn't take time to research it, before
spouting off.

It still works in 'theory', just not in 'practice' - that whole reality
thing rears its ugly head.

I guess we need to submit an identity_update enhancement.


Yeah. Or start using SQL Server CE. I'm not using SQL Server CE myself,
but apparently you can use ALTER TABLE to change a column to IDENTITY in
SQL Server CE. Funny, isn't it?

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
17
by: Trevor Best | last post by:
I don't know if this has been reported before but it appears to be a bug with Access. If I create two tables both with an identity column then create an insert trigger on table1 that inserts a...
3
by: Jacek Jurkowski | last post by:
My application is using an identity field of an SQL Server. Identity field called "ID" is a Primary Key too. The problem is when i add some record to the table SQL Server is assigning it sime new...
2
by: .Net Newbie | last post by:
Hello, I am somewhat new to .Net and currently working on an intranet site using C# going against SQL Server 2k. I am accepting personal information on a single webform and trying to insert the...
5
by: HankD | last post by:
I get the following error when I add a new application to our server. If I run aspnet_regiis -i it takes care of the problem but when I add another application I get the same error for the second...
17
by: Jon B | last post by:
Hi All! I have a ASP.NET 2.0 site that works on the Windows 2000 Server. However, when I tried to view this site on my local Windows XP machine, I get "Server Unavailable". If I switch the...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
8
by: Tony Toews [MVP] | last post by:
Thanks to a posting by fellow MVP Steve Foster On a computer that is running Windows Vista, Windows Server 2008, or Windows XP, an incorrect value is returned when an application queries the...
8
by: Brett | last post by:
I wrote an ASP.NET application that queries a SQL Server database (on a different box from the web server) and displays the result in a GridView. The datasource for the GridView is a SQLDataSource....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.