Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Use the sys.database_mirroring DMV to quickly check if the databases are in principal or mirror role and what state they are inWhat is the Dual table in Oracle and why do I need it? »
    comments

    I wrote a blogpost about the fact that there is a common myth that you can't rollback a truncate statement in SQL this post was written on June 13, 2007 and it showed you that you could rollback a truncate. Here is some code that shows that.

    First create this very simple table

    1. CREATE TABLE dbo.TruncateTest (ID int IDENTITY PRIMARY KEY,
    2.                 SomeOtherCol varchar(49))
    3. GO

    Add the following two rows

    1. INSERT dbo.TruncateTest VALUES(1)
    2. INSERT dbo.TruncateTest VALUES(1)



    Now execute this whole block in one shot, you will see three resultsets, two of them will have two rows and one resultset will be empty

    1. SELECT * FROM dbo.TruncateTest -- 2 rows
    2.  
    3. BEGIN TRAN
    4.     TRUNCATE TABLE dbo.TruncateTest
    5.     SELECT * FROM dbo.TruncateTest -- 0 rows
    6. ROLLBACK TRAN
    7.  
    8. SELECT * FROM dbo.TruncateTest  -- 2 rows again after rollback


    Here is the output

    ID          SomeOtherCol
    ----------- -------------------------------------------------
    1           1
    2           1
    
    (2 row(s) affected)
    
    ID          SomeOtherCol
    ----------- -------------------------------------------------
    
    (0 row(s) affected)
    
    ID          SomeOtherCol
    ----------- -------------------------------------------------
    1           1
    2           1
    
    (2 row(s) affected)
    

    As you can see the table was empty at one point, however the table has the same two rows again, if you execute this query, you will see those two row again

    1. SELECT * FROM dbo.TruncateTest



    What about Oracle, can you rollback a truncate statement?

    In SQL Server the minimum permission required is ALTER on table_name. TRUNCATE TABLE permissions default to the table owner, members of the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, I blogged about this as well in the post Why do you need additional privileges for truncate table compared to delete?

    In Oracle to truncate a table, the table must be in your schema or you must have DROP ANY TABLE system privilege.

    In Oracle a truncate statement is actually a DDL statement, you CANNOT rollback a truncate after it has happened. A truncate statement removes all rows and returns the freed space to the tablespace containing the table.

    Please keep these differences in mind when working with different platforms, don't assume anything, it will bite you in the butt, always reference the documentation. For some more Oracle and SQL Server differences, see also my post Differences between Oracle and SQL Server when working with NULL and blank values

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1912 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)