我正在尝试在 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
......