简单地将值插入日期格式的表中

问题描述 投票:-1回答:2

我正在尝试将一些基本数据插入表中,但始终出现错误。起初,我以为插入日期列时的日期格式不正确,但现在我在猜测。

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

我得到的错误是

Msg 241,第16级,状态1,第2行从字符串转换日期和/或时间时转换失败。

这里是表格的列...

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');
sql sql-server ssms date-formatting
2个回答
0
投票

似乎您的值列表中有一个错误。值“ 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');

0
投票

Always列出进行insert时的列。列名称和值必须以相同的顺序:

insert into book (BookName, WriterID, Genre, DateOfPublication, , NoOfPages, EditorID)
    values ('Valley of Heroes', '10', 'Fiction', '2010-01-12', 874, '20');
© www.soinside.com 2019 - 2024. All rights reserved.