So, here's one :-P. We've got a test web server right? And an identity field in a table on SQL server for the test server doesn't correspond exactly to the ID field for the Production server. However, the ID field is replicated in multiple tables on an oracle database (bad design... I know). Anyhow, the oracle database gets copied from our production server frequently. This means that any changes we make to reconcile the initial ID field by editing those other columns will just be overwritten every time we copy the tables. Ergo, if we can make the MSSQL ID field value match the one on the production server, it will match the oracle tables whenever they are copied from Production.All that to say, my strategy is:- remove the ID constraint
- if it exists change the ID of the row with the Production ID I want to max(idfield+1)
- change the ID of the row I'm trying to fix to the production value
- Add the ID constraint back starting at max(idfield+2)
Here's what I've got so far:BEGIN TRANSACTIONuse www;--select MAX(DeptID) from wwwDepartments;-- order by DeptID;DECLARE @maxid int;select @maxid=MAX(DeptID) from wwwDepartments;ALTER TABLE wwwDepartments DROP CONSTRAINT PK_wwwDepartments;ALTER TABLE wwwDepartments ALTER COLUMN DeptID int NOT NULL;INSERT INTO wwwDepartments (DeptID, Department, ParentID, Dept_Host, Dept_Path, ColorID, DefaultPage, wwwSite, Template_ID, CalendarCatID, ViewStatus_ID, ApprovalType_ID, Developer_ID, BCode, DepartmentTemplate, HeaderFilename, HideDeptName, TemplateBodyWidth, Defaultmenu, FileSizeQuota)SELECT @maxid+1, Department, ParentID, Dept_Host, Dept_Path, ColorID, DefaultPage, wwwSite, Template_ID, CalendarCatID, ViewStatus_ID, ApprovalType_ID, Developer_ID, BCode, Department Template, HeaderFilename, HideDeptName, TemplateBodyWidth, Defaultmenu, FileSizeQuotaFROM wwwDepartments WHERE DeptID=259;DELETE FROM wwwDepartments WHERE DeptID=259;UPDATE wwwDepartments SET DeptID=259WHERE DeptID=228;ALTER TABLE wwwDepartments ALTER COLUMN DeptID INT NOT NULL; ALTER TABLE wwwDepartments ADD CONSTRAINT [PK_wwwDepartments] PRIMARY KEY NONCLUSTERED ( [DeptID] ASC IDENTITY (@maxid+2,1) )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, FILLFACTOR = 99) ON [PRIMARY]update wwwDepartments set DeptID=288where DeptID=289;
Does anyone have an example of how you might do this?