So I want to copy just the data from a Prod database (SQL 2005) down to my local machine (SQL 2005开发者_高级运维 & SQL 2008 Management Studio installed).
The problem is I'm running into foreign key constraints that are causing the task/scripts to fail. I can get by these errors if I import certain tables first, but is there an easier way to do this all at once? I'm not a DBA so I don't have access to the database back up.
I've tried the SQL Import/Export data Wizard and Publishing Wizard, but it also gets the PK error.
I would drop all Foreign Key contraints, the re-apply after you have copied all the data across.
e.g.
-- Drop Constraint
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo]. [FK_ForeignKeyName]') AND parent_object_id = OBJECT_ID(N'[dbo].[Table]'))
ALTER TABLE [dbo].[Table] DROP CONSTRAINT [FK_ForeignKeyName]
-- Add it back again
ALTER TABLE [dbo].[Table] WITH CHECK ADD CONSTRAINT [FK_ForeignKeyName] FOREIGN KEY([FeildName])
REFERENCES [dbo].[OtherTable] ([OtherField])
Details on how to globally disable/enable constraints in order to do bulk data importing:
http://decipherinfosys.wordpress.com/2008/02/20/disableenable-foreign-key-and-check-constraints-in-sql-server/
精彩评论