Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:59:07+00:00 2026-06-18T02:59:07+00:00

i am trying to run query within if else block.Like below In this query

  • 0

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
......
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:59:08+00:00Added an answer on June 18, 2026 at 2:59 am

    You can’t use GO inside a BEGIN/END block. GO(*) is a command to your client tool to take all of the text back to the previous GO (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:

    IF
    BEGIN
    END
    ELSE
    BEGIN
    

    With no END for that last BEGIN, 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:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am trying to run this query in SQL but it is not
I'm currently trying to run a LINQ query over a MS SQL database. This
I'm trying to run a query to a MySQL database from within a class
Hi i am trying to run a query on a database, but i keep
Hey guys I am getting this exception when trying to run the query Column
I'm trying to run the following query, but am not sure how to limit
I am trying to run a mysql query within some php and have the
I am trying to query a database within grails using: def per = User.get(springSecurityService.principal.id)
I'm trying to query a string to match within a range in the database.
Within a ASP.NET/C# class I am trying to run a query where I compare

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.