简体   繁体   English

如何检查数据库中是否已存在表?

[英]How to check if a table already exists in the database?

Now I am using something like: 现在我使用的是:

IF (object_id('table', 'U') is not null)  
AND (NOT EXISTS (SELECT * FROM [table] WHERE id = 0))  
    RETURN; 

to check if this 'table' already exists in my database but I get the next error message while running the script if there is no such table in the current database: 检查我的数据库中是否已存在此“表”,但如果当前数据库中没有此类表,则在运行脚本时会收到下一条错误消息:

Msg 208, Level 16, State 1, Line 5 Msg 208,Level 16,State 1,Line 5
Invalid object name 'table'. 无效的对象名称't​​able'。

Is this correct way to check if a table exists in a database? 这是检查数据库中是否存在表的正确方法吗? Or may be I must change any settings of my SQL Management Studio? 或者我可能必须更改SQL Management Studio的任何设置?

ANSWER : I changed my script and everything works well: 答案 :我改变了我的脚本,一切运作良好:

IF (object_id('table', 'U') is not null)  
    IF (NOT EXISTS (SELECT * FROM [table] WHERE id = 0))  
        RETURN;  
IF EXISTS (SELECT * 
           FROM sys.objects 
           WHERE object_id = OBJECT_ID(N'table') 
           AND type in (N'U'))

I ran into similar issues found doing the query as such helped. 我遇到了类似的问题,发现这样做有助于查询。 The N meerly indicates unicode N meerly表示unicode

if (object_id('tablex', 'U') is not null) 
begin 
select * from tablex  
end

works for me. 适合我。 If the table does not exist then compilation of that statement should be deferred (assuming that you are on SQL Server 2005+). 如果该表不存在,那么应该推迟该语句的编译(假设您使用的是SQL Server 2005+)。

If you are referencing the table in anything other than the statement immediately after the if you need to use begin...end . 如果你是后立即引用除声明以外的任何表if你需要使用begin...end

Updated. 更新。

You need to use 你需要使用

IF (object_id('table', 'U') is not null)
    IF (NOT EXISTS (SELECT * FROM [table] WHERE id = 0))
       RETURN; 

Your existing statement cannot compile as is. 您现有的语句无法按原样编译。 The whole statement gets compiled and all objects must exist at that point even if compilation of the statement has previously been deferred. 整个语句被编译,并且所有对象必须在该点存在,即使先前已经推迟了语句的编译。

Additionally SQL Server is not guaranteed to do short circuit evaluation in any case. 此外,SQL Server 无法保证在任何情况下都进行短路评估

This might work better for you 这可能对你更好

if (object_id('table', 'U') is not null)
  if not exists (SELECT * FROM [table] WHERE id = 0)

you can do something like this: 你可以这样做:

IF (EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES 
    WHERE TABLE_TYPE='BASE TABLE' 
    AND TABLE_NAME='QRTZ_JOB_DETAILS'))
    BEGIN
    PRINT 'EXISTS'
    END

where TABLE_NAME is the name ob the table you want to check in your database. 其中TABLE_NAME是您要在数据库中检查的表的名称ob。

UPDATE: 更新:

if you're using SQL SERVER 2000+ msarchet answer is more appropriate, though. 如果您使用的是SQL SERVER 2000+,那么msarchet的答案更合适。

This is one of many ways. 这是众多方式之一。

   IF EXISTS (SELECT 1 
        FROM sysobjects 
        WHERE xtype='u' AND name='tablename') 
            SELECT 'tablename exists.' 
    ELSE 
            SELECT 'tablename does not exist.' 

I think u provide the Table Name to [Table], if so try to change it to Something else. 我想你提供表名到[表],如果是这样,试着把它改成别的东西。 Then it will work fine i think. 那我觉得它会很好用。 Then all the above said answers will work fine. 那么上述所有答案都可以正常使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM