检查SQL Server中是否存在表

不羁的心 提交于 2020-08-10 12:14:16

问题:

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. 我希望这是关于如何使用SQL语句检查SQL Server 2000/2005中是否存在表的最终讨论。

When you Google for the answer, you get so many different answers. 当您用Google搜索答案时,会得到很多不同的答案。 Is there an official/backward and forward compatible way of doing it? 有官方/向后和向前兼容的方式吗?

Here are two possible ways of doing it. 这是两种可能的方法。 Which one among the two is the standard/best way of doing it? 两种方法中的哪一种是标准/最佳方法?

First way: 第一种方式:

IF EXISTS (SELECT 1 
           FROM INFORMATION_SCHEMA.TABLES 
           WHERE TABLE_TYPE='BASE TABLE' 
           AND TABLE_NAME='mytablename') 
   SELECT 1 AS res ELSE SELECT 0 AS res;

Second way: 第二种方式:

IF OBJECT_ID (N'mytablename', N'U') IS NOT NULL 
   SELECT 1 AS res ELSE SELECT 0 AS res;

MySQL provides the simple MySQL提供的简单

SHOW TABLES LIKE '%tablename%'; 

statement. 声明。 I am looking for something similar. 我正在寻找类似的东西。


解决方案:

参考一: https://stackoom.com/question/haq/检查SQL-Server中是否存在表
参考二: https://oldbug.net/q/haq/Check-if-table-exists-in-SQL-Server
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!