Check if table exists in SQL Server

前端 未结 28 1515
梦如初夏
梦如初夏 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 04:52

    You can use this :

         IF OBJECT_ID (N'dbo.T', N'U') IS NOT NULL 
            BEGIN 
                print 'deleted table';
                drop table t 
            END
         else 
            begin 
                print 'table not found' 
            end
    
     Create table t (id int identity(1,1) not null, name varchar(30) not null, lastname varchar(25) null)
     insert into t( name, lastname) values('john','doe');
     insert into t( name, lastname) values('rose',NULL);
    
     Select * from t
    1   john    doe
    2   rose    NULL
    
     -- clean
     drop table t
    

提交回复
热议问题