Interview Question in SQL Server Clustering


 

Interview Question :: Whats wrong with this SQL query


USE [Cost]
GO
/****** Object: Table [dbo].[CostTable] Script Date: 06/08/2007 12:05:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CostTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[AccountingMonth] [int] NOT NULL,
[ExpenseCategory] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
[PrimaryCostArea] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
[SecondaryCostArea] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,
[Cost] [float] NULL,
CONSTRAINT [PK_CostTable] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
---------------

Msg 170, Level 15, State 1, Line 11
Line 11: Incorrect syntax near '('.
---------------


Which is line "[ExpenseCategory] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL" but I can't see anything wrong with it...
Answers to "Whats wrong with this SQL query"
RE: Whats wrong with this SQL query?

This is weird, the syntax looks all correct, yet I get the same thing in Query Analyzer, it seems the error originates from the PAD_INDEX and IGNORE_DUP_KEY syntax, but that looks correct also, if I run it without this, it Parses fine. Strange one.



I havent tried it but perhaps you could create the table and then ALTER TABLE and add the indexes.



USE [Cost]

GO

/****** Object: Table [dbo].[CostTable] Script Date: 06/08/2007 12:05:05 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE TABLE [dbo].[CostTable](

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

[AccountingMonth] [int](4) NOT NULL,

[ExpenseCategory] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,

[PrimaryCostArea] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,

[SecondaryCostArea] [nvarchar](50) COLLATE Latin1_General_CI_AS NULL,

[Cost] [float] NULL,

CONSTRAINT [PK_CostTable] PRIMARY KEY CLUSTERED

(

[ID1] ASC

) ON [PRIMARY]

) ON [PRIMARY]
 
Vote for this answer ::  
RE: Whats wrong with this SQL query?

This is not a query, this is DDL. But if I had to take a guess, the size of your column should be associated to your datatype ([nvarchar(50)] rather than [nvarchar](50)). Not a platform I'm familiar with.. but worth guessing....
 
Vote for this answer ::  
RE: Whats wrong with this SQL query?

I don't see anything obvious besides the fact that on this line:



CREATE TABLE [dbo].[CostTable](



you never closed the ()
 
Vote for this answer ::  
RE: Whats wrong with this SQL query?

Isn't it [nvarchar(50)]?
 
Vote for this answer ::  
Update Alert Setting