【问题标题】:How to insert into a table with a column named 'when' and 'user' [duplicate]如何插入包含名为“when”和“user”的列的表中[重复]
【发布时间】:2014-10-21 23:38:42
【问题描述】:

如何以编程方式将行/记录插入到表中(由第三方软件生成)其中一个字段被命名为whenuser,它们恰好是 sql 关键字。 p>

sql命令是:

INSERT INTO APPT 
(ApptID,Type,Descrip,When,Flag,User,DTS) 
VALUES 
(41,0,'Test Note','20/10/2014 1:42:00 PM',16,'AP','22/10/2014 8:24:01 AM') 

这给出了错误:

Error 156: Incorrect syntax near the keyword 'When'.
Error 156: Incorrect syntax near the keyword 'User'.

【问题讨论】:

  • 简单的解决方案:不要对列名或表名使用保留字。相反,请考虑类似UserIdApptDate
  • 嗯...有时您无法控制架构

标签: sql sql-server


【解决方案1】:

要转义列(或表)名称中的保留关键字,请将列名称放在方括号中:

INSERT INTO APPT (ApptID,Type,Descrip,[When],Flag,[User],DTS) 

【讨论】:

    【解决方案2】:

    所有的 SQL 关键字都应该放在方括号内。

    INSERT INTO APPT 
    (ApptID,Type,Descrip,[When],Flag,[User],DTS) 
    VALUES 
    (41,0,'Test Note','20/10/2014 1:42:00 PM',16,'AP','22/10/2014 8:24:01 AM')
    

    【讨论】:

      猜你喜欢
      • 2020-01-19
      • 2020-07-02
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2016-07-01
      • 2018-03-06
      相关资源
      最近更新 更多