Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Swapping tables

Author  Topic 

alun02
Starting Member

5 Posts

Posted - 2007-07-11 : 10:44:22
Hi guys,

I need to write a script which calculates a set of new rows for a table (takes about 10 mins) and then swaps these new values into a table, removing all the old ones which can then be discarded. The way I'm thinking of doing this is to create a temporary table, populate it with the new values then once that's done removing all entries from the old table and inserting the new ones in. The purpose of the exercise is to minimise the amount of time there is no data in the table, so that queries on the table return successfully as much as possible. Is the temporary table idea the best or is there an alternative mechanism I can use?

Thanks in advance,
Alun

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-11 : 10:52:52
put the rows in the temp table, do the processing.
then just update the values, don't delete and insert them
this way you'll have one operation instead of two.


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

alun02
Starting Member

5 Posts

Posted - 2007-07-11 : 10:55:46
Hi spirit1,

That would work in most cases, but it may be that some of the rows in the original table may need to be deleted in the update, i.e. there may be fewer rows in the table after the 'swap' than there were before.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-07-11 : 10:59:41
well then... i think your way will do just fine.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

alun02
Starting Member

5 Posts

Posted - 2007-07-11 : 11:47:26
Just to let anyone that's interested know, the method I ended up using was to create a new table, populate it with the new values then once that was done renaming the old table, renaming the new one to be the same as the original one and dropping the old table.
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-07-12 : 11:10:36
be aware....when you drop a table...you drop the permissions that go with it...in this instance this technique may not be causing you a problem...but other times it may be, depending on the scurity model you are using....(and I hope you're not using the "sa" userid!!)
Go to Top of Page
   

- Advertisement -