Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Small Identity and PK script

Posted on 2006-10-24
2
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

636 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