How Truncate statement ?

  • 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

  • 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.

  • 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

  • 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.

  • I have to make a point of waking up a little more before answering these questions. :angry:

  • Truncate does not fail for me...?

    CREATE TABLE [dbo].[CustomerMaster](

    [CustomerId] [int] IDENTITY(1,1) NOT NULL,

    [CustomerCode] [varchar](30),

    [CustomerName] [varchar](200),

    [CreatedDate] [datetime],

    [ContactNo] [varchar](20)

    CONSTRAINT [PK_CustomerId] PRIMARY KEY CLUSTERED

    (

    [CustomerId] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    CREATE TABLE [dbo].[CustomerDocuments](

    [DocAttachmentId] [int] IDENTITY(1,1) NOT NULL,

    [CustomerId] [int] NOT NULL,

    [DocumentType] [varchar](5),

    [DocumentName] [varchar](200) NULL,

    CONSTRAINT [PK_CustomerDocs] PRIMARY KEY CLUSTERED

    (

    [DocAttachmentId] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    insert into [CustomerMaster] (CustomerCode, customerName, CreatedDate, ContactNo)

    values ('ABC','ABC','1/1/10','0000000')

    insert into CustomerDocuments (CustomerID, DocumentType, DocumentName)

    values ('1','123','123')

    insert into CustomerDocuments (CustomerID, DocumentType, DocumentName)

    values ('1','456','456')

    select * from CustomerDocuments

    select * from CustomerMaster

    TRUNCATE TABLE CustomerDocuments

    TRUNCATE TABLE CustomerMaster

    SELECT IDENT_CURRENT( 'CustomerMaster' )

    drop table CustomerDocuments

    drop table CustomerMaster

  • wware (10/21/2010)


    Truncate does not fail for me...?

    That's because you haven't defined a foreign key constraint.

    John

  • Doh! Looks like I need to wake up a bit more, too. Careless cut and paste.

  • Iulian -207023 (10/21/2010)


    With all these restrictions what kind of application would use truncate table?[/b]

    One that supports RDMS management and Administration.

    It can take up to 3 hours to transactionaly Delete and re-insert 3 million rows during a table schema update. That time is reduced to about 30 minutes if you clear the table using Truncate table.

    Most people that have to work with deploying production DB changes understand this, or learn about it quick.

    redgate has several utilities for RDMS management that understand this also.

  • knmanojclt (10/21/2010)


    I was searching in the sql for 'costomermaster' in the entire sql but I didn't find anything.

    Thanks & Regards

    Manoj Kumar

    I really hope you are joking. If you are, then har har, you got us all by mispelling the QOTD and then making fun of our posts about your mistake.

    If you are not joking the section you 'missedspelling' is:


    TRUNCATE TABLE CustomerMast

    SELECT IDENT_CURRENT( 'CustomerMast' )


    Which completely changes the answer. There is no trucate error because the Truncate is not executed when there is no object found. The SELECT IDENT_CURRENT( 'CustomerMast' ) will always return NULL instead of 1 or 21 because there is no Identity records in the system tables for a table that does not exist.

    I hope you can find the Humorus in this myth stundering of your question.:cool:

  • nice question, thanks!

    Answering these is becoming a daily habit for me now.

  • The typo is corrected and I will award back points for those that might have mistakenly picked up on that.

  • I understand: so it makes admin's life easier, I guess you can even schedule it to empty huge tables very easy and fast then load them with new and fresh data

    and about applications this has to be handled carefuly since it needs db_ddladmin at the very least (quoteing John).

    Thanks a lot,

    Iulian

  • Thanks for the question, I think it is good to help people understand the limitations of TRUNCATE TABLE.

  • Nice question 🙂

Viewing 15 posts - 16 through 30 (of 33 total)

You must be logged in to reply to this topic. Login to reply