Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

How Truncate statement ? Expand / Collapse
Author
Message
Posted Thursday, October 21, 2010 1:34 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 6:02 AM
Points: 1,012, Visits: 1,008
Good question, well done!

as Kingston wrote earlier, per BOL looks like there are a few more restrictions on truncate,

You cannot use TRUNCATE TABLE on tables that:
* Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
* Participate in an indexed view.
* Are published by using transactional replication or merge replication.

For tables with one or more of these characteristics, use the DELETE statement instead.

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. For more information, see CREATE TRIGGER (Transact-SQL).


With all these restrictions what kind of application would use truncate table?


Thanks,
Iulian
Post #1008211
Posted Thursday, October 21, 2010 1:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:36 PM
Points: 6,133, Visits: 8,398
Nice question, and a (to me) surprising large number opf incorrect answers. Shame about the typo in the table name, though - it was obvious that this was not intended, but it will confuse some people. And seriously, how much time would it have cost you to copy and paste the code in a query window and hit execute before submitting the question?

Hardy21 (10/20/2010)
Nice question.
Delete is working for CustomerMaster table but truncate throws an error. I would like to know why it is not working? Any specific reason?

DELETE works on a row-by-row basis. Even if no WHERE clause is used. For each row deleted, the refential integrity can be checked.
TRUNCATE TABLE works by deallocating entire pages in the database file, without looking at the contents. There is no way to check if rows deleted were referenced by a FOREIGN KEY. Therefor, TRUNCATE TABLE is simply forbidden if there is a FOREIGN KEY that references the target table.

Iulian -207023 (10/21/2010)
With all these restrictions what kind of application would use truncate table?

An application that does not use triggers, is not involved in transactional replication or merge replication, and that is able to temporarily remove referencing foreign key constraints and indexed views based on the table in order to profit from a tremendous performance gain when a very large table has to be emptied.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1008218
Posted Thursday, October 21, 2010 1:57 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 5:14 AM
Points: 1,115, Visits: 456
Got it right as I knew that you could not truncate a table with FK Reference. However, the explanation for why it was the error is that the table does not exist. Not sure if this was a oversight on the person who entered the question or the person who published it. Good question though I think it will teach people some of the restrictions of truncate table.
Post #1008219
Posted Thursday, October 21, 2010 2:02 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, February 10, 2014 7:34 AM
Points: 17, Visits: 22
Hardy21 (10/21/2010)
w.rooks (10/21/2010)
The error you will get is: CostomerMast does not Exist!!!

The table is called CostomerMastER


This was typo mistake.
If you change it to CustomerMaster still SQL throws an error:
Cannot truncate table 'CustomerMaster' because it is being referenced by a FOREIGN KEY constraint.


You can't change the question! There is no way to know if a typo was ment to be made. My answer is the right one.

Had the question been different your's would be right.
Post #1008223
Posted Thursday, October 21, 2010 3:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
Got it right by process of elimination.

First, I thought the answer might be 1. A new Identity would not have been generated after the truncate so it cannot be 1. Maybe it will be 21. No, the last identity generated for that table should be 20 -- not 21. This caused me to look closer at the query to notice the constraint.
Post #1008250
Posted Thursday, October 21, 2010 3:07 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:28 AM
Points: 5,488, Visits: 10,339
Hugo Kornelis (10/21/2010)
Iulian -207023 (10/21/2010)
With all these restrictions what kind of application would use truncate table?

An application that does not use triggers, is not involved in transactional replication or merge replication, and that is able to temporarily remove referencing foreign key constraints and indexed views based on the table in order to profit from a tremendous performance gain when a very large table has to be emptied.


It should be noted that TRUNCATE is a DDL operation, not DML, and as such requires membership of db_ddladmin at the very least. You would want to weight up the benefits of any performance gains against the security costs of granting such access to users or application service accounts.

John
Post #1008252
Posted Thursday, October 21, 2010 4:38 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, September 18, 2014 7:22 AM
Points: 52, Visits: 84
Hi,

Truncate statement is faster than than delete statement and it can be extensively use while working with temporary tables. Also it is useful to reset the identity column value that delete statement does not.

Thanks & Regards
Manoj Kumar.
Post #1008296
Posted Thursday, October 21, 2010 5:04 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, September 18, 2014 7:22 AM
Points: 52, Visits: 84
Hi ,

I was searching in the sql for 'costomermaster' in the entire sql but I didn't find anything. It would be grate If u can tell me the statement misspelled.

Thanks & Regards
Manoj Kumar
Post #1008320
Posted Thursday, October 21, 2010 5:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 5:14 AM
Points: 1,115, Visits: 456
The misspelling is

TRUNCATE TABLE CustomerMast

The table is called CustomerMaster there is no reference to the table you are looking for in the SQL. This was yet again a misspelling on the thread by one of the contributors.
Post #1008361
Posted Thursday, October 21, 2010 6:53 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, December 12, 2014 8:24 AM
Points: 2,818, Visits: 2,567
I have to make a point of waking up a little more before answering these questions.
Post #1008412
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse