2013-01-31 79 views
1

我試圖在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 
...... 
+0

您可以通過使用NOT EXISTS修改腳本,這樣你就不必使用冗餘IF,開始,結束塊。 –

+0

嘗試不存在,但執行相同的事情,看到更新的問題 – sharad

回答

2

不能使用GO一個BEGIN/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 
+0

什麼可以使用,而不是**去**?它會得到執行,如果我刪除去 – sharad

+0

@sharad - 我已經更新了一個完整的腳本。因爲這些是全新的表格,所以我不會贅述,因爲這些是全新的表格,並且行爲是*稍微*不同(如果數據庫存在但它會嘗試重新添加表格,但是'BrandMaster'沒有) –

+0

@sharad - 噢,我也會考慮從'sp_dboption'切換並使用['ALTER DATBASE'](http://msdn.microsoft.com/en-us/library/ms174269(v = sql.90).aspx )或在最初的'CREATE'中包含適當的選項。這是MS不僅警告將來刪除的情況之一,它已經發生了('sp_dboption'在SQL Server 2012中不可用) –