dba123
Yak Posting Veteran
90 Posts |
Posted - 2006-08-01 : 16:52:02
|
I have 2 problems:1) When I run this, etch time I keep getting the error saying that constraint isn't found when I try to drop because my creation of the constraint at the end for some reason isn't creating it or being run2) The first insert doesn't insert anything, although I get no errorsALTER PROCEDURE Categories_I3) I also get this when trying to run just the first 2 insert statements togetherMsg 2627, Level 14, State 1, Line 7Violation of PRIMARY KEY constraint 'Category_PK'. Cannot insert duplicate key in object 'Category'ASBEGINDELETE CategoryDBCC CHECKIDENT ('Category', RESEED, 0)ALTER TABLE [Category] DROP CONSTRAINT Category_Category_FK1SET IDENTITY_INSERT Category ONINSERT INTO Category(CategoryId, ParentCategoryID, [Name], [Description], DisplayOrder, DisplayInExplorer, Keywords, Active, CreateDate, CreateUserID)SELECT 1, 1, CategoryName, '', 1, 1, '', 1, GETDATE(), 1 FROM CategoriesStaging WHERE CategoryName = 'All'INSERT INTO Category(CategoryID, ParentCategoryID, [Name], [Description], DisplayOrder, DisplayInExplorer, Keywords, Active, CreateDate, CreateUserID)SELECT 2, 1, CategoryName, '', 1, 1, '', 1, GETDATE(), 1 FROM CategoriesStaging WHERE CategoryName = 'Store'SET IDENTITY_INSERT Category OFF/* Finally, insert the rest and match on the Parent Category Name based on the CategoryStaging table*/WHILE (@@ROWCOUNT <> 0)BEGIN INSERT INTO Category (ParentCategoryID, [Name], [Description], DisplayOrder, DisplayInExplorer, Keywords, Active, CreateDate, CreateUserID, UpdateDate, UpdateUserID) SELECT c.CategoryID, s.CategoryName, '', 1, 1, '', 1, GETDATE(), 1, GETDATE(), 1 FROM Category c INNER JOIN CategoriesStaging s ON c.[Name] = s.ParentCategoryName WHERE NOT EXISTS (SELECT 1 FROM Category c WHERE s.[CategoryName] = c.[Name])END ALTER TABLE [Category] ADD CONSTRAINT [Category_Category_FK1] FOREIGN KEY ( [ParentCategoryID] ) REFERENCES [Category] ( [CategoryID] )END |
|