Search code examples
sqlif-statementcreate-table

IF...BEGIN...END


I have this query and I get Incorrect Syntax Errors near ')' (end of CREATE TABLE) and near 'END'.

Now, this works fine if I remove the BEGIN and END lines but Im not entirely sure what's wrong with it as is.

Note: This is not the finished product as there is more that will go after this and a lot more that preceded it. This is just one part of a much larger project but the parts are fairly independent and nothing in this query relies on any other part other than the existance of that [ARCSCON] table.

Can anyone tell me what I'm doing wrong?

IF  EXISTS( SELECT * FROM sys.objects 
    WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[ARCSCON]'))
BEGIN

CREATE TABLE  [tempSAItems](        
            [Id] int identity(44444,1),
            [Name] nvarchar(55),
            [Type] nvarchar(50) DEFAULT 'Service Agreement',
            [Inactive] bit DEFAULT '0',
            [Purchased] bit DEFAULT '0',
            [MSDS] bit DEFAULT '0',
            [IncomeAccountID] int DEFAULT '7',
            [LaborCoverd] bit DEFAULT '0',
            [PartsCoverd] bit DEFAULT '0',
            [LifeTime] bit DEFAULT '0',
            [TravelCoverd] bit DEFAULT '0',
            [NumVisits] int, --[DURATION]
            [bLaborItem] bit DEFAULT '0',
            [bDirectCost] bit DEFAULT '0',
            [bAddToSales] bit DEFAULT '0',
            [sCostingMethod] nvarchar(50) DEFAULT 'Average Cost')
GO

INSERT INTO [tempSAItems]([Name])
SELECT DISTINCT [SCHEDTYPE]
FROM [ARCSCON]
GO  


END

Solution

  • As @LittleBoobyTables commented just Rid of the Go written at the middle.

    IF  EXISTS( SELECT * FROM sys.objects 
        WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[ARCSCON]'))
    BEGIN
    
    CREATE TABLE  [tempSAItems](        
                [Id] int identity(44444,1),
                [Name] nvarchar(55),
                [Type] nvarchar(50) DEFAULT 'Service Agreement',
                [Inactive] bit DEFAULT '0',
                [Purchased] bit DEFAULT '0',
                [MSDS] bit DEFAULT '0',
                [IncomeAccountID] int DEFAULT '7',
                [LaborCoverd] bit DEFAULT '0',
                [PartsCoverd] bit DEFAULT '0',
                [LifeTime] bit DEFAULT '0',
                [TravelCoverd] bit DEFAULT '0',
                [NumVisits] int, --[DURATION]
                [bLaborItem] bit DEFAULT '0',
                [bDirectCost] bit DEFAULT '0',
                [bAddToSales] bit DEFAULT '0',
                [sCostingMethod] nvarchar(50) DEFAULT 'Average Cost')
    --GO
    ^Here
    
    INSERT INTO [tempSAItems]([Name])
    SELECT DISTINCT [SCHEDTYPE]
    FROM [ARCSCON]
    GO  
    
    
    END