1

我正在尝试在 if else 块中运行查询。如下所示在此查询中,我首先检查数据库是否存在,否则我将执行另一个查询来创建它。但是每次运行此查询时,即使存在,它也会执行 else 块数据库存在,语法错误怎么办?

IF EXISTS(SELECT name FROM sys.databases WHERE name = 'SampleDB')
    BEGIN
    END
    ELSE 
    BEGIN   
    CREATE DATABASE [SampleDB]  
    go
    use [SampleDB] 
    Go

    exec sp_dboption N'SampleDB', N'autoclose', N'false'
    GO

    exec sp_dboption N'SampleDB', N'bulkcopy', N'false'
    GO

    exec sp_dboption N'SampleDB', N'trunc. log', N'false'
    GO

    exec sp_dboption N'SampleDB', N'torn page detection', N'true'
    GO

    exec sp_dboption N'SampleDB', N'read only', N'false'
    GO

    exec sp_dboption N'SampleDB', N'dbo use', N'false'
    GO

    exec sp_dboption N'SampleDB', N'single', N'false'
    GO

    exec sp_dboption N'SampleDB', N'autoshrink', N'false'
    GO

    exec sp_dboption N'SampleDB', N'ANSI null default', N'false'
    GO

    exec sp_dboption N'SampleDB', N'recursive triggers', N'false'
    GO

    exec sp_dboption N'SampleDB', N'ANSI nulls', N'false'
    GO

    exec sp_dboption N'SampleDB', N'concat null yields null', N'false'
    GO

    exec sp_dboption N'SampleDB', N'cursor close on commit', N'false'
    GO

    exec sp_dboption N'SampleDB', N'default to local cursor', N'false'
    GO

    exec sp_dboption N'SampleDB', N'quoted identifier', N'false'
    GO

    exec sp_dboption N'SampleDB', N'ANSI warnings', N'false'
    GO

    exec sp_dboption N'SampleDB', N'auto create statistics', N'true'
    GO

    exec sp_dboption N'SampleDB', N'auto update statistics', N'true'
    GO


    CREATE TABLE [dbo].[BrandMaster] (
        [BrandId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrandMaster] PRIMARY KEY NOT NULL ,
        [BrandName] [nvarchar] (50) NOT NULL ,
        [BrandStatus] [bit] NOT NULL ,
         )
    GO


    SET IDENTITY_INSERT [dbo].[BrandMaster] ON
    SET IDENTITY_INSERT [dbo].[BrandMaster] OFF



    CREATE TABLE [dbo].[BrandProductMaster] (
        [BrandProductId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrandProductMaster] PRIMARY KEY NOT NULL ,
        [ProductId] [int] NOT NULL ,
        [BrandId] [int] NOT NULL ,
        [Units] [nvarchar] (15) NULL ,
        [Status] [bit] NOT NULL ,
         )
    GO


    SET IDENTITY_INSERT [dbo].[BrandProductMaster] ON
    SET IDENTITY_INSERT [dbo].[BrandProductMaster] OFF




    CREATE TABLE [dbo].[BrokerMaster] (
        [BrokerId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrokerMaster] PRIMARY KEY NOT NULL ,
        [BrokerName] [nvarchar] (100) NOT NULL ,
        [BrokerPercentage] [float] NOT NULL ,
        [BrokerAddress] [nvarchar] (100) NULL ,
        [BrokerTelephoneNo] [bigint] NULL ,
        [BrokerMobileNo] [bigint] NULL ,
        [BrokerFaxNo] [bigint] NULL ,
        [BrokerEmailId] [nvarchar] (75) NULL ,
        [BrokerStatus] [bit] NOT NULL ,
    )
    GO

    SET IDENTITY_INSERT [dbo].[BrokerMaster] ON
    INSERT INTO [dbo].[BrokerMaster] ([BrokerId],[BrokerName],[BrokerPercentage],[BrokerAddress],[BrokerEmailId],[BrokerStatus])
        VALUES (1,'No Broker',0.0,'','',1)
    GO

    SET IDENTITY_INSERT [dbo].[BrokerMaster] OFF

    GO
END

编辑后:

IF NOT EXISTS(SELECT name FROM sys.databases WHERE name = 'SampleDB')
    CREATE DATABASE [SampleDB]  
    go
    use [SampleDB] 
    Go

    exec sp_dboption N'SampleDB', N'autoclose', N'false'
    GO

    exec sp_dboption N'SampleDB', N'bulkcopy', N'false'
    GO

    exec sp_dboption N'SampleDB', N'trunc. log', N'false'
    GO

    exec sp_dboption N'SampleDB', N'torn page detection', N'true'
    GO
......
4

1 回答 1

2

您不能GOBEGIN/END块内使用。GO(*) 是您的客户端工具的命令,用于将所有文本返回到前一个GO(或文件的开头)并要求 SQL Server 执行它(称为批处理)。SQL Server 编译所有这些文本,然后执行它。所以它会看到一个不完整的结构:

IF
BEGIN
END
ELSE
BEGIN

没有END最后一个BEGIN,所以你总是会得到一个错误。它永远不会真正尝试执行代码。


(*) 实际上,它可以是任何东西。但只有精神病患者才会改变他们的工具设置,使其远离默认值GO.


我会这样做:

IF NOT EXISTS(SELECT name FROM sys.databases WHERE name = 'SampleDB')
BEGIN   
    CREATE DATABASE [SampleDB]  

    exec sp_dboption N'SampleDB', N'autoclose', N'false'
    exec sp_dboption N'SampleDB', N'bulkcopy', N'false'
    exec sp_dboption N'SampleDB', N'trunc. log', N'false'
    exec sp_dboption N'SampleDB', N'torn page detection', N'true'
    exec sp_dboption N'SampleDB', N'read only', N'false'
    exec sp_dboption N'SampleDB', N'dbo use', N'false'
    exec sp_dboption N'SampleDB', N'single', N'false'
    exec sp_dboption N'SampleDB', N'autoshrink', N'false'
    exec sp_dboption N'SampleDB', N'ANSI null default', N'false'
    exec sp_dboption N'SampleDB', N'recursive triggers', N'false'
    exec sp_dboption N'SampleDB', N'ANSI nulls', N'false'
    exec sp_dboption N'SampleDB', N'concat null yields null', N'false'
    exec sp_dboption N'SampleDB', N'cursor close on commit', N'false'
    exec sp_dboption N'SampleDB', N'default to local cursor', N'false'
    exec sp_dboption N'SampleDB', N'quoted identifier', N'false'
    exec sp_dboption N'SampleDB', N'ANSI warnings', N'false'
    exec sp_dboption N'SampleDB', N'auto create statistics', N'true'
    exec sp_dboption N'SampleDB', N'auto update statistics', N'true'
END
go
use SampleDB
go
if not exists (select * from sys.tables where name='BrandMaster')
begin
    exec sp_executesql N'CREATE TABLE [dbo].[BrandMaster] (
        [BrandId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrandMaster] PRIMARY KEY NOT NULL ,
        [BrandName] [nvarchar] (50) NOT NULL ,
        [BrandStatus] [bit] NOT NULL ,
         )'

    exec sp_executesql N'CREATE TABLE [dbo].[BrandProductMaster] (
        [BrandProductId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrandProductMaster] PRIMARY KEY NOT NULL ,
        [ProductId] [int] NOT NULL ,
        [BrandId] [int] NOT NULL ,
        [Units] [nvarchar] (15) NULL ,
        [Status] [bit] NOT NULL ,
         )'

    exec sp_executesql N'CREATE TABLE [dbo].[BrokerMaster] (
        [BrokerId] [int] IDENTITY (1, 1) CONSTRAINT [PK_BrokerMaster] PRIMARY KEY NOT NULL ,
        [BrokerName] [nvarchar] (100) NOT NULL ,
        [BrokerPercentage] [float] NOT NULL ,
        [BrokerAddress] [nvarchar] (100) NULL ,
        [BrokerTelephoneNo] [bigint] NULL ,
        [BrokerMobileNo] [bigint] NULL ,
        [BrokerFaxNo] [bigint] NULL ,
        [BrokerEmailId] [nvarchar] (75) NULL ,
        [BrokerStatus] [bit] NOT NULL ,
    )'
    exec sp_executesql N'INSERT INTO [dbo].[BrokerMaster] ([BrokerName],[BrokerPercentage],[BrokerAddress],[BrokerEmailId],[BrokerStatus])
        VALUES (''No Broker'',0.0,'''','''',1)'
END
于 2013-01-31T14:38:20.930 回答