Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Administration - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 6525

Trouble Truncating Table Referenced by Forien Key

$
0
0
ALTER TABLE ClientCoverage NOCHECK CONSTRAINT ALLALTER TABLE ClientCoverageException NOCHECK CONSTRAINT ALLALTER TABLE ContractorCoverage NOCHECK CONSTRAINT ALLALTER TABLE ContractorCoverageException NOCHECK CONSTRAINT ALLALTER TABLE Postal NOCHECK CONSTRAINT ALLI'm getting errors when attempting to Truncate various tables.FOr example to get the following error on one of the tables:[Execute SQL Task] Error: Executing the query "-- This code is used to verify the connection bef..." failed with the following error: "Cannot truncate table 'Postal_StateProvince' because it is being referenced by a FOREIGN KEY constraint.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.I'm disabling the Foreign Keys as follows: [code="sql"]ALTER TABLE Postal_CityTownship NOCHECK CONSTRAINT ALLALTER TABLE Postal_CountyRegion NOCHECK CONSTRAINT ALLALTER TABLE Postal_StateProvince NOCHECK CONSTRAINT ALLALTER TABLE Postal_Country NOCHECK CONSTRAINT ALLALTER TABLE ClientCoverageType NOCHECK CONSTRAINT ALLALTER TABLE Postal_CountyRegion NOCHECK CONSTRAINT ALLALTER TABLE ProgramAreas NOCHECK CONSTRAINT ALLALTER TABLE TRADES NOCHECK CONSTRAINT ALL[/code]I'm using the following script to identify FK Constraints:[code="sql"]SELECT tab1.name AS [table], col1.name AS [column], tab2.name AS [referenced_table], col2.name AS [referenced_column], obj.name AS FK_NAME --sch.name AS [schema_name]FROM sys.foreign_key_columns fkcINNER JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_idINNER JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_idINNER JOIN sys.schemas sch ON tab1.schema_id = sch.schema_idINNER JOIN sys.columns col1 ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_idINNER JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_idINNER JOIN sys.columns col2 ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_idORDER BY tab1.name [/code]I'm using a SSIS Pages to do this.Do you disable the constraint at the child level or the parent level?I'm not providing much information but can someone give me insight as to what I need to do to resolver this issue?

Viewing all articles
Browse latest Browse all 6525

Trending Articles