如何检查在c#中数据库(ACCESS或SQL)中是否存在表?

时间:2021-12-03 07:22:08

I found a lot of questions regarding with this question.

关于这个问题我发现了很多问题。

But is there any simple statements to accomplish this task?

但是有什么简单的语句可以完成这个任务吗?

for both SQL and ACCESS

用于SQL和ACCESS

3 个解决方案

#1


4  

IF (EXISTS (SELECT 1 FROM sys.tables WHERE name = 'table_name'))
BEGIN
    -- do stuff
END

sys.tables can also give you some information about the table object, e.g. the is_replicated column tells you if the table was created by replication or the has_replication_filter column tells you if the table has a replication filter set up

sys。表还可以提供关于表对象的一些信息,例如is_replication列告诉您表是通过复制创建的,或者has_replication_filter列告诉您表是否设置了复制过滤器

NB: this is for SQL Server

这是SQL Server

Edit: For Access:

编辑:访问:

SELECT COUNT(*) as Exists from MsysObjects 
WHERE type = 1
AND name = 'MY_TABLE_NAME' 

#2


2  

Note that there is no standardized way to do this in SQL, you will have to write plattform-specific code.

请注意,在SQL中没有标准化的方法来实现这一点,您将不得不编写特定于plattform的代码。

To my knowledge, all DBMS have this functionality in one way or another, but it differs greatly, eg in Oracle you can query the sys.all_tables view.

据我所知,所有的DBMS都具有这种或那种功能,但是它有很大的不同(如在Oracle中可以查询sys)。all_tables视图。

#3


0  

You can also do using OBJECT_ID.

您还可以使用OBJECT_ID。

IF OBJECT_ID('table1') IS NOT NULL
print 'Exists' 
else
print 'Not Exists' 

#1


4  

IF (EXISTS (SELECT 1 FROM sys.tables WHERE name = 'table_name'))
BEGIN
    -- do stuff
END

sys.tables can also give you some information about the table object, e.g. the is_replicated column tells you if the table was created by replication or the has_replication_filter column tells you if the table has a replication filter set up

sys。表还可以提供关于表对象的一些信息,例如is_replication列告诉您表是通过复制创建的,或者has_replication_filter列告诉您表是否设置了复制过滤器

NB: this is for SQL Server

这是SQL Server

Edit: For Access:

编辑:访问:

SELECT COUNT(*) as Exists from MsysObjects 
WHERE type = 1
AND name = 'MY_TABLE_NAME' 

#2


2  

Note that there is no standardized way to do this in SQL, you will have to write plattform-specific code.

请注意,在SQL中没有标准化的方法来实现这一点,您将不得不编写特定于plattform的代码。

To my knowledge, all DBMS have this functionality in one way or another, but it differs greatly, eg in Oracle you can query the sys.all_tables view.

据我所知,所有的DBMS都具有这种或那种功能,但是它有很大的不同(如在Oracle中可以查询sys)。all_tables视图。

#3


0  

You can also do using OBJECT_ID.

您还可以使用OBJECT_ID。

IF OBJECT_ID('table1') IS NOT NULL
print 'Exists' 
else
print 'Not Exists'