October 21, 2010 at 3:07 am
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
October 21, 2010 at 4:38 am
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.
October 21, 2010 at 5:04 am
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
October 21, 2010 at 5:51 am
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.
October 21, 2010 at 6:53 am
I have to make a point of waking up a little more before answering these questions. :angry:
October 21, 2010 at 7:22 am
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
October 21, 2010 at 7:25 am
wware (10/21/2010)
Truncate does not fail for me...?
That's because you haven't defined a foreign key constraint.
John
October 21, 2010 at 7:29 am
Doh! Looks like I need to wake up a bit more, too. Careless cut and paste.
October 21, 2010 at 7:35 am
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.
October 21, 2010 at 7:46 am
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:
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:
October 21, 2010 at 8:18 am
nice question, thanks!
Answering these is becoming a daily habit for me now.
October 21, 2010 at 8:26 am
The typo is corrected and I will award back points for those that might have mistakenly picked up on that.
October 21, 2010 at 2:17 pm
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
October 21, 2010 at 3:08 pm
Thanks for the question, I think it is good to help people understand the limitations of TRUNCATE TABLE.
October 22, 2010 at 6:32 am
Nice question 🙂
Viewing 15 posts - 16 through 30 (of 33 total)
You must be logged in to reply to this topic. Login to reply