Solved

Small Identity and PK script

Posted on 2006-10-24
2
296 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 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

719 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