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)
 Update on an IDENTITY field

Author  Topic 

mobius
Starting Member

13 Posts

Posted - 2007-08-28 : 14:56:22
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 TRANSACTION
use 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, FileSizeQuota
FROM wwwDepartments WHERE DeptID=259;

DELETE FROM wwwDepartments WHERE DeptID=259;

UPDATE wwwDepartments
SET DeptID=259
WHERE 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=288
where DeptID=289;


Does anyone have an example of how you might do this?

Kristen
Test

22859 Posts

Posted - 2007-08-29 : 07:16:10
Only skimmed the question, apologies if I answer the wrong question.

If there are no new inserts to this table (other than by import from another system) then no point having the IDENTITY attribute at all - let the "other" source worry about assigning that.

If that isn't the case you can INSERT into your table using SET IDENTITY_INSERT MyTable ON (and OFF when you are done) which you should use during import to preserve the source values.

In which case there might also be a need to "assign" a next-value-to-use for SQL Server (either the physically next value, or a completely separated value to distinguish new IDs assigned within this database. For example: I've seen "Odd numbers assigned on this server, even numbers on the other one" [0r multiples of 3, 4, whatever!!] or "Positive on this, Negative on that") [You can do this with DBCC CHECKIDENT]

Kristen
Go to Top of Page
   

- Advertisement -