Check if table exists in SQL Server

前端 未结 28 1900
梦如初夏
梦如初夏 2020-11-22 04:23

I would like this to be the ultimate discussion on how to check if a table exists in SQL Server 2000/2005 using SQL Statements.

When you Google for the answer, you g

28条回答
  •  情话喂你
    2020-11-22 05:08

    I've had some problems either with selecting from INFORMATIONAL_SCHEME and OBJECT_ID. I don't know if it's an issue of ODBC driver or something.. Queries from SQL management studio, both, were okay.

    Here is the solution:

    SELECT COUNT(*) FROM 
    

    So, if the query fails, there is, probably, no such table in the database (or you don't have access permissions to it).

    The check is done by comparing the value (integer in my case) returned by SQL executor which deals with ODBC driver..

    if (sqlexec(conectionHandle, 'SELECT COUNT(*) FROM myTable') == -1) {
      // myTable doesn't exist..
    }
    

提交回复
热议问题