i am trying to run query within if else block.Like below In this query first i check for if database exist or not else i will execute another query to create it.But every time when i run this query it executing else block even if there database exist,what to do is syntax is wrong?
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
After EDIT :
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
......
You can’t use
GOinside aBEGIN/ENDblock.GO(*) is a command to your client tool to take all of the text back to the previousGO(or the start of the file) and ask SQL Server to execute it (it’s called a batch). SQL Server compiles all of that text and then executes it. So it will see an incomplete structure:With no
ENDfor that lastBEGIN, so you’ll always get an error. It never gets as far as actually trying to execute the code.(*) Actually, it could be anything. But only a psychopath would alter their tool settings away from the default value of
GO.I would do this: