Re: identitycolumn as pk

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: David Gugick (davidg-nospam_at_imceda.com)
Date: 12/03/04


Date: Fri, 3 Dec 2004 09:34:38 -0500

Ivo Grootjes wrote:
> Hi NG,
>
> This would be my first post on a newsgroup so bare with me :)
>
> I'm working on an existing application which uses a database with
> tables which have identity columns as primarykeys. The webapplication
> needs new functionality which requires 4 tables to be copied from one
> database to another. The tables are related to eachother. For
> instance there's a chapter table and a publication table. a chapter
> can contain 0..* publications. The chapter table also has a relation
> toitself (allowing a hierarchical structure). I'm receiving the data
> through a asp.net dataset which contains 4 datatables. What i want to
> do is insert the data in the new database through 4 batch inserts but
> since i only know the primarykey after i've inserted a record i can't
> do this.
>
> What i'm doing right now to overcome this issue is loop through the
> asp.net datatables and insert the records one by one and store the
> generated keys so i can use them for related records. Because the
> tables consist of tens of thousands of records this is slow and not
> the preffered way of doing things.
>
> What would be a good solution to this. Should i generate a primarykey
> myself outside of the database and change the database schema? I have
> the feeling that that is the way to go but i'm not really sure. I
> think using identity columns as primary keys is a commonly used
> technique so i bet there must be other solutions to this problem.
> Changing the database schema ultimately is an option but we prefer
> not to.
>
> Can anybody point out some tips, reading material, or anything else
> which might help me to find a good solution to this problem?
>
> Thank in advance

I think what you're saying is that you have conflicts in your identity
values in the two databases.

You can use SET IDENTITY_INSERT ON <table_name> ON

in order to insert into the table without SQL Server creating the
identity value for you.

When you are done, you probably should run:

DBCC CHECKIDENT('table_name', RESEED)

to make sure SQL Server knows what number to use next.

-- 
David Gugick
Imceda Software
www.imceda.com 


Relevant Pages

  • SET IDENTITY_INSERT tablename ON problem
    ... I'm updating an existing SQL 2000 database. ... A couple of my tables use identity columns. ... I use the ADO ...
    (microsoft.public.vb.database.ado)
  • RE: Copying the Contents of a Table from One Database to Another
    ... as long as there are no identity columns. ... MCITP: Database Administrator ... MCTS: SQL Server 2005 ... "Chaplain Doug" wrote: ...
    (microsoft.public.sqlserver.mseq)
  • Re: identitycolumn as pk
    ... >> tables which have identity columns as primarykeys. ... >> database to another. ... Should i generate a primarykey ... working with identitycolumns as PKs the id for Chapter1 will only be known ...
    (microsoft.public.sqlserver.programming)
  • Re: Identity columns with ClientDataSets?
    ... "How do you handle Identity columns with SQL Server and ClientDataSets?" ... > In my opinion you are doing too much work if your database is SQL server. ... In my opinion, that is more work. ...
    (borland.public.delphi.database.ado)
  • Re: SET IDENTITY_INSERT tablename ON problem
    ... A couple of my tables use identity columns. ... > database I add a new tmp table with the new structure. ... > to work I need to set IDENTITY_INSERT ON for the tmp table. ... > The next command is to insert the data but I get an error that says I ...
    (microsoft.public.vb.database.ado)