Archive and Restore procedureProblem

  • Hello,

    I have written a stored procedure for archival and restore for our application's

    production database

    Its working fine. Archival works with respect to a specific id.

    In this application user usually archives petricular project data.

    With lots of relationship being made in the database it became very easy

    to write a archival/Restore SP.

    Durign data archival the procedure deletes the data in the actual database.

    The problem is since the production database grows on time the rows ids(primary key used for data archival procedure) become same in archive and production database. So

    during restore i will get the primary key violation problem.

    Give me some solution so that i can change my archival logic.

    Thanks.

    Anand

  • Identity values should not "reset" unless you TRUNCATE the table. So, just make sure that you use DELETE instead of TRUNCATE.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • i am using delete. Lets me give an example

    Lets say there are 3 tables

    Table1(a1,b1,c1,d1) a1 is primary key

    Values....

    1,b1,c1,d1

    2,b1,c1,d1

    3,b1,c1,d1

    Table2(a2,a1,c1,d1) a2 is primary key and a1 is foregin key ref.. Table1

    Values....

    1,1,c1,d1

    2,1,c1,d1

    Table3(a3,a1,c3,d3) a3 is primary key and a1 is foregin key ref.. Table1

    Values....

    1,1,c3,d3

    2,1,c3,d3

    3,2,c3,d3

    4,2,c3,d3

    When i archive data with a1=1 then data will be like this...

    Table 1

    2,b1,c1,d1

    3,b1,c1,d1

    Table2(a2,a1,c1,d1) a2 is primary key and a1 is foregin key ref.. Table1

    Values....

    Table3(a3,a1,c3,d3) a3 is primary key and a1 is foregin key ref.. Table1

    Values....

    3,2,c3,d3

    4,2,c3,d3

    Before restoring on time data might still be added...

    Table1

    Value..

    2,b1,c1,d1

    3,b1,c1,d1

    Table2(a2,a1,c1,d1) a2 is primary key and a1 is foregin key ref.. Table1

    Values....

    1,2,c1,d1

    2,2,c1,d1

    3,2,c1,d1

    Table3(a3,a1,c3,d3) a3 is primary key and a1 is foregin key ref.. Table1

    Values....

    3,2,c3,d3

    4,2,c3,d3

    In this condition if we restore the archive data since the Table2 has values which are already

    in archive data it gives primary key violation problem.

    What do we do on this condition.

  • Nope. That is NOT how it works. As I explained earlier, DELETE will NOT reset the Identity counter.

    Here is a test script that I just wrote:Set NoCount ON

    CREATE TABLE dbo.Table1

    (

    ID int NOT NULL IDENTITY (1, 1),

    Val varchar(20) NOT NULL

    ) ON [PRIMARY]

    GO

    ALTER TABLE dbo.Table1 ADD CONSTRAINT

    PK_Table1 PRIMARY KEY CLUSTERED

    (

    ID

    ) ON [PRIMARY]

    GO

    INSERT Into Table1(Val) Select 'Row One'

    INSERT Into Table1(Val) Select 'Row Two'

    INSERT Into Table1(Val) Select 'Row Three'

    GO

    print 'Initial Rows:'

    Select * From Table1

    GO

    Delete From Table1

    Go

    INSERT Into Table1(Val) Select 'New Row Four'

    INSERT Into Table1(Val) Select 'New Row Five'

    GO

    print 'New Rows After Delete:'

    Select * From Table1

    GO

    And here are the results when executed on SQL Server 2000:Initial Rows:

    ID Val

    ----------- --------------------

    1 Row One

    2 Row Two

    3 Row Three

    New Rows After Delete:

    ID Val

    ----------- --------------------

    4 New Row Four

    5 New Row Five

    Try it yourself and let us know if your results are any different.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thats true. but if i set identity then i can not have my(user) own value to be entered there.

  • if i use SET IDENTITY_INSERT TableDelPrim ON and column list then i need to make

    it for whole application. It takes lot of my time.

    Is there any other work around. Pls let me know

  • anbillava (12/29/2008)


    if i use SET IDENTITY_INSERT TableDelPrim ON and column list then i need to make

    it for whole application. It takes lot of my time.

    Is there any other work around. Pls let me know

    Well, actually, you probably should rewrite your app to use SQL Server correctly. In all likelihood, you are going to have many other problems caused by not using Identifiers where you ought to.

    How are you determining your ID values now?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • There are different instance of generating id.. Most of which i can replace by identity. But in one case a random number will created by one of method and also it checks in the database to see the random number is previously created.

  • anbillava (12/29/2008)


    There are different instance of generating id.. Most of which i can replace by identity. But in one case a random number will created by one of method and also it checks in the database to see the random number is previously created.

    I don't see any practical way that you can archive those random ID's. You cannot check for a conflict if they are not in the database anymore.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • thank you very much brother. i will look into that... and keep you post on further updates

  • With the identity after deleting some of the item its possible to insert same id values

    with identity insert option setting on. Is there any way to override this. ?

  • You should not be using the IDENTITY INSERT option, except for restoring from your archive. You should let SQL assign the identity values for all new records.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • If i have understood u correct.....

    Those ids will change if i do so. Because these ids are interlinked

    with other tables.

    For e.g

    I have a transaction like this

    Table1 with column idTable1, time, name,

    Table 2 with column idTable2, idTable1,value1, value2,value3.....

    Here i am inserting table1 data first. With that i will get idTable1 and then i will go for

    inserting Table2... It happens in one shot.

    If i allow this to happen through identity. I dont know how i can track idTable1

    easily.

    hope u will understand this.

  • Yep, I believe that I am following it OK.

    What you want to use is the SCOPE_IDENTITY() function, which returns the last identity value (for any table) that you created in the current scope. So typically you use it like this:INSERT Into (val) 'New row X';

    SELECT SCOPE_IDENTITY();This returns the new identity value to your client program. If you want to keep it in your SQL session and then just keep going using the new identity, you would use a variable like this:

    Declare @id1 int;

    INSERT Into Table1 (val)

    Select 'New row X';

    SELECT @id1 = SCOPE_IDENTITY();

    INSERT Into Table2 (idTable1, val2)

    Select @id1, 'New child row Y';

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • I am feeling better now. Thank you very much...

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic. Login to reply