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 7563821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:45:45+00:00 2026-05-30T13:45:45+00:00

I use this command to drop a table in sql-server 2008 IF EXISTS(SELECT name

  • 0

I use this command to drop a table in sql-server 2008

IF EXISTS(SELECT name FROM [DBName]..sysobjects WHERE name = N'TableName' AND xtype='U')
DROP TABLE [DBName].[SchemaName].[TableName];

But now I have 2 tables with same name in different schema:

[DBName].[Schema1].[Members]

And

 [DBName].[Schema2].[Members]

So, what is your suggestion for check if exist this tables? How can I check table names with schema?

UPDATE:

OK, there is 3 different answers and all of them worked, so I don’t know which one is better, does any one know about use object_id or sys.tables?

  • 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-05-30T13:45:46+00:00Added an answer on May 30, 2026 at 1:45 pm
    IF EXISTS(
      SELECT * 
      FROM [DBName].sys.tables t
      JOIN [DBName].sys.schemas s
        ON t.SCHEMA_ID = s.schema_id
      WHERE 
          t.name = N'TableName' AND t.type='U' 
      AND s.NAME = 'SchemaName'
    )
      DROP TABLE [DBName].[SchemaName].[TableName];
    

    Update:

    object_id in sys.tables is the same as object_id in sysobjects for the same table. And is completely the same as function OBJECT_ID returns for the same table name. See the following illustrating examples.

    So, you may simplify the query:

    IF exists 
      (
        SELECT * 
        FROM DBName.sys.tables 
        WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
        AND type = 'U'
      )
      DROP TABLE [DBName].[SchemaName].[TableName];
    

    or in this way:

    IF exists 
      (
        SELECT * 
        FROM DBName.sys.objects
        WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
        AND type = 'U'
      )
      DROP TABLE [DBName].[SchemaName].[TableName];
    

    or for sql2000-styled tables:

    IF exists 
      (
        SELECT * 
        FROM DBName..sysobjects
        WHERE object_id = OBJECT_ID('[DBName].[SchemaName].[TableName]')
        AND xtype = 'U'
      )
      DROP TABLE [DBName].[SchemaName].[TableName];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this command to copy all files whose names start with 'file' from
I use this $query = SELECT * FROM info ORDER BY id DESC limit
I'm trying to drop a SQL Server database using the following code: SqlCommand command
In pylint I use this command --reports=n to disable the reports, but now I
On OS X I use this command to convert a sound file to CAFF
How do you use the nant <copy> command and maintain the directory structure? This
I use this code to update data in database table. Can reuse same code
I have an url return an XML page result. When I use this command:
I currently use this function to wrap executing commands and logging their execution, and
use this website a lot but first time posting. My program creates a number

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.