Database creation in IF block

  • I get the following error when attempting to run this script; Line 17: Incorrect syntax near ')'  Is this a problem with my IF block or something in the database creation?

    IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'IDGStuff')

    BEGIN

     PRINT 'ERROR!  The IDGStuff database already exists on this computer.'

    END

    ELSE

    BEGIN

     /* create the database */

     CREATE DATABASE [IDGStuff]  ON (

      NAME = 'IDGStuff_Data',

      FILENAME = 'C:\SQLDATA\data\IDGStuff_Data.MDF' ,

      SIZE = 3,

      FILEGROWTH = 10%)

      LOG ON (

      NAME = 'VCDatabase_Log',

      FILENAME = 'C:\SQLDATA\data\IDGStuff_Log.LDF' ,

      SIZE = 1,

      FILEGROWTH = 10% )

     GO

     USE IDGStuff

     GO

     CREATE TABLE [IDGSeed] (

      [IDG_ID] [int] IDENTITY (1, 1) NOT NULL ,

      [IDG_Value] [varchar] (10) NULL)  ON [PRIMARY]

    END

     

     

  • The GO statements in the middle of the BEGIN....END are causing the problem.

    This should work (you may need to put an IF EXISTS.... around your create table as well:-

    IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'IDGStuff')

    BEGIN

     PRINT 'ERROR!  The IDGStuff database already exists on this computer.'

    END

    ELSE

    BEGIN

     /* create the database */

     CREATE DATABASE [IDGStuff]  ON (

      NAME = 'IDGStuff_Data',

      FILENAME = 'C:\SQLDATA\data\IDGStuff_Data.MDF' ,

      SIZE = 3,

      FILEGROWTH = 10%)

      LOG ON (

      NAME = 'VCDatabase_Log',

      FILENAME = 'C:\SQLDATA\data\IDGStuff_Log.LDF' ,

      SIZE = 1,

      FILEGROWTH = 10% )

    END

     GO

     USE IDGStuff

     GO

     CREATE TABLE [IDGSeed] (

      [IDG_ID] [int] IDENTITY (1, 1) NOT NULL ,

      [IDG_Value] [varchar] (10) NULL)  ON [PRIMARY]

  • Thanks Ian.  That makes sense although it's a bummer that you can't encapsulate all that logic in one IF block.

    Have a great day!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply