Drop all temp tables one time

  • I used code below to drop temp table:

    IF OBJECT_ID(N'tempdb..[#temp1]', N'U') IS NOT NULL

    DROP TABLE [#temp1]

    But in a query, there are about 15 temp table.

    How to code to drop all temp tables one time without going through all temp tables' name?

  • Are you writing a stored procedure?

    If so, you don't need to explicitly drop the temp tables. The whole point is that they'll go away when the connection closes at the end of the proc.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • No, in query window

  • Then you'll need to drop them explicitly.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Use @table vars

  • GSquared (12/10/2010)


    Then you'll need to drop them explicitly.

    Well you could always reboot the server between runs :hehe:.

    Pls don't try this at home folks!

  • nzrdb6 (12/10/2010)


    Use @table vars

    But only if you understand the performance and transactional differences between the two.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 7 posts - 1 through 6 (of 6 total)

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