Drop Table if exists

  • HI,

    For Droping a table if exists I am using below commands ,Is there any other efficient way of doing same

    if exists (select * from dbo.sysobjects where name ='ABC_school')

    DROP TABLE XYZ_DB..ABC_school

  • I tend to simply check for the object_id a lot:

    I also tend to put the command for create/drop/alter for the table in dynamic SQL to avoid syntax checking.

    IF OBJECT_ID('ABC_School') IS NOT NULL

    EXEC('DROP TABLE ABC_School')

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Why is avoiding syntax checking a good idea?

  • laurie-789651 (8/23/2012)


    Why is avoiding syntax checking a good idea?

    It's a holdover from how my scripts get deployed; trying to avoid GO statements, so everything gets wrapped into dynamic SQL for the DDL commands; I'd have to create the scenario as a demo, but as i remember it, for example if we add a new column to an existing table, and then UPDATE that table with a command that references the new column, you get a syntax error because the column didn't exist when the entire command was validated.

    CREATE TABLE Bananas(BananaId int identity(1,1) not null primary key)

    GO

    ALTER TABLE Bananas ADD NewCol varchar(30)

    UPDATE Bananas

    SET NewCol = 'Green Panamanian'

    Msg 207, Level 16, State 1, Line 2

    Invalid column name 'NewCol'.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I would suggest:

    IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[ABC_SCHEMA].[ABC_TABLE]') AND [Type] IN (N'U'))

    DROP TABLE [ABC_SCHEMA].[ABC_TABLE]

    Because:

    1. It's safer to check the schema in witch the table exists, to avoid deletion of similar table name in different schema or script execution failure.

    2. Checking de object type, avoid script execution failure in case there is a view with similar name.

  • Lowell (8/23/2012)


    laurie-789651 (8/23/2012)


    Why is avoiding syntax checking a good idea?

    It's a holdover from how my scripts get deployed; trying to avoid GO statements, so everything gets wrapped into dynamic SQL for the DDL commands; I'd have to create the scenario as a demo, but as i remember it, for example if we add a new column to an existing table, and then UPDATE that table with a command that references the new column, you get a syntax error because the column didn't exist when the entire command was validated.

    CREATE TABLE Bananas(BananaId int identity(1,1) not null primary key)

    GO

    ALTER TABLE Bananas ADD NewCol varchar(30)

    UPDATE Bananas

    SET NewCol = 'Green Panamanian'

    Msg 207, Level 16, State 1, Line 2

    Invalid column name 'NewCol'.

    Yes - I've come across that myself.

    I was interested to know because at this site there are huge sections of dynamic SQL with no dynamic element. I'll check & see if that offers an explanation.

    Thanks

  • only way

  • Thanks a lot for suggestions: 🙂

  • MSzI (8/23/2012)


    I would suggest:

    IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[ABC_SCHEMA].[ABC_TABLE]') AND [Type] IN (N'U'))

    DROP TABLE [ABC_SCHEMA].[ABC_TABLE]

    Because:

    1. It's safer to check the schema in witch the table exists, to avoid deletion of similar table name in different schema or script execution failure.

    2. Checking de object type, avoid script execution failure in case there is a view with similar name.

    I agree with your points, but there's no need for the verbose syntax. Something like this does the same in less space:

    if object_id('[schema].[Test]', 'U') is null

    --do something

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin (8/23/2012)


    MSzI (8/23/2012)


    I would suggest:

    IF EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[ABC_SCHEMA].[ABC_TABLE]') AND [Type] IN (N'U'))

    DROP TABLE [ABC_SCHEMA].[ABC_TABLE]

    Because:

    1. It's safer to check the schema in witch the table exists, to avoid deletion of similar table name in different schema or script execution failure.

    2. Checking de object type, avoid script execution failure in case there is a view with similar name.

    I agree with your points, but there's no need for the verbose syntax. Something like this does the same in less space:

    if object_id('[schema].[Test]', 'U') is null

    --do something

    I agree on using the object_id, but for a drop table you would want to check that the object_id is NOT null:

    if object_id('[schema].[Test]', 'U') is not null

    drop table [schema].[Test]

Viewing 10 posts - 1 through 9 (of 9 total)

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