2013-08-23 70 views

回答

2
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES T 
     WHERE T.TABLE_SCHEMA = 'dbo' 
     AND T.TABLE_NAME = 'YOURTABLENAME' ) 
    BEGIN 

     CREATE TABLE dbo.YOURTABLENAME 
     (
      ColumnDefinitionsHere 
     ) 
    END 

GO 
+0

謝謝你,它的完成 –

0
(SELECT count(*) 
       FROM INFORMATION_SCHEMA.TABLES 
       WHERE TABLE_SCHEMA = 'TheSchema' 
       AND TABLE_NAME = 'TheTable') 

如果count返回0則意味着該表沒有退出

0

你需要尋找到一個系統視圖做到這一點:

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SomeTable]') AND type in (N'U')) 
BEGIN 
    CREATE TABLE [dbo].[SomeTable](
     [SomeId] [int] NOT NULL 
    ) 
END ELSE PRINT 'SomeTable already exists.'; 
相關問題