SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Archive and Restore procedureProblem


Archive and Restore procedureProblem

Author
Message
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
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
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14800 Visits: 9518
Identity values should not "reset" unless you TRUNCATE the table. So, just make sure that you use DELETE instead of TRUNCATE.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
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.
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14800 Visits: 9518
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.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
Thats true. but if i set identity then i can not have my(user) own value to be entered there.
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
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
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14800 Visits: 9518
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?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
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.
RBarryYoung
RBarryYoung
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14800 Visits: 9518
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.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
anbillava
anbillava
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 91
thank you very much brother. i will look into that... and keep you post on further updates
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search