DROP IF EXISTS VS DROP?

前端 未结 5 504
南方客
南方客 2020-12-07 09:29

Can someone tell me if there is any difference between

DROP IF EXISTS [TABLE_NAME]
DROP [TABLE_NAME]

I am askin

5条回答
  •  不思量自难忘°
    2020-12-07 09:46

    It is not what is asked directly. But looking for how to do drop tables properly, I stumbled over this question, as I guess many others do too.

    From SQL Server 2016+ you can use

    DROP TABLE IF EXISTS dbo.Table
    

    For SQL Server <2016 what I do is the following for a permanent table

    IF OBJECT_ID('dbo.Table', 'U') IS NOT NULL 
      DROP TABLE dbo.Table; 
    

    Or this, for a temporary table

    IF OBJECT_ID('tempdb.dbo.#T', 'U') IS NOT NULL
      DROP TABLE #T; 
    

提交回复
热议问题