-3

我有一张桌子AuditTrail

当我运行此查询时:

INSERT INTO AuditTrail
                     ( EntityName, Date, [User], Action, OldValue
                      , NewValue, ModuleName, EntityRef, EntityAttribute
                      , EntityAltRef) 
VALUES (aaa, '2012-02-07 00:00:00.000', sa, DELETE, '11', aaa, aaa, sa, aaa, aaa)

我收到一个错误

Incorrect Syntax Near Keyword 'DELETE'

我在这里做错了什么?

这是表结构

ID                numeric(18, 0)  Unchecked
EntityName        nvarchar(60)    Unchecked
Date              datetime        Unchecked
[User]            sysname         Unchecked
Action            varchar(30)     Unchecked
OldValue          varchar(1024)   Checked
NewValue          varchar(1024)   Checked
ModuleName        varchar(6)      Checked
EntityRef         varchar(30)     Checked
EntityAttribute   varchar(60)     Checked
EntityAltRef      varchar(64)     Checked
4

3 回答 3

2

'在 sa 和 delete 之间添加。

INSERT INTO AuditTrail ( EntityName, Date, [User], Action, OldValue, NewValue, ModuleName, EntityRef, EntityAttribute, EntityAltRef) 
VALUES ('aaa','2012-02-07 00:00:00.000','sa','DELETE','11','aaa','aaa','sa','aaa','aaa')

sa 是默认管理员用户,而 delete 是 SQL 中的关键字,您必须将它们作为文字插入,这就是为什么您必须用 封装它们',我假设这aaa也是一个字符串值。

于 2013-06-14T07:42:23.343 回答
1

DELETE 是命令,不能直接使用。用单引号括起来,这样databae就会明白它是字符串而不是命令。

INSERT INTO AuditTrail
                     ( EntityName, Date, [User], Action, OldValue
                      , NewValue, ModuleName, EntityRef, EntityAttribute
                      , EntityAltRef) 
              VALUES (aaa,'2012-02-07 00:00:00.000'
                     ,sa,'DELETE','11',aaa,aaa,sa,aaa,aaa);
于 2013-06-14T07:48:17.113 回答
1

这是因为DELETE它是一个保留关键字,所以你应该用引号转义,让数据库无法理解它是一个字段而不是命令。你也在使用

INSERT INTO AuditTrail ( EntityName, Date, [User], Action, OldValue, NewValue, ModuleName, EntityRef, EntityAttribute, EntityAltRef) VALUES (aaa,'2012-02-07 00:00:00.000',sa,'DELETE','11',aaa,aaa,sa,aaa,aaa)

检查您的其他字段并记住不是整数列的值需要用引号括起来'

于 2013-06-14T07:44:14.787 回答