![]() Can be used for any other object as well. Of course, you can add a message if want. Why go to the bother of checking if a table actually exists, when you can just try and drop using BEGIN TRY without getting any error messages if the table doesn't exist. Overall, this is a great reference sheet and I have already book-marked it. Since we don’t use the older versions that often, I sometimes forget that you can’t use just “DROP TABLE IF EXISTS” and I find myself scrambling to remember the “IF OBJECT_ID…” variation for the older SQL installs. At my place of business, we have different installs of SQL ranging from SQL Server 2008 to SQL Server 2019. Thanks for putting together this list of “DROP TABLE” options. Here, querying the related system views, we see our view, stored procedure, and Here we see a graphical representation of the tables just created. To get started, I’ve created a new database called MyDatabase for myįirst, we’ll try to drop a table that does not exist without using any Membership in the db_ddladmin fixed database role.The DROP TABLE statement requires the user to have one of the following: DROP TABLE with IF EXISTS (SQL Server 2016 and up). ![]() Querying the sys.tables System View (all supported versions).OBJECT_ID() function (all supported versions). ![]() If it doesn't exist you can skip the DROP TABLE.Īt an example of what we see if we attempt to drop a table that doesn’t existĪnd the following ways to conditionally run DROP TABLE: The solution is to add conditional logic to your T-SQL to check if the tableĮxists before trying to drop the table. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |