【问题标题】:Incorrect syntax near 'a' with enum('a','b','c')带有 enum('a','b','c') 的 'a' 附近的语法不正确
【发布时间】:2018-10-20 08:26:26
【问题描述】:
CREATE TABLE ec_recurring 
(   
    [recurring id] int NOT NULL IDENTITY,   
    [price] decimal(10,4) NOT NULL,
    [frequency] enum('day','week','semi_month','month','year') NOT NULL,   
    [duration] cast(10 as int) unsigned NOT NULL,
    [cycle] cast(10 as int) unsigned NOT NULL,
    [trial status] tinyint(4) NOT NULL,
    [trial price] decimal(10,4) NOT NULL,
    [trial frequency] enum('day','week','semi_month','month','year') NOT NULL,   
    [trial duration] cast(10 as int) unsigned NOT NULL,
    [trial cycle] cast(10 as int) unsigned NOT NULL,
    [status] tinyint(4) NOT NULL,   
    [sort order] cast(11 as int) NOT NULL,
    [date created] datetime2 NOT NULL,   
    [date modified] datetime2 NOT NULL,
    [created by] varchar(20) NOT NULL,   
    [modified by] varchar(20) NOT NULL,
    [active] tinyint(4) NOT NULL,   

    PRIMARY KEY ([recurring id])
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 

这是我的代码。为什么这个错误会在接近白天和投射时发生?

消息 102,第 15 级,状态 1,第 3922 行
“day”附近的语法不正确

【问题讨论】:

  • 欢迎来到stackoverflow。请花一分钟时间回答tour,尤其是如何How to Ask,以及edit您的问题。
  • 错误是什么?还是我们必须猜测?
  • 那么,您使用的是哪个数据库系统?您放置的唯一标签是 java 和 c# - 这些都与这里无关,因为您没有显示任何 java 代码或 c# 代码。
  • sql server 数据库
  • sql server 你是说microsoft的sql server?没有enum 这样的东西。它来自 MySql。

标签: sql sql-server enums


【解决方案1】:

在微软的SQL Server中没有enum这样的东西,它来自MySql,但是你可以使用它的等价物:

mycol VARCHAR(10) NOT NULL CHECK (mycol IN('Useful', 'Useless', 'Unknown'))

Original Post

【讨论】:

    【解决方案2】:

    如果说的是T-SQL,那么day附近的错误说明语法不正确(就是T-SQL中没有enum关键字),应该是这样的:

    [frequency] VARCHAR(10) NOT NULL,
    CONSTRAINT ck_Enum CHECK ([frequency] IN('day','week','semi_month','month','year')), 
    

    考虑到您在错误上下文中使用 cast 的第二个错误,SQL 不知道列的类型。我相信你想写:

    [duration] INT DEFAULT 10 NOT NULL 
    CONSTRAINT ck_Duration CHECK ([duration] > 0), 
    

    额外检查持续时间是否无符号。你应该遵守语言的语法,因为你有更多的错误。举个例子:

    CREATE TABLE ec_recurring 
    ( 
    [recurring id]      INT IDENTITY NOT NULL
    CONSTRAINT pl_recId PRIMARY KEY, 
    
    [price]             DECIMAL(10, 4) NOT NULL,
    
    [frequency]         VARCHAR(10) NOT NULL,
    CONSTRAINT ck_Enum CHECK ([frequency] IN('day','week','semi_month','month','year')), 
    
    [duration]          INT DEFAULT 10 NOT NULL 
    CONSTRAINT ck_Duration CHECK ([duration] > 0), 
    
    [cycle]             INT DEFAULT 10 NOT NULL 
    CONSTRAINT ck_Cycle CHECK ([cycle] > 0), 
    
    [trial status]      TINYINT DEFAULT 4 NOT NULL, 
    
    [trial price]       DECIMAL(10, 4) NOT NULL, 
    
    [trial frequency]   VARCHAR(10) NOT NULL 
    CONSTRAINT ck_TrialEnum CHECK ([trial frequency] IN('day','week','semi_month','month','year')), 
    
    [trial duration]    INT DEFAULT 10 NOT NULL 
    CONSTRAINT ck_TrialDuration CHECK ([trial duration] > 0), 
    
    [trial cycle]       INT DEFAULT 10 NOT NULL 
    CONSTRAINT ck_TrialCycle CHECK ([trial cycle] > 0), 
    
    [status]            TINYINT DEFAULT 4 NOT NULL, 
    
    [sort order]        INT DEFAULT 11 NOT NULL, 
    
    [date created]      DATETIME2 NOT NULL, 
    
    [date modified]     DATETIME2 NOT NULL, 
    
    [created by]        DATETIME NOT NULL, 
    
    [modified by]       DATETIME NOT NULL, 
    
    [active]            TINYINT DEFAULT 4 NOT NULL, 
    );
    

    【讨论】:

    • 非常感谢您的努力。它正在工作
    • @VinoThini 如果某个答案解决了您的问题,您应该接受它,以便其他人知道问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2014-10-29
    • 2013-12-16
    相关资源
    最近更新 更多