Re: Turning Off Foreign Keys during DB coping
- From: "Wm. Scott Miller" <Scott.Miller@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 16 Nov 2005 15:46:08 -0500
You can create a task that will create a script (or even a Stored Proc) that
will disable and then reenable the constraints. You can generate both
scripts by the below resultset:
SELECT 'ALTER TABLE ' + TABLE_NAME + ' NOCHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES
SELECT 'ALTER TABLE ' + TABLE_NAME + ' CHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES
Scott
"Shimon Sim" <shimonsim048@xxxxxxxxxxxxxxxx> wrote in message
news:e$ma4qu6FHA.3752@xxxxxxxxxxxxxxxxxxxxxxx
> Michael
> Do you suggest me to write the script for each table to disable keys?
> It is not really a solution since I have over 100 tables. Plus the schema
> changes and my script can become invalid.
> Is there something that can disable all the keys whatever they are?
> An if such thing exists how can I out it in the DTS package together with
> backup and data transfer tasks?
>
> Thank you,
> Shimon
>
> "Michael Cheng [MSFT]" <v-mingqc@xxxxxxxxxxxxxxxxxxxx> wrote in message
> news:%2338TAel6FHA.3036@xxxxxxxxxxxxxxxxxxxxxxxx
>> Hi Shimon,
>>
>> Welcome to use MSDN Managed Newsgroup!
>>
>> You could use NOCHECK instead of UNCHECKED when altering a existing
>> constraint to be invalid. I made a sample on my side with DTS and it
>> works.
>> Here is a sample
>>
>> CREATE TABLE Test2
>> (
>> TID INT Primary KEY,
>> CID INT IDENTITY(1,1) UNIQUE
>> )
>>
>> INSERT Test2 VALUES (11)
>> INSERT Test2 VALUES (12)
>> INSERT Test2 VALUES (13)
>> INSERT Test2 VALUES (14)
>>
>> --SELECT * FROM Test2
>>
>> CREATE TABLE Test2Child
>> (
>> TCID INT FOREIGN KEY REFERENCES Test2(CID),
>> TC CHAR(1)
>> )
>>
>> INSERT Test2Child VALUES (55,'B')
>> --The statement above will fail
>> ALTER TABLE Test2Child NOCHECK CONSTRAINT FK__Test2Child__TCID__48CFD27E
>>
>> INSERT Test2Child VALUES (55,'B')
>> --The statement above will do since we have altered NOCHECK
>> --Use a DTS package to do the test, it also works
>>
>> NOTE that if you have specified NOCHECK for the constraint, it will not
>> check the constraints any more.
>>
>>
>>
>> Sincerely yours,
>>
>> Michael Cheng
>> Microsoft Online Partner Support
>>
>> When responding to posts, please "Reply to Group" via your newsreader so
>> that others may learn and benefit from your issue.
>> =====================================================
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>
>
.
- Follow-Ups:
- Re: Turning Off Foreign Keys during DB coping
- From: Shimon Sim
- Re: Turning Off Foreign Keys during DB coping
- References:
- Turning Off Foreign Keys during DB coping
- From: Shimon Sim
- RE: Turning Off Foreign Keys during DB coping
- From: Michael Cheng [MSFT]
- Re: Turning Off Foreign Keys during DB coping
- From: Shimon Sim
- Turning Off Foreign Keys during DB coping
- Prev by Date: Re: Turning Off Foreign Keys during DB coping
- Next by Date: Re: Rows Transfered Value
- Previous by thread: Re: Turning Off Foreign Keys during DB coping
- Next by thread: Re: Turning Off Foreign Keys during DB coping
- Index(es):
Relevant Pages
|