So, in our fake application I have a table called testing.dbo.will which can be created with this command:
To add data use this command:USE [Testing]GO/****** Object: Table [dbo].[will] Script Date: 06/29/2012 15:00:44 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[will]([GroupID] [int] NOT NULL,[UserID] [int] NOT NULL) ON [PRIMARY]GO
INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (1, 1)Now that we have a sample table, we can create a new table AND copy table from this original table with this command:INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (1, 2)INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (1, 3)INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (2, 1)INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (2, 1)INSERT INTO [Testing].[dbo].[Will] (GroupID, UserID) VALUES (2, 1)
To verify we have an exact match I wrote this command to compare the two tables. Any results will indicate records that are not identical. Ideally, there will be none.SELECT *INTO [testing].[dbo].[new]FROM [testing].[dbo].[will]
Once I am positive I have an exact match (i.e., there are no records when I run the above query) I will simply remove records from the original table:SELECT MIN(TableName) as TableName, GroupId, UserIDFROM(SELECT Table A as TableName, Orig.*FROM testing.dbo.will AS OrigUNION ALLSELECT Table B as TableName, Archive.*FROM testing.dbo.new AS Archive) tmpGROUP BY GroupId,UserIDHAVING COUNT(*) = 1
DELETEThis way, we have a clean slate and our users are now able to do anything they choose. So, to restore users, we use this approach:
FROM [Testing].[dbo].[Will]
Again, a quick sanity check to identify and potential misses:INSERT INTO [Testing].[dbo].[will]SELECT *FROM [Testing].[dbo].[New]
And, a final clean up sweep:SELECT MIN(TableName) as TableName, GroupId, UserIDFROM(SELECT Table A as TableName, Orig.*FROM testing.dbo.will AS OrigUNION ALLSELECT Table B as TableName, Archive.*FROM testing.dbo.new AS Archive) tmpGROUP BY GroupId, UserIDHAVING COUNT(*) = 1
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N[Testing].[dbo].[will]) AND type in (NU))DROP TABLE [Testing].[dbo].[will]GO
0 comments:
Post a Comment