A table cannot be truncate in SQL _sever.

Source: Internet
Author: User
Truncate table t_test: Message 4711, level 16, status 1, 1st rows
The table 't_ test' cannot be truncated because it has been published for replication. Error table forwarding from http://technet.microsoft.com/zh-cn/library/cc645613 (V = SQL .105). aspx

4711

16

No

The table '%. * ls' cannot be truncated because the table is published for replication or Change Data Capture is enabled.

I checked it for a long time. There is one of the following solutions, but it seems to be 08. Now I am using 05, it seems a bit boring... from: http://blog.sqlauthority.com/2009/12/25/sql-server-cdc-and-truncate-cannot-truncate-table-because-it-is-published-for-replication-or-enabled-for-change-data-capture/ «SQL authority news-training ms SQL Server 2005/2008 query optimization and performance tuningsql server-whitepaper SQL Server 2008 full-text search: internals and enhancements» SQL Server-CDC and truncate-cannot truncate table because it is published for replication or enabled for Change Data Capture

December 25,200 9 by pinaldave

Few days ago, I got the great opportunity to visit Bangalore Infosys. Please read the complete details for the event here:Sqlauthority news-notes from techdays 2009 at Infosys, Bangalore. I mentioned during the session that CDC is asynchronous and it reads the log file to populate its data. I had received a very interesting question during the session.

The question is as follows: does CDC feature capture the data during the truncate operation?Answer: It is not possible or not applicable. Truncate is operation that is not logged in the log file, and if one tries to truncate the table that is enabled for CDC, it will right away throw the following error.

MSG 4711, level 16, state 1, line 1
Cannot truncate table because it is published for replication or enabled for Change Data Capture.

Let us create the scenario that will generate the above error.

/***** Set up testtable *****/
Use Adventureworks
Go
-- Create Table
Create Table DBO. testtable ( ID Int )
Go
-- Insert one hundred records
-- Insert 1
Insert DBO. testtable ( ID )
Select top 100 row_number () Over ( Order A. Name ) Rowid
From SYS. all_objects
Cross Join SYS. all_objects B
Go
/***** Enable CDC *****/
-- The following script will enable CDC in adventureworks database.
Use Adventureworks
Go
Exec SYS. Sp_cdc_enable_db
Go
-- Following script will enable CDC on DBO. testtable table.
Use Adventureworks
Go
Exec SYS. Sp_cdc_enable_table
@ Source_schema = N 'dbo' ,
@ Source_name = N 'testtable' ,
@ Role_name = Null
Go
/* Attempt to truncate table will thrown Following Error
MSG 4711, level 16, state 1, line 1
Cannot truncate table 'testtable' because it is published for replication or enabled for Change Data Capture.
*/
Truncate table Testtable
Go
/***** Clean up *****/
-- Disabling Change Data Capture on a table
Use Adventureworks ;
Go
Execute SYS. Sp_cdc_disable_table
@ Source_schema = N 'dbo' ,
@ Source_name = N 'testtable' ,
@ Capture_instance = N 'dbo _ testtable' ;
Go
-- Disable Change Data Capture feature on Database
Use Adventureworks
Go
Exec SYS. Sp_cdc_disable_db
Go
-- Drop table
Drop table Testtable
Go

The workaround for this is to either use Delete statement instead of truncate or to disable CDC first and then enable the CDC on the table after the truncate operation is completed.

I am interested to know if you have any interesting fact of above CDC. I will publish the information on this blog with due credit.

Reference:Pinal Dave (Http://blog.SQLAuthority.com)

 

There is also an article.

Http://www.sqlservercentral.com/blogs/jeffrey_yao/2008/04/22/difference-between-truncate-and-delete/

    • Posted on 22 then l 2008
    • Comments
    • Briefcase
    • Print

This is an old topic and well known ented, just Google it and I believe you will get a lot,

However, there is one difference that seems not being mentioned and actually I find it out myself only two hours ago. So here is the point

If a table is published for replication, it cannot be truncated, while it can still be deleted.

Otherwise, you will get the following msg

MSG 4711, level 16, state 1, line 1

Cannot truncate table 'xxx' because it is published for replication.

My environment is SQL Server 2k5 SP2 (Developer Edition)

Next time, when you are asked this question in an interview, just give this answer to impress your interviewer. [:)]

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.