Check if table exists and if it doesn't exist, create it in SQL Server 2008

后端 未结 8 665
名媛妹妹
名媛妹妹 2020-12-07 12:12

I am writing a Stored procedure in SQL Server 2008. I need to check if a table exists in the database. If it doesn\'t then I need to create it.

How do I do this?

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 12:19

    Something like this

    IF  NOT EXISTS (SELECT * FROM sys.objects 
    WHERE object_id = OBJECT_ID(N'[dbo].[YourTable]') AND type in (N'U'))
    
    BEGIN
    CREATE TABLE [dbo].[YourTable](
        ....
        ....
        ....
    ) 
    
    END
    

提交回复
热议问题