我試圖在if內運行查詢block.Like下面在這個查詢中,我首先檢查數據庫是否存在,否則我將執行另一個查詢來創建它。但每當我運行這個查詢執行else塊即使有數據庫存在,該怎麼辦是語法錯誤?如果其他在SQL Server 2008中
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
......
您可以通過使用NOT EXISTS修改腳本,這樣你就不必使用冗餘IF,開始,結束塊。 –
嘗試不存在,但執行相同的事情,看到更新的問題 – sharad