Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:22:18+00:00 2026-06-09T00:22:18+00:00

This query is supposed to be dropping temp tables/views from a sql DB however

  • 0

This query is supposed to be dropping temp tables/views from a sql DB however when dropping the last table/view it keeps looping. Anyone have an answer as why this would be going into an infinite loop when dropping the last table?

DECLARE @name VARCHAR(128)

DECLARE @SQL VARCHAR(254)

DECLARE @type VARCHAR(50)

DECLARE @tmp_table TABLE
    (
      table_name VARCHAR(50)
    , table_type VARCHAR(50)
    )

;WITH    cte_tempTables

          AS (
               SELECT
                table_name
              , crdate
              , crdate + 90 [ExperiationDate]
              , TABLE_TYPE
               FROM
                INFORMATION_SCHEMA.TABLES t
               inner join sysobjects s
                on s.name = t.table_name
               WHERE
                TABLE_CATALOG = 'SBR_Temp'
                AND t.table_name NOT IN ( 'DaleDelq' ,'tblCancelContract' ,
                                          'tblCreateContracts' ,'MWFRTPay' )

             )

    INSERT INTO
        @tmp_table
        (
          table_name
        , table_type 
        )
        select
            table_name
          , table_type

        FROM
            [cte_tempTables]
        WHERE
            ExperiationDate < GETDATE()

SELECT TOP 1
    @name = [table_name]
  , @type = CASE WHEN [table_type] = 'BASE TABLE' THEN 'TABLE'
                 ELSE 'VIEW'
            END

FROM
    @tmp_table

WHILE @name IS NOT NULL

    OR @name <> ''

    BEGIN
        SELECT
            @SQL = 'DROP ' + @type + ' SBR_Temp.[dbo].[' + RTRIM(@name) + ']'
            --EXEC (@SQL)
        PRINT 'Dropped ' + @type + ':' + @name

        DELETE
            @tmp_table
        WHERE
            [table_name] = @name

        SELECT TOP 1
            @name = [table_name]
          , @type = CASE WHEN [table_type] = 'BASE TABLE' THEN 'TABLE'
                         ELSE 'VIEW'
                    END
        FROM
            @tmp_table
            SELECT @name
    END
GO

here is an example of the results

(4 row(s) affected)
Dropped VIEW:vue_SunsetCoveClientInventory

(1 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_SunsetCoveClientCoOwners

(1 row(s) affected)

(1 row(s) affected)
Dropped TABLE:BKDischarge

(1 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(1 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(0 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(0 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(0 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(0 row(s) affected)

(1 row(s) affected)
Dropped VIEW:vue_nocoop

(0 row(s) affected)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:22:21+00:00Added an answer on June 9, 2026 at 12:22 am

    Once you have no more rows in @tmp_table, you are no longer changing the value of @name. I think you’d rather use:

    WHILE EXISTS (SELECT NULL FROM @tmp_table)
    BEGIN
        SELECT TOP 1...
    

    Changing to this style of check also has the benefits of:

    • You’re no longer assuming that you have at least one row to start with.
    • You don’t have to maintain the same SELECT TOP 1... in two places.

    You can run this demo code to see your issue in simplified form:

    -- Setup two rows of example data
    declare @table table (
        id int primary key
    )
    insert into @table select 1
    insert into @table select 2
    
    declare @id int
    
    -- Select, display and delete the first row
    select top 1 @id = id from @table
    select @id
    delete from @table where id = @id
    
    -- Select, display and delete the second row
    select top 1 @id = id from @table
    select @id
    delete from @table where id = @id
    
    -- Nothing left to select, but @id still retains its value!
    select top 1 @id = id from @table
    select @id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This query is supposed to run with ms access 2003 using SQL. the function
this query SELECT * FROM tblContracts LEFT JOIN tblPartys ON tblContracts.id = tblPartys.Contract_id INNER
This query works fine for me: $query = SELECT p.topnode_id, p.param_key, p.param_value FROM tbl_params
This query works: item = db.GqlQuery(SELECT * FROM Item WHERE CSIN = 13)[0] although
I want to write a query like this: For a table that has these
I am doing this query in a stored procedure: SELECT TOP 1 [Employe] FROM
Here's the query that is being executed on SQL Server 2008: SELECT "dbo"."tblMainData"."recID" FROM
Are Table names in a Query supposed to be Case-SensitIvE in MySQL? For example,
I want results from three tables where the results from the first table are
Is order of elements returned from query supposed to match order of elements in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.