How do I query if a database schema exists

前端 未结 5 634
野性不改
野性不改 2020-12-25 09:19

As part of our build process we run a database update script as we deploy code to 4 different environments. Further, since the same query will get added to until we drop a r

5条回答
  •  我在风中等你
    2020-12-25 09:39

    @bdukes is right on the money for determining if the schema exists, but the statement above won't work in SQL Server 2005. CREATE SCHEMA needs to run in its own batch. A work around is to execute the CREATE SCHEMA statement in an exec.

    Here is what I used in my build scripts:

    IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = '')
    BEGIN
        -- The schema must be run in its own batch!
        EXEC( 'CREATE SCHEMA ' );
    END
    

提交回复
热议问题