Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 28th, 2003, 05:18 AM
Ckwop Ckwop is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Widnes, England, UK
Posts: 3 Ckwop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Ckwop
Question Converting an Int field to an indentity

Is it possible to convert an int field into an identity field without losing any of the previous values in the int field?

Simon.

Reply With Quote
  #2  
Old November 28th, 2003, 09:25 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Identities are generally in numeric order...so switching from an int field to an Identity field may renumber them but I'm not sure.

Reply With Quote
  #3  
Old November 28th, 2003, 09:27 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
actually, I just tried it and I didn't lose any of the values that were there when I changed it from and int to an identity.

Reply With Quote
  #4  
Old November 28th, 2003, 10:18 AM
Ckwop Ckwop is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Widnes, England, UK
Posts: 3 Ckwop User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Ckwop
What sql string did you use? Alter table alter column [column name] int indentity([LastNumber in the database plus 1,1)?

Reply With Quote
  #5  
Old March 11th, 2004, 01:35 PM
RangeCowboy RangeCowboy is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 RangeCowboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re-creating an Identity field in a second database

I needed to migrate three tables from one database to another.
In my case the BOX table with an identity field is used to index into a second table SERIALS.
In a perfect world no records would be deleted but in my case records do get deleted, so simply copying records and inserting
into a second database creates different identities when there are deletions in the source table.

The solution is as follows using a cursor for the box_id

1. Select max(identity_field) from source table into a variable.

2. Create and populate the target table in the second database with that many records. Fill not null fields with bland data and in one field write an unusual value which cant exist in the source table e.g. Production_Line = 99

3. Iterate through the source table and update the target table with
Update Target set fld1,fld2... where Target.Identity_fld = Source.Identity_fld

4.Finally delete in the Target table all records with Production_Line = 99

In the example below, The lotnumber migration is easy since no identity fields are used, but the serial numbers table is based on the identity field in the box table ( A box contains ten serial numbered items) so simply allowing the identity field in the target database to naturally increment will destroy the box --> serials relationship.

Migrating 300,000 serials in 30,000 boxes takes about 5 minutes


/************************************************** ********************************
** File Name: c_MigrateData_to_Live (from the test database)
**
** Description: Copies Three tables LotNumber --> Box -- DB_Serials to a target database dbo.Live
**
** This is used when you have just created a clean empty database and you have
** another database with Lot-Box-Serials to be migrated
** It first gets the max of Identity in the source database table BOX with Max(Identity Field)
** Then creates this many "empty boxes" with production-line = 99
** Now insert the lotnumber table and then update the target box table data using the identity field
** Then insert serials using the same identity field values.
** Finally delete boxes with prod_line=99
**
** Creation Date: 3/11/2004
**
** Author(s): RangeCowboy
**
** System(s): All
************************************************** *******************************/
CREATE procedure c_MigrateData_to_Live
as
declare @BoxCount int, @Table_Identity_Field int
select @Table_Identity_Field=1
select @BoxCount = max(box_id) from DB_test.dbo.box
while (@Table_Identity_Field < @BoxCount)
begin
insert into DB_live.dbo.box
(
box_val,
prod_line, -- These are three not null fields in my table BOX
name
)
values(@Table_Identity_Field,99,1)
select @Table_Identity_Field = @Table_Identity_Field+1
end



insert into DB_live.dbo.lotnumber --Simple insert from source, No identities
(
Lot_no,
name,
boxcount,
date_val
)
select
Lot_no,
name,
boxcount,
date_val
from DB_test.dbo.Lotnumber


declare
@box_val varchar(18),
@prod_line int,
@pallet_id int,
@partial char(1),
@cnt tinyint,
@name int,
@Hold bit,
@Lot_no varchar(7),
@boxcnt int,
@closed bit



declare tempcur cursor for
select box_id from DB_test.dbo.box
open tempcur
fetch next from tempcur into @Table_Identity_Field
while (@@fetch_status = 0)
begin
select
@box_val=box_val,
@prod_line=prod_line,
@Pallet_id=pallet_id,
@partial=partial,
@cnt=cnt,
@name=name,
@Hold=Hold,
@lot_no=Lot_no,
@boxcnt=boxcnt,
@closed=closed
from DB_test.dbo.box
where box_id = @Table_Identity_Field

update DB_live.dbo.box
set
box_val=@box_val,
prod_line=@prod_line,
Pallet_id=@pallet_id,
partial=@partial,
cnt=@cnt,
name=@name,
Hold=@Hold,
lot_no=@Lot_no,
boxcnt=@boxcnt,
closed=@closed
where box_id = @Table_Identity_Field

fetch next from tempcur into @Table_Identity_Field
End

CLOSE tempcur
DEALLOCATE tempcur

insert into DB_live.dbo.DB_serials
(
serialnumber,
box_id
)
select
serialnumber,
box_id
from DB_test.dbo.DB_serials


delete from DB_live.dbo.box where prod_line=99
GO

Reply With Quote
  #6  
Old March 11th, 2004, 09:12 PM
Doug G Doug G is offline
Grumpier Old Moderator
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Sep 2003
Posts: 10,143 Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level)Doug G User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 19 m 36 sec
Reputation Power: 181
Check the SQL Server Books Online for SET IDENTITY_INSERT ON.

This allows you to transfer a table's current identity values into a new identity column without renumbering.

http://msdn.microsoft.com/library/e...et-set_7zas.asp
__________________
======
Doug G
======
I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain

Reply With Quote
  #7  
Old March 12th, 2004, 01:49 PM
RangeCowboy RangeCowboy is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 2 RangeCowboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by Doug G
Check the SQL Server Books Online for SET IDENTITY_INSERT ON.

This allows you to transfer a table's current identity values into a new identity column without renumbering.

http://msdn.microsoft.com/library/en-us/tsqlref/ts_set-set_7zas.asp

Excellent !

Thank you Doug, I did not know that (obviously)

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Converting an Int field to an indentity


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT