Solved

Small Identity and PK script

Posted on 2006-10-24
2
255 Views
Last Modified: 2012-06-27
Hi guys,

I've got a table in which I have two columns, ID and URL. I would like to have a script which can set the ID column to be the PK and enable an identity starting from 1 increasing with 1.

NOTE: The table already contains 5000 records which I don't want to loose.

Can anyone help me with this?

Thanks!

Wim Toremans
0
Comment
Question by:Wimmeke
2 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 17794418
Hi,

I assume the ID column already has values in  - do you want to keep those ID values?
0
 
LVL 10

Accepted Solution

by:
AaronAbend earned 125 total points
ID: 17794421
create a new table with an incremental id, set identity_insert to allow inserts of old records, update those records to use negative numbers, then turn identity_insert off and you are ready to go

create table newtable (id int identity(1,1), url varchar(1234))

set identity_insert  newtable on

insert newtable (id, url) select oldid, oldurl from oldtable

update newtable set id=id*-1 (to use negative numbers for the old records)

set identity_insert  newtable off
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

17 Experts available now in Live!

Get 1:1 Help Now