views:

1314

answers:

4

Hello everyone,

I am using SQL Server 2008 management studio to execute the following SQL statements, and here is the related error message from SQL Server management studio. Any ideas what is wrong?

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

Create TABLE [dbo].[BatchStatus](
    [BatchID] [uniqueidentifier] NOT NULL CONSTRAINT [PK_BatchStatus_ID],
    [BatchStatus] [int] NULL,
    CONSTRAINT [PK_BatchStatus_ID] PRIMARY KEY CLUSTERED 
    (
        [BatchID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO



Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ','.
Msg 319, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'with'. If this statement is a common table
expression, an xmlnamespaces clause or a change tracking context clause, the 
previous statement must be terminated with a semicolon.

thanks in advance, George

+1  A: 

Try this:

Create TABLE [dbo].[BatchStatus](
    [BatchID] [uniqueidentifier] NOT NULL,
    [BatchStatus] [int] NULL,
    CONSTRAINT [PK_BatchStatus_ID] PRIMARY KEY CLUSTERED 
    (
        [BatchID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
dario-g
Your SQL statement is the same as mine? Confused... :-)
George2
It's not. He removed CONSTRAINT [PK_BatchStatus_ID] after the BatchID definition.
jitter
Thanks, your solution works! But I am so confused, we are not allowing constraints after column definition? I just copy some workable SQL statement from my legacy SQL Server version (2005) projects. Is it a new restriction or grammar in SQL Server 2008?
George2
It's from SQL Server version (2005) but you should look at yours database compatibility setting.
dario-g
+1  A: 

You are defining the "PK_BatchStatus_ID" constraint twice - once on the line where you define the BatchID field, once at the end of the table definition.

You can EITHER define your constraint "inline" with the column:

CREATE TABLE [dbo].[BatchStatus]
    ([BatchID] [uniqueidentifier] NOT NULL 
        CONSTRAINT [PK_BatchStatus_ID] PRIMARY KEY,
    [BatchStatus] [int] NULL
) ON [PRIMARY]

or then you can define it AFTER all the columns of the table

CREATE TABLE [dbo].[BatchStatus]
    ([BatchID] [uniqueidentifier] NOT NULL,
    [BatchStatus] [int] NULL,
    CONSTRAINT [PK_BatchStatus_ID] PRIMARY KEY CLUSTERED 
    (
        [BatchID] ASC
    )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

but you can't have both (partially)

Marc

marc_s
Cool! You are the man!
George2
A: 

Maybe this is what you really wanted. A constraint specifying the default value

Create TABLE [dbo].[BatchStatus](
    [BatchID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BatchStatus_ID] DEFAULT((0)),
    [BatchStatus] [int] NULL,
    CONSTRAINT [PK_BatchStatus_ID] PRIMARY KEY CLUSTERED 
    (
        [BatchID] ASC
    ) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
jitter
A: 

while using

USE mssqltips_tde; CREATE DATABASE ENCRYPTION KEY with ALGORITHM = AES_256 ENCRYPTION BY SERVER CERTIFICATE TDECert GO

getting error

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'KEY'. Msg 319, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

please help in resolving the same as i need to implement Encryption on my DB

John Saunders

related questions