Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Archive and Restore procedureProblem Expand / Collapse
Author
Message
Posted Monday, December 29, 2008 1:54 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, 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

Post #626836
Posted Monday, December 29, 2008 2:12 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 11:54 AM
Points: 9,294, Visits: 9,492
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."
Post #626844
Posted Monday, December 29, 2008 3:04 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, 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.
Post #626862
Posted Monday, December 29, 2008 4:11 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 11:54 AM
Points: 9,294, Visits: 9,492
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."
Post #626895
Posted Monday, December 29, 2008 6:19 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, Visits: 91
Thats true. but if i set identity then i can not have my(user) own value to be entered there.
Post #626935
Posted Monday, December 29, 2008 6:50 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, 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
Post #626946
Posted Monday, December 29, 2008 7:31 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 11:54 AM
Points: 9,294, Visits: 9,492
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."
Post #626966
Posted Monday, December 29, 2008 7:37 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, 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.
Post #626968
Posted Monday, December 29, 2008 7:52 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 11:54 AM
Points: 9,294, Visits: 9,492
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."
Post #626974
Posted Monday, December 29, 2008 7:59 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 7, 2012 1:29 AM
Points: 46, Visits: 91
thank you very much brother. i will look into that... and keep you post on further updates
Post #626975
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse