473,883 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To make an existing column become an identity column

Hi
(SQL Server 2000)

I have an existing table (t) with a column that is NOT an identity column
(t.ID), but it has manually inserted "row numbers". I want to make this
column become an identity column. This column is a key field to other
tables, so I want to keep the row numbers that are allready inserted.

From the Query Analyzer, how do I do this?

Thanks in advance!

Regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY
Jul 20 '05 #1
3 19075
Try this.

Copy the table by using Export Data. Then using the copied table set your
column to identity and see if any of the numbers change.

Dave
"Gunnar Vøyenli" <gv@edbkonsulen t.no> wrote in message
news:3f******** @news.broadpark .no...
Hi
(SQL Server 2000)

I have an existing table (t) with a column that is NOT an identity column
(t.ID), but it has manually inserted "row numbers". I want to make this
column become an identity column. This column is a key field to other
tables, so I want to keep the row numbers that are allready inserted.

From the Query Analyzer, how do I do this?

Thanks in advance!

Regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY

Jul 20 '05 #2
CJ
Check SET IDENTITY_INSERT in Books Online(BOL). It will allow you to insert
explicit values into an identity field.
CJ
"Gunnar Vøyenli" <gv@edbkonsulen t.no> wrote in message
news:3f******** @news.broadpark .no...
Hi
(SQL Server 2000)

I have an existing table (t) with a column that is NOT an identity column
(t.ID), but it has manually inserted "row numbers". I want to make this
column become an identity column. This column is a key field to other
tables, so I want to keep the row numbers that are allready inserted.

From the Query Analyzer, how do I do this?

Thanks in advance!

Regards,
Gunnar Vøyenli
EDB-konsulent as
NORWAY

Jul 20 '05 #3
Gunnar Vøyenli (gv@edbkonsulen t.no) writes:
I have an existing table (t) with a column that is NOT an identity column
(t.ID), but it has manually inserted "row numbers". I want to make this
column become an identity column. This column is a key field to other
tables, so I want to keep the row numbers that are allready inserted.

From the Query Analyzer, how do I do this?


There is no convenient ALTER TABLE command for this. (Except in SQL
Server CE!). So you need to:

1) Rename the existing table and any constraints it may have.
2) Run a CREATE TABLE statement with the new definition of the column,
including constraints for the table.
3) Isssue SET IDENTITY_INSERT ON for the table to permit inserting of
explicit values in the IDENTITY column.
4) INSERT newtbl (...) SELECT ... FROM oldtbl.
5) Move referencing foreign keys to refer to the new table.
6) Restore triggers and indexes on the new table.
7) drop the old table.

From the Enterprise Manager you can carry out this by point-and-click.
However, what EM performs behind the scenes is something like the above.

--
Erland Sommarskog, SQL Server MVP, so****@algonet. se

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

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

Similar topics

2
14501
by: Jennifer | last post by:
I've got a table with 36+ million rows. I've been asked to modify the table and add in an identity column. The code I used caused SQL to lock up and it maxed out the log files. :) The code I used is: Begin Transaction Alter Table ODS_DAILY_SALES_POS ADD ODS_DAILY_SALES_POS_ID BigInt NOT NULL IDENTITY (1,1) Commit
7
12856
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment (and would be the new PK). But, I would really like to have orders with the oldest OrderDate having the smallest ID number and, for a same OrderDate, I'd to have the smallest CustomerID first. So my question is:
1
2313
by: ibm_97 | last post by:
DB2 8.2 on AIX Hi, I'd like to change a column's name in a table which is part of replication. This column is identity column (generated always). 1. Since DB2 will drop and recreate the table with the changed column name, do I have to do something like 'set integrity off'?
2
5470
by: WhiteEagl | last post by:
Hello, I would need some help with this identity column problem in SQLServer. I have a database with two tables. The Parent table has an Identity column. Parent (ParentID IDENTITY, Name) Child (ChildID, Name, ParentID)
1
19815
by: smauldin | last post by:
Creating a table with an identity column works fine create table test(a integer, id integer generated always as identity ) When I attempt to add an identity column it fails. create table test2(a integer) alter table test2 add column id integer generated always as identity DB21034E The command was processed as an SQL statement because it was not a
5
32061
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as the INSERTS are happening in a different session. Eg, We have the following table....
7
25328
ak1dnar
by: ak1dnar | last post by:
Hi this is my first visit to the MSSQL forum with a question. Let me explain the scenario, I have a table say clients table with the structure like id,foo,etc.. and lots of records on it. But the issue is this id column is not an IDENTITY column. But the values for the Id column don’t repeat since it has handled from the application level.Now I need to change this id column as an IDENTITY column with out loosing the records on the table....
13
2613
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to create a new column in a datatable from two existing columns. I have no problem to create the new column using the datatable.columns.add method. The problem is the value of the new column may become system.dbnull since one of the two existing columns may have system.dbnull. How can I fix it so the new column will get the value of the other column even the other column is system.dbnull?
2
2851
by: kostasgio | last post by:
Hello, this is my first post here, i hope i'll find this forum usefull. Although i did a search about my question, i didnt find what i need , because the question isnt exactly what it sounds. Here's my scenario I have a table that doesnt have an identity column, but does have unique rows depending on a combination of two columns. the table's name is carowner and the two columns i am talking about are iteid,cusid and they are both of...
0
9786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11125
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10407
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9568
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7962
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.