Msg 4712: cannot truncate table
Hello,
I am running SQL Server 2005 SP1. Whenever I issue a 'truncate' query to the DB engine it fails if the table to be modified is currently referenced by a foreign key constraint.
A code snippet (T-SQL) that demonstrates the problem is beneath.
Does anybody know a fix-it?
Thanks in advance,
Intenion
USE tempdb
Go
-- Clearing the 'tmpdb' database
IFOBJECT_ID('user')ISNOTNULL
DROPTABLE [user];IFOBJECT_ID('address')ISNOTNULL
DROPTABLE address;-- Creating tables: 'address' and 'user'
CREATETABLE address
(
id
INTIDENTITY(0, 1)CONSTRAINT pk_addressPRIMARYKEY,zip
SMALLINTNOTNULLCONSTRAINT chk_zipCHECK(zip> 0),street
NVARCHAR(50)NOTNULLCONSTRAINT chk_streetCHECK(LEN(street)> 0),number
SMALLINTNOTNULLCONSTRAINT chk_numberCHECK(number> 0) CONSTRAINT unq_addressUNIQUE(zip, street, number));
Go
CREATETABLE [user]
(
address
INTNULLCONSTRAINT fk_user_addressFOREIGNKEYREFERENCES address(id)ONDELETESETNULL);
Go
-- Populating tables
INSERTINTO addressVALUES(2365,'street 1', 2);
INSERTINTO addressVALUES(23369,'street 2', 2);
Go
INSERTINTO [user]VALUES('someone@isp.net', 0);
INSERTINTO [user]VALUES('somebody@isp-r-us.com', 1);
Go
-- Truncate query that fails
TRUNCATETABLE address;
-- Delete query that passes
--DELETE address;

