使用日期格式将值简单插入 table

Simple Inserting values into table with Date format

我正在尝试将一些基本数据插入 table,但我一直收到错误消息。起初,我以为我在插入日期列时只是日期格式不正确,但现在我又在猜测了。

最初,第 4 列的日期格式为 2010 年 1 月 12 日,数据类型为 DATE。我已将格式更改为 2010-01-12 但我仍然遇到相同的错误。我可能在这里遗漏了一些简单的东西,但我不太确定它是什么。

我得到的错误是

Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.

这是 table...

的列
BookName (PK, Varchar(45), not null)
Genre (varchar(25), not null)
DateOfPublication (date,not null)
NoOfPages (int, not null)
WriterID (PK, FK, varchar(11), not null)
EditorID (FK, varchar(11), not null)
insert into BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Valley of Heroes','10','Fiction','2010-01-12',874,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('The Ruler''s Return','11','Fantasy','2012-03-14',765,'22');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('eRobot','11','Fantasy','2011-04-15',264,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('An Uncle''s Letters','12','Fiction','2012-06-12',258,'20');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Pretty flowers','13','Album','2013-01-31',148,'22');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('A Tale of Lions','12','Fantasy','2012-08-17',301,'21');
insert into BOOK (BookName, genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('eRobot','13','Sci Fi','2012-10-04',465,'23');

总是 在您执行 insert 时列出列。列名和值的顺序必须相同:

insert into book (BookName, WriterID, Genre, DateOfPublication, , NoOfPages, EditorID)
    values ('Valley of Heroes', '10', 'Fiction', '2010-01-12', 874, '20');

您的值列表似乎有误。 值“10”应该在最后..

你的陈述

insert into BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Valley of Heroes','10','Fiction','2010-01-12',874,'20');

会尝试将值 'Fiction' 添加到 DateOfPublication 列。

要更正它,请使用

insert into BOOK (BookName, Genre, DateOfPublication, NoOfPages, WriterID, EditorID)
values ('Valley of Heroes','Fiction','2010-01-12',874,'10','20');