Avatar of Tom Knowlton
Tom Knowlton
Flag for United States of America asked on

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.
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Tom Knowlton

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dsacker

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tom Knowlton

ASKER
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  ))
dsacker

You would DROP CONSTRAINT FK_YourForeignKey. However, you won't be able to recreate it until you have those values in your parent table.
SOLUTION
Anthony Perkins

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tom Knowlton

ASKER
We ended-up working around the issue without having to drop and then reinstate constraints / triggers.

But the solution does what I asked.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes