Temporarily ignore or turn-off restraints, import data, then turn them back on?

Is there a T-SQL command to temporarily ignore or turn-off restraints, import data, then turn them back on?

-- turn off constraints for table A

--import data
-- INSERT INTO table A

-- turn constraints back on for table A

Perhaps not best practice, but I need to get some data into a few tables for a new project, for testing, and I don't have time to try an unravel FK constraints.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsackerContract ERP Admin/ConsultantCommented:
I usually also disable triggers.

ALTER TABLE YourTable DISABLE TRIGGER all
ALTER TABLE YourTable NOCHECK CONSTRAINT all

-- Do your thing

ALTER TABLE YourTable CHECK CONSTRAINT all
ALTER TABLE YourTable ENABLE TRIGGER all
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
In a Data Warehouse load it's typical to DROP the fk's, load your data, validate/throw out bad rows, then re-create the fk's.

Also, keep in mind you're playing with fire here, so paint us a picture of why you can't INSERT with the FK's in place.
0
Tom KnowltonWeb developerAuthor Commented:
print 'begin delete'
DELETE FROM [UNAdmin].[ApplicationComponents]
DELETE FROM [UNAdmin].[ApplicationComponentTexts]
DELETE FROM [UNAdmin].[ApplicationFeatures]
DELETE FROM [UNAdmin].[ApplicationFeatureTexts]
DELETE FROM [UNAdmin].[ApplicationModules]
DELETE FROM [UNAdmin].[ApplicationModuleTexts]
DELETE FROM [UNAdmin].[ApplicationResources]
DELETE FROM [UNAdmin].[ApplicationSettings]   
DELETE FROM [UNAdmin].[ApplicationLanguages]   
GO
print 'end delete'


print 'begin app resources'
INSERT [UNAdmin].[ApplicationResources] ([ID], [Description], [Value], [RecordCreated], [RecordModified]) VALUES (1, N'Union ID', N'UBC ID', CAST(N'2011-02-12 06:35:11.520' AS DateTime), CAST(N'2011-02-12 06:35:11.520' AS DateTime))
GO
print 'end app resources'

print 'begin with app settings'
INSERT [UNAdmin].[ApplicationSettings] ([ID], [Description], [Value], [RecordCreated], [RecordModified]) VALUES (1, N'Show Help', 0, CAST(N'2011-02-12 06:35:27.870' AS DateTime), CAST(N'2011-02-12 06:35:27.870' AS DateTime))
GO
print 'end app settings'

print 'begin app languages'
INSERT [UNAdmin].[ApplicationLanguages] ([ID], [Name], [IsDeleted], [RecordCreated], [RecordModified]) VALUES (1, N'English', 1, CAST(N'2015-04-09 21:28:57.773' AS DateTime), CAST(N'2015-04-09 21:28:57.773' AS DateTime))
GO
print 'end app languages'

print 'begin app comp texts'
INSERT [UNAdmin].[ApplicationComponentTexts] ([ID], [LanguageID], [Text], [IsDeleted], [RecordCreated], [RecordModified]) VALUES (1, 1, N'Dashboard', 0, CAST(N'2010-01-23 08:33:56.233' AS DateTime), CAST(N'2010-01-23 08:33:56.233' AS DateTime))
GO
print 'end app comp texts'

print 'begin app feat texts'
INSERT [UNAdmin].[ApplicationFeatureTexts] ([ID], [LanguageID], [Text], [IsDeleted], [RecordCreated], [RecordModified]) VALUES (1, 1, N'My Bulletin Board', 0, CAST(N'2010-01-23 08:33:56.250' AS DateTime), CAST(N'2010-01-23 08:33:56.250' AS DateTime))
GO
print 'end app feat texts'

print 'begin app comps'
INSERT [UNAdmin].[ApplicationComponents] ([ID], [TextID], [SortOrder], [ModuleID], [DefaultFeatureID], [SecurityResourceRightID], [IsEnabled], [IsDeleted], [RecordCreated], [RecordModified]) VALUES (1, 1, 1, NULL, 38, 1, 1, 0, CAST(N'2010-01-23 08:33:56.250' AS DateTime), CAST(N'2010-01-23 08:33:56.250' AS DateTime))
GO
print 'end app comps'

print 'begin app features'
INSERT [UNAdmin].[ApplicationFeatures] ([ID], [TextID], [ComponentID], [SortOrder], [URL], [SecurityResourceRightID], [IsEnabled], [IsDeleted], [RecordCreated], [RecordModified]) VALUES (1, 1, 1, 2, N'/Common/DashboardClerical.aspx', 9, 1, 0, CAST(N'2010-01-23 08:33:56.250' AS DateTime), CAST(N'2010-01-23 08:33:56.250' AS DateTime))
GO
print 'end app features'




print 'end all processing'

Open in new window



When the script above runs, here are the messages:

begin delete

(0 row(s) affected)

(1 row(s) affected)

(0 row(s) affected)

(1 row(s) affected)

(0 row(s) affected)

(0 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)
end delete
begin app resources

(1 row(s) affected)
end app resources
begin with app settings

(1 row(s) affected)
end app settings
begin app languages

(1 row(s) affected)
end app languages
begin app comp texts

(1 row(s) affected)
end app comp texts
begin app feat texts

(1 row(s) affected)
end app feat texts
begin app comps
Msg 547, Level 16, State 0, Line 4
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ApplicationComponents_ApplicationFeatures". The conflict occurred in database "NCCRC_Onebox_tom", table "UNAdmin.ApplicationFeatures", column 'ID'.
The statement has been terminated.
end app comps
begin app features
Msg 547, Level 16, State 0, Line 4
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ApplicationFeatures_ApplicationComponents". The conflict occurred in database "NCCRC_Onebox_tom", table "UNAdmin.ApplicationComponents", column 'ID'.
The statement has been terminated.
end app features
end all processing

Open in new window


ApplicationComponents and ApplicationFeatures seem to reference each other.  UGH.

How do I drop and then enable just ONE foreign key constaint?  (( on table Application Components  ))
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

dsackerContract ERP Admin/ConsultantCommented:
You would DROP CONSTRAINT FK_YourForeignKey. However, you won't be able to recreate it until you have those values in your parent table.
0
Anthony PerkinsCommented:
You would DROP CONSTRAINT FK_YourForeignKey.  However, you won't be able to recreate it until you have those values in your parent table.
You may want to double check that.  This is from SQL Server's BOL:
If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.
But to be clear, I am not advocating this approach.
0
Tom KnowltonWeb developerAuthor Commented:
We ended-up working around the issue without having to drop and then reinstate constraints / triggers.

But the solution does what I asked.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.