tags:

views:

29

answers:

3

I have a couple of big datasets ( ~O(1TB) ), which I want to to import into my database. I use BULK INSERT to import data into temporary tables, then use SELECT and INSERT INTO to fill my real tables with the data. This is because I want to change the order of some things, and split some data files into logical tables. And if this functionality is not needed, I just BULK INSERT directly into my target table.

I would like to check if all foreign key constraints have been enforced. If I flag BULK INSERT to CHECK_CONSTRAINTS during the import stage the import process slows down to a crawl.

Is there a command to do this after the fact? I have very limited familiarity with SQL Server, and databasing in general.

Thanks in advance.

EDIT:

Suggested reading : MSDN Article

+1  A: 

Tibor Karaszi wrote a great article about trusted constraints: Non-trusted constraints

AlexKuznetsov
Found a "DBCC CHECKCONSTRAINTS()" hint in that post, going to try it out. :)
Gleno
+2  A: 

How many tables/foreign keys are you looking at? Have you considering writing a quick custom query that checks for orphaned rows?

LesterDove
That's exactly what I'm doing right now. There are roughly 10 tables some of which cross-reference each other. I thought that there could be a way to save some time. :)
Gleno
I like this answer +1, you need to know where the FK's not valid
MikeAinOz
+1  A: 

To check one table:

alter table YourTable with check check constraint all

To check all tables:

exec sp_msforeachtable 'alter table ? with check check constraint all'
Joe Stefanelli
Hm, I tried 'alter table MyTable with check check constraint all' and got a... "Command(s) completed successfully." would it have failed otherwise?
Gleno
Yes. You'd get an error like "ALTER TABLE statement conflicted with COLUMN FOREIGN KEY constraint ..." if there was a violation.
Joe Stefanelli
Okay, then thank you very much for your help.
Gleno