Results 1 to 12 of 12

Thread: [RESOLVED] delete all data from all table

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] delete all data from all table

    Hi all
    I have a database in SQL SERVER 2005
    that contaion 35 table
    I want to clear all the table data using one query plese guide me!

    I do not want to delete using while loop!

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: delete all data from all table

    I don't use SQL Server, but don't know of anyway to delete all from 35 tables in one query. Maybe there's a way to copy just the structure of the db to another one. What's the problem with using a loop?
    Tengo mas preguntas que contestas

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: delete all data from all table

    Delete the database and then create it again. No loop required.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: delete all data from all table

    Or, rather than deleting the database, drop all the tables and recreate them.

    My I ask what the issue with using a loop might be?

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: delete all data from all table

    Create a Script that will drop and then recreate the tables. Don't forget about indexes and primary keys.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: delete all data from all table

    How about just...

    Code:
    Truncate Table SomeTableA; Truncate Table SomeTableB; Truncate Table Sometable C;...
    and so on

    If that's a problem for you then create a stored procedure called TruncateAllMyTables and put the 35 truncate statements in that SPROC - and just execute it.

    If you have lots of referential integrity between tables you might need to use DELETE FROM SOMETABLEA instead - but of course that is lots and lots slower and use the log file (which will expand it greatly).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: delete all data from all table

    Wouldn't Truncate have issues if there are FK relationships to of the table? I know that Oracle will not allow a tracate if the table is a Parent in a FK relationship.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: delete all data from all table

    That is what I meant by...

    Quote Originally Posted by szlamany
    If you have lots of referential integrity between tables you might need to use DELETE FROM SOMETABLEA instead - but of course that is lots and lots slower and use the log file (which will expand it greatly).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: delete all data from all table

    Quote Originally Posted by szlamany
    How about just...

    Code:
    Truncate Table SomeTableA; Truncate Table SomeTableB; Truncate Table Sometable C;...
    and so on

    If that's a problem for you then create a stored procedure called TruncateAllMyTables and put the 35 truncate statements in that SPROC - and just execute it.

    If you have lots of referential integrity between tables you might need to use DELETE FROM SOMETABLEA instead - but of course that is lots and lots slower and use the log file (which will expand it greatly).
    Thanks sir for guidence
    I want to ask what is best to do?

    Truncate or delete statement??

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: delete all data from all table

    If there is referential integrity between tables then you must use Delete on the Parent side of the relationship. Truncate on any other table.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: delete all data from all table

    As Gary said - TRUNCATE will fail with an error if there are foreign keys and what not.

    Try TRUNCATE TABLE - if it fails - change it do DELETE FROM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: delete all data from all table

    Thanks for guiding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width