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.
Author |
Topic |
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 08:23:32
|
IDENTITY_INSERT is already ON for table 'Elbalazo.dbo.DeliveryOption'. Cannot perform SET operation for table 'City'Here's my entire script:--------------------------------------------------------------------/*Disable Constraints & Triggers*/exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'/*Perform delete operation on all table for cleanup*/exec sp_MSforeachtable 'DELETE ?'/*Enable Constraints & Triggers again*/--exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'/*Reset Identity on tables with identity column*/exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'-- CitySET IDENTITY_INSERT City ONINSERT INTO Elbalazo.dbo.City ( [CityID] ,[CityName] ,[CountyID] ,[Active])SELECT [CityID],[CityName],[CountyID],1FROM [ElbalazoProduction].dbo.tbl_CitySET IDENTITY_INSERT City OFF-- StateSET IDENTITY_INSERT [State] ONINSERT INTO Elbalazo.dbo.State ( [StateID] ,[State] ,[Active])SELECT [StateID],[State],1FROM [ElbalazoProduction].dbo.tbl_StateSET IDENTITY_INSERT [State] OFF-- NumberOfPeopleOptionSET IDENTITY_INSERT NumberOfPeopleOption ONINSERT INTO [Elbalazo].[dbo].[NumberOfPeopleOption] ([NumberOfPeopleOptionID] ,[NumberOfPeopleNameOption] ,[Active])SELECT [NumberOfPeopleID], [NumberOfPeopleName],1FROM [ElbalazoProduction].dbo.tbl_NumberOfPeopleSET IDENTITY_INSERT NumberOfPeopleOption OFF-- DeliveryOptionSET IDENTITY_INSERT DeliveryOption ONINSERT INTO [Elbalazo].[dbo].[DeliveryOption] ([DeliveryOptionID] ,[DeliveryOptionName] ,[Active])SELECT [DeliveryOptionID], [DeliveryOptionName],1FROM [ElbalazoProduction].dbo.tbl_DeliveryOptionSET IDENTITY_INSERT DeliveryOption OFF-- UserSET IDENTITY_INSERT [User] ONINSERT INTO [Elbalazo].[dbo].[User] ([UserID] ,[FirstName] ,[LastName] ,[Address1] ,[Address2] ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[Email] ,[CreateDate] ,[Active])SELECT [CustomerID] ,[FirstName] ,[LastName] ,[AddressLine1] ,NULL ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[EmailPrefix] + '@' + [EmailSuffix] ,[CreateDate] ,1FROM [ElbalazoProduction].dbo.tbl_CustomerSET IDENTITY_INSERT [User] OFF-- EntreeOptionSET IDENTITY_INSERT EntreeOption ONINSERT INTO [Elbalazo].[dbo].[EntreeOption] ([EntreeOptionID] ,[EntreeOptionName] ,[Active])SELECT [EntreeOptionID] ,[EntreeOptionName] ,1FROM [ElbalazoProduction].dbo.tbl_EntreeOptionSET IDENTITY_INSERT EntreeOption OFF-- CateringOrderSET IDENTITY_INSERT CateringOrder ONINSERT INTO [Elbalazo].[dbo].[CateringOrder] ([CateringOrderID] ,[UserID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID])SELECT [CateringOrderID] ,[CustomerID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID]FROM [ElbalazoProduction].dbo.tbl_CateringOrderSET IDENTITY_INSERT CateringOrder OFF-- CateringOrder_EntreeItemSET IDENTITY_INSERT CateringOrderEntreeItem ONINSERT INTO [Elbalazo].[dbo].[CateringOrderEntreeItem] ([CateringOrderEntreeItemID] ,[CateringOrderID] ,[EntreeItemID])SELECT [CateringORder_EntreeItemID] ,[CateringOrderID] ,[EntreeItemID]FROM [ElbalazoProduction].dbo.tbl_CateringOrder_EntreeItemSET IDENTITY_INSERT CateringOrderEntreeItem OFFselect * from BeanOptionselect * from CateringItemIncludedselect * from CateringOrderselect * from CateringOrderEntreeItemselect * from CateringOrderEntreesselect * from Cityselect * from Countryselect * from DeliveryOptionselect * from EntreeOptionselect * from NumberOfPeopleOptionselect * from [State]select * from [User] |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 08:30:45
|
city is the first one you try so I guess the indentity insert on DeliveryOption is on from a previous failed run of the script or a test that you did before running it.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-14 : 08:44:31
|
Cant spot anything obvious. But what you could do is to try prefixing the db.schema names to tables used in SET IDENTITY_INSERT and also try a batch seperator GO after each table population between identity insert off and next tables identity insert on. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 16:47:08
|
>>>city is the first one you try so I guess the indentity insert on DeliveryOption is on from a previous failed run of the script or a test that you did before running it.right I was thinking the same thing. But. I even disabled that delete script before it. I even turned it off explicitly but I still get that error, makes no difference what I do. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 16:47:49
|
quote: Originally posted by visakh16 Cant spot anything obvious. But what you could do is to try prefixing the db.schema names to tables used in SET IDENTITY_INSERT and also try a batch seperator GO after each table population between identity insert off and next tables identity insert on.
I'll try that next. Thanks. |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 17:11:16
|
The delete won't affect it.It's 'Elbalazo.dbo.DeliveryOption' that's the problem not city.did you turn DeliveryOption off?==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:19:37
|
Ok, unfortunately this did not work:--********* INSERT SCRIPTS TO TRANSFER PRODUCTION DATA TO NEW ELBALAZO DATABASE *********----TODO go through delete all records and reinsert again but this time with identity-- so that all keys match up just like they were in production-- Make sure to keep current bean options but also include old and disable them for record history-- Clean DB of all records-- Stolen from http://dotnetslackers.com/community/blogs/mosessaur/archive/2007/12/09/sql-server-2005-clean-your-database-records-amp-reset-identity-columns-all-in-6-lines.aspx/*Disable Constraints & Triggers*/exec sp_MSforeachtable 'ALTER TABLE ? -- NOCHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'/*Perform delete operation on all table for cleanup*/exec sp_MSforeachtable 'DELETE ?'/*Enable Constraints & Triggers again*/exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'/*Reset Identity on tables with identity column*/exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'-- CityALTER TABLE City -- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.City ONINSERT INTO Elbalazo.dbo.City ( [CityID] ,[CityName] ,[CountyID] ,[Active])SELECT [CityID],[CityName],[CountyID],1FROM [ElbalazoProduction].dbo.tbl_CitySET IDENTITY_INSERT City OFFGO-- StateALTER TABLE State-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.[State] ONINSERT INTO Elbalazo.dbo.State ( [StateID] ,[State] ,[Active])SELECT [StateID],[State],1FROM [ElbalazoProduction].dbo.tbl_StateSET IDENTITY_INSERT [State] OFFGO-- NumberOfPeopleOptionALTER TABLE NumberOfPeopleOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.NumberOfPeopleOption ONINSERT INTO [Elbalazo].[dbo].[NumberOfPeopleOption] ([NumberOfPeopleOptionID] ,[NumberOfPeopleNameOption] ,[Active])SELECT [NumberOfPeopleID], [NumberOfPeopleName],1FROM [ElbalazoProduction].dbo.tbl_NumberOfPeopleSET IDENTITY_INSERT [NumberOfPeople] OFFGO-- DeliveryOptionALTER TABLE DeliveryOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.DeliveryOption ONINSERT INTO [Elbalazo].[dbo].[DeliveryOption] ([DeliveryOptionID] ,[DeliveryOptionName] ,[Active])SELECT [DeliveryOptionName],[DeliveryOptionName], 1FROM [ElbalazoProduction].dbo.tbl_DeliveryOptionSET IDENTITY_INSERT DeliveryOption OFFGO-- UserALTER TABLE [User]-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.[User] ONINSERT INTO [Elbalazo].[dbo].[User] ([UserID] ,[FirstName] ,[LastName] ,[Address1] ,[Address2] ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[Email] ,[CreateDate] ,[Active])SELECT [CustomerID] ,[FirstName] ,[LastName] ,[AddressLine1] ,NULL ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[EmailPrefix] + '@' + [EmailSuffix] ,[CreateDate] ,1FROM [ElbalazoProduction].dbo.tbl_CustomerSET IDENTITY_INSERT [User] OFFGO-- EntreeOptionALTER TABLE EntreeOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.EntreeOption ONINSERT INTO [Elbalazo].[dbo].[EntreeOption] ([EntreeOptionID] ,[EntreeOptionName] ,[Active])SELECT [EntreeOptionID] ,[EntreeOptionName] ,1FROM [ElbalazoProduction].dbo.tbl_EntreeOptionSET IDENTITY_INSERT EntreeOption OFFGO-- CateringOrderALTER TABLE CateringOrder-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.CateringOrder ONINSERT INTO [Elbalazo].[dbo].[CateringOrder] ([CateringOrderID] ,[UserID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID])SELECT [CateringOrderID] ,[CustomerID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID]FROM [ElbalazoProduction].dbo.tbl_CateringOrderSET IDENTITY_INSERT CateringOrder OFFGO-- CateringOrder_EntreeItemALTER TABLE CateringOrderEntreeItem-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.CateringOrderEntreeItem ONINSERT INTO [Elbalazo].[dbo].[CateringOrderEntreeItem] ([CateringOrderEntreeItemID] ,[CateringOrderID] ,[EntreeItemID])SELECT [CateringORder_EntreeItemID] ,[CateringOrderID] ,[EntreeItemID]FROM [ElbalazoProduction].dbo.tbl_CateringOrder_EntreeItemSET IDENTITY_INSERT CateringOrderEntreeItem OFFGOselect * from BeanOptionselect * from CateringItemIncludedselect * from CateringOrderselect * from CateringOrderEntreeItemselect * from CateringOrderEntreesselect * from Cityselect * from Countryselect * from DeliveryOptionselect * from EntreeOptionselect * from NumberOfPeopleOptionselect * from [State]select * from [User]Msg 156, Level 15, State 1, Line 28Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'SET'.(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(316 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected)(0 row(s) affected) |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:20:56
|
This is wierd. If I take out the enabling of the identity insert and stuff and just do a straight up insert all is fine for this script. But that's not all that I want. I want to ensure I'm inserting the same keys when I port this stuff over in my script so I need to definitely have this identity insert working properly. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:21:59
|
I don't mean to double post but, I added the dbo to the end sets also--********* INSERT SCRIPTS TO TRANSFER PRODUCTION DATA TO NEW ELBALAZO DATABASE *********----TODO go through delete all records and reinsert again but this time with identity-- so that all keys match up just like they were in production-- Make sure to keep current bean options but also include old and disable them for record history-- Clean DB of all records-- Stolen from http://dotnetslackers.com/community/blogs/mosessaur/archive/2007/12/09/sql-server-2005-clean-your-database-records-amp-reset-identity-columns-all-in-6-lines.aspx/*Disable Constraints & Triggers*/exec sp_MSforeachtable 'ALTER TABLE ? -- NOCHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'/*Perform delete operation on all table for cleanup*/exec sp_MSforeachtable 'DELETE ?'/*Enable Constraints & Triggers again*/exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'/*Reset Identity on tables with identity column*/exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'-- CityALTER TABLE City -- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.City ONINSERT INTO Elbalazo.dbo.City ( [CityID] ,[CityName] ,[CountyID] ,[Active])SELECT [CityID],[CityName],[CountyID],1FROM [ElbalazoProduction].dbo.tbl_CitySET IDENTITY_INSERT Elbalazo.dbo.City OFFGO-- StateALTER TABLE State-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.[State] ONINSERT INTO Elbalazo.dbo.State ( [StateID] ,[State] ,[Active])SELECT [StateID],[State],1FROM [ElbalazoProduction].dbo.tbl_StateSET IDENTITY_INSERT Elbalazo.dbo.[State] OFFGO-- NumberOfPeopleOptionALTER TABLE NumberOfPeopleOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.NumberOfPeopleOption ONINSERT INTO [Elbalazo].[dbo].[NumberOfPeopleOption] ([NumberOfPeopleOptionID] ,[NumberOfPeopleNameOption] ,[Active])SELECT [NumberOfPeopleID], [NumberOfPeopleName],1FROM [ElbalazoProduction].dbo.tbl_NumberOfPeopleSET IDENTITY_INSERT Elbalazo.dbo.[NumberOfPeople] OFFGO-- DeliveryOptionALTER TABLE DeliveryOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.DeliveryOption ONINSERT INTO [Elbalazo].[dbo].[DeliveryOption] ([DeliveryOptionID] ,[DeliveryOptionName] ,[Active])SELECT [DeliveryOptionName],[DeliveryOptionName], 1FROM [ElbalazoProduction].dbo.tbl_DeliveryOptionSET IDENTITY_INSERT Elbalazo.dbo.DeliveryOption OFFGO-- UserALTER TABLE [User]-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.[User] ONINSERT INTO [Elbalazo].[dbo].[User] ([UserID] ,[FirstName] ,[LastName] ,[Address1] ,[Address2] ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[Email] ,[CreateDate] ,[Active])SELECT [CustomerID] ,[FirstName] ,[LastName] ,[AddressLine1] ,NULL ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[EmailPrefix] + '@' + [EmailSuffix] ,[CreateDate] ,1FROM [ElbalazoProduction].dbo.tbl_CustomerSET IDENTITY_INSERT Elbalazo.dbo.[User] OFFGO-- EntreeOptionALTER TABLE EntreeOption-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.EntreeOption ONINSERT INTO [Elbalazo].[dbo].[EntreeOption] ([EntreeOptionID] ,[EntreeOptionName] ,[Active])SELECT [EntreeOptionID] ,[EntreeOptionName] ,1FROM [ElbalazoProduction].dbo.tbl_EntreeOptionSET IDENTITY_INSERT Elbalazo.dbo.EntreeOption OFFGO-- CateringOrderALTER TABLE CateringOrder-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.CateringOrder ONINSERT INTO [Elbalazo].[dbo].[CateringOrder] ([CateringOrderID] ,[UserID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID])SELECT [CateringOrderID] ,[CustomerID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID]FROM [ElbalazoProduction].dbo.tbl_CateringOrderSET IDENTITY_INSERT Elbalazo.dbo.CateringOrder OFFGO-- CateringOrder_EntreeItemALTER TABLE CateringOrderEntreeItem-- NOCHECK CONSTRAINT ALLSET IDENTITY_INSERT Elbalazo.dbo.CateringOrderEntreeItem ONINSERT INTO [Elbalazo].[dbo].[CateringOrderEntreeItem] ([CateringOrderEntreeItemID] ,[CateringOrderID] ,[EntreeItemID])SELECT [CateringORder_EntreeItemID] ,[CateringOrderID] ,[EntreeItemID]FROM [ElbalazoProduction].dbo.tbl_CateringOrder_EntreeItemSET IDENTITY_INSERT Elbalazo.dbo.CateringOrderEntreeItem OFFGOselect * from BeanOptionselect * from CateringItemIncludedselect * from CateringOrderselect * from CateringOrderEntreeItemselect * from CateringOrderEntreesselect * from Cityselect * from Countryselect * from DeliveryOptionselect * from EntreeOptionselect * from NumberOfPeopleOptionselect * from [State]select * from [User] |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:32:05
|
Even if I explicitely run thisSET IDENTITY_INSERT Elbalazo.dbo.City OFFThen just run a test using just one of those inserts here, I still get the same error saying Identity Insert is already ON even though I just disabled it:SET IDENTITY_INSERT Elbalazo.dbo.City ONINSERT INTO Elbalazo.dbo.City ( [CityID] ,[CityName] ,[CountyID] ,[Active])SELECT [CityID],[CityName],[CountyID],1FROM [ElbalazoProduction].dbo.tbl_CitySET IDENTITY_INSERT Elbalazo.dbo.City OFFMsg 8107, Level 16, State 1, Line 1IDENTITY_INSERT is already ON for table 'Elbalazo.dbo.NumberOfPeopleOption'. Cannot perform SET operation for table 'Elbalazo.dbo.City'. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:32:48
|
AHHHHHHHHHHHHHHHHHHHH! I HATE SQL. (Sorry, I'm a C# developer who knows a good amout but...) frustrated! |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:33:31
|
I guess I should say I hate DEBUGGING a language that is not easily debugged like it is in OOP. :( no harm meant. But it's true. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:39:09
|
What's also strange is, if I got into Management Studio, right-click the City table for example, then modify..I notice that the Identity Specification is set to NO. Well fine but I also notice that I'm not able to enable it manually as the (Is Identity) dropdown right below it is greyed out. I check, and my database is not read-only so what gives? I have the proper permissions with db_owner access. So something is up. Not sure what's limiting/blocking me but it sure is driving me crazy with this entire situation and this script. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:40:50
|
Alright so I tested again as I'm just testint on one table just to see what happens (SET IDENTITY_INSERT Elbalazo.dbo.City OFF). Ran this and sure enough it does set the Identity and Is Identity propertys of my table back to YES. But this still doesn't fix my script problem per my last script post. |
|
|
dba123
Yak Posting Veteran
90 Posts |
Posted - 2008-04-14 : 17:43:54
|
Ok, resolved (hell if I know). I just went through an manually ran each SET IDENTITY_INSERT OFF for each and every table. Then ran it again and again and now it's fine.--********* INSERT SCRIPTS TO TRANSFER PRODUCTION DATA TO NEW ELBALAZO DATABASE *********----TODO go through delete all records and reinsert again but this time with identity-- so that all keys match up just like they were in production-- Make sure to keep current bean options but also include old and disable them for record history-- Clean DB of all records-- Stolen from http://dotnetslackers.com/community/blogs/mosessaur/archive/2007/12/09/sql-server-2005-clean-your-database-records-amp-reset-identity-columns-all-in-6-lines.aspx/*Disable Constraints & Triggers*/--exec sp_MSforeachtable 'ALTER TABLE ? -- NOCHECK CONSTRAINT ALL'--exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'/*Perform delete operation on all table for cleanup*/--exec sp_MSforeachtable 'DELETE ?'/*Enable Constraints & Triggers again*/--exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'--exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'/*Reset Identity on tables with identity column*/--exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'-- City--ALTER TABLE Elbalazo.dbo.City SET IDENTITY_INSERT Elbalazo.dbo.City ONINSERT INTO Elbalazo.dbo.City ( [CityID] ,[CityName] ,[CountyID] ,[Active])SELECT [CityID],[CityName],[CountyID],1FROM [ElbalazoProduction].dbo.tbl_CitySET IDENTITY_INSERT Elbalazo.dbo.City OFF-- State--ALTER TABLE Elbalazo.dbo.StateSET IDENTITY_INSERT Elbalazo.dbo.[State] ONINSERT INTO Elbalazo.dbo.State ( [StateID] ,[State] ,[Active])SELECT [StateID],[State],1FROM [ElbalazoProduction].dbo.tbl_StateSET IDENTITY_INSERT Elbalazo.dbo.[State] OFF-- NumberOfPeopleOption--ALTER TABLE Elbalazo.dbo.NumberOfPeopleOptionSET IDENTITY_INSERT Elbalazo.dbo.NumberOfPeopleOption ONINSERT INTO [Elbalazo].[dbo].[NumberOfPeopleOption] ([NumberOfPeopleOptionID] ,[NumberOfPeopleNameOption] ,[Active])SELECT [NumberOfPeopleID], [NumberOfPeopleName],1FROM [ElbalazoProduction].dbo.tbl_NumberOfPeopleSET IDENTITY_INSERT Elbalazo.dbo.[NumberOfPeopleOption] OFF-- DeliveryOption--ALTER TABLE Elbalazo.dbo.DeliveryOptionSET IDENTITY_INSERT Elbalazo.dbo.DeliveryOption ONINSERT INTO [Elbalazo].[dbo].[DeliveryOption] ([DeliveryOptionID] ,[DeliveryOptionName] ,[Active])SELECT [DeliveryOptionID],[DeliveryOptionName], 1FROM [ElbalazoProduction].dbo.tbl_DeliveryOptionSET IDENTITY_INSERT Elbalazo.dbo.DeliveryOption OFF-- User--ALTER TABLE Elbalazo.dbo.[User]SET IDENTITY_INSERT Elbalazo.dbo.[User] ONINSERT INTO [Elbalazo].[dbo].[User] ([UserID] ,[FirstName] ,[LastName] ,[Address1] ,[Address2] ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[Email] ,[CreateDate] ,[Active])SELECT [CustomerID] ,[FirstName] ,[LastName] ,[AddressLine1] ,NULL ,[CityID] ,[StateID] ,[Zip] ,[PhoneAreaCode] ,[PhonePrefix] ,[PhoneSuffix] ,[EmailPrefix] + '@' + [EmailSuffix] ,[CreateDate] ,1FROM [ElbalazoProduction].dbo.tbl_CustomerSET IDENTITY_INSERT Elbalazo.dbo.[User] OFF-- EntreeOption--ALTER TABLE Elbalazo.dbo.EntreeOptionSET IDENTITY_INSERT Elbalazo.dbo.EntreeOption ONINSERT INTO [Elbalazo].[dbo].[EntreeOption] ([EntreeOptionID] ,[EntreeOptionName] ,[Active])SELECT [EntreeOptionID] ,[EntreeOptionName] ,1FROM [ElbalazoProduction].dbo.tbl_EntreeOptionSET IDENTITY_INSERT Elbalazo.dbo.EntreeOption OFF-- CateringOrder--ALTER TABLE Elbalazo.dbo.CateringOrderSET IDENTITY_INSERT Elbalazo.dbo.CateringOrder ONINSERT INTO [Elbalazo].[dbo].[CateringOrder] ([CateringOrderID] ,[UserID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID])SELECT [CateringOrderID] ,[CustomerID] ,[NumberOfPeopleID] ,[BeanOptionID] ,[TortillaOptionID] ,[CreateDate] ,[Notes] ,[EventDate] ,[DeliveryOptionID]FROM [ElbalazoProduction].dbo.tbl_CateringOrderSET IDENTITY_INSERT Elbalazo.dbo.CateringOrder OFF-- CateringOrder_EntreeItem--ALTER TABLE Elbalazo.dbo.CateringOrderEntreeItemSET IDENTITY_INSERT Elbalazo.dbo.CateringOrderEntreeItem ONINSERT INTO [Elbalazo].[dbo].[CateringOrderEntreeItem] ([CateringOrderEntreeItemID] ,[CateringOrderID] ,[EntreeItemID])SELECT [CateringORder_EntreeItemID] ,[CateringOrderID] ,[EntreeItemID]FROM [ElbalazoProduction].dbo.tbl_CateringOrder_EntreeItemSET IDENTITY_INSERT Elbalazo.dbo.CateringOrderEntreeItem OFFselect * from BeanOptionselect * from CateringItemIncludedselect * from CateringOrderselect * from CateringOrderEntreeItemselect * from CateringOrderEntreesselect * from Cityselect * from Countryselect * from DeliveryOptionselect * from EntreeOptionselect * from NumberOfPeopleOptionselect * from [State]select * from [User] |
|
|
yasirbam
Starting Member
1 Post |
Posted - 2008-11-14 : 12:30:19
|
This is what BOL says about this issue:"At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, Microsoft® SQL Server™ returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for."Using GO between the statements to break down the batch as suggested by vishakh16 should work.Yasir |
|
|
|
|
|
|
|