Check if a SQL table exists

后端 未结 9 2326
北海茫月
北海茫月 2020-12-01 06:30

What\'s the best way to check if a table exists in a Sql database in a database independant way?

I came up with:

   bool exists;
   const string sql         


        
9条回答
  •  隐瞒了意图╮
    2020-12-01 06:51

    I fully support Frederik Gheysels answer. If you have to support multiple database systems, you should implement your code against an abstract interface with specific implementations per database system. There are many more examples of incompatible syntax than just checking for an existing table (e.g.: limiting the query to a certain number of rows).

    But if you really have to perform the check using the exception handling from your example, you should use the following query that is more efficient than a COUNT(*) because the database has no actual selection work to do:

    SELECT 1 FROM my_table WHERE 1=2
    

提交回复
热议问题