2009-06-21 13 views
1

Ich verwende SQL Server 2008 Management Studio, um die folgenden SQL-Anweisungen auszuführen, und hier ist die zugehörige Fehlermeldung von SQL Server Management Studio. Irgendwelche Ideen was ist falsch?Was ist falsch mit meinem create table SQL?

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. 

Antwort

1

Sie definieren die „PK_BatchStatus_ID“ Constraint zweimal - einmal auf der Linie, wo Sie die BatchID Feld definieren, einmal am Ende der Tabellendefinition.

können Sie ENTWEDER definieren Sie Ihre Constraint "inline" mit der Säule:

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

oder dann können Sie es NACH alle Spalten der Tabelle

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] 

aber Sie definieren kann nicht beide haben (teilweise)

Marc

+0

Cool! Du bist der Mann! – George2

1

Versuchen Sie folgendes:

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 
+0

Ihre SQL-Anweisung ist die gleiche wie meine? Verwirrt ... :-) – George2

+2

Es ist nicht. Er entfernte CONSTRAINT [PK_BatchStatus_ID] nach der BatchID-Definition. – jitter

+0

Danke, deine Lösung funktioniert! Aber ich bin so verwirrt, wir erlauben keine Einschränkungen nach der Spaltendefinition? Ich kopiere nur eine praktikable SQL-Anweisung aus meinen älteren SQL Server Version (2005) -Projekten. Ist es eine neue Einschränkung oder Grammatik in SQL Server 2008? – George2

0

Vielleicht wollten Sie das wirklich. Eine Einschränkung, die den Standardwert angibt

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] 
Verwandte Themen