如何使用数据类型时间在 SQL Server 中插入时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48487984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to INSERT time in SQL Server using data type time
提问by Mango
I am writing the query to insert data to the table but I keep getting an error:
我正在编写查询以向表中插入数据,但我不断收到错误消息:
Incorrect Syntax near ':'`
':'` 附近的语法不正确
This is the query that create the table:
这是创建表的查询:
CREATE TABLE Consultation_Slot
(
Slot_ID CHAR(7) NOT NULL PRIMARY KEY,
Appointment_Purpose VARCHAR(255) NULL,
Appointment_Status VARCHAR(11) NOT NULL,
Cancellation_Reason VARCHAR(255) NULL,
Slot_Time time(7) NOT NULL,
Slot_Date DATE NOT NULL,
Slot_Day CHAR(10) NOT NULL,
Room_No VARCHAR(5) NOT NULL,
Lecturer_ID CHAR(3) NOT NULL
REFERENCES Lecturer(Lecturer_ID),
Student_ID CHAR(6) NOT NULL
REFERENCES Student(Student_ID),
Schedule_ID CHAR(5) NOT NULL
REFERENCES Weekly_Consultation_Schedule(Schedule_ID)
)
This is the INSERT
statement that I tried to execute:
这是INSERT
我试图执行的语句:
INSERT INTO Consultation_Slot
VALUES (1000000,'I need to learn maths','avaliable','', 13:30, 1-28-2018,
'Sunday', 'RN001', 1111, 880001, 30001);
GO
回答by Racil Hilan
You need to surround the DATE
and TIME
values with apostrophes:
您需要用撇号将DATE
和TIME
值括起来:
INSERT INTO Consultation_Slot VALUES
(1000000,'I need to learn maths','avaliable','', '13:30', '1-28-2018',
'Sunday', 'RN001', 1111, 880001, 30001);
Only number type values don't need apostrophes.
只有数字类型值不需要撇号。
It is also recommended to surround all the other values in your queries with apostrophes. It's true that they are numbers, but the column types are string based, so providing the values as number will cause unnecessary implicit casting:
还建议使用撇号将查询中的所有其他值括起来。它们确实是数字,但列类型是基于字符串的,因此将值提供为数字会导致不必要的隐式转换:
INSERT INTO Consultation_Slot VALUES
('1000000','I need to learn maths','avaliable','', '13:30', '1-28-2018',
'Sunday', 'RN001', '1111', '880001', '30001');
However, it seems that your table design is incorrect, and some of those columns should be numbers not string, so review your table design.
但是,您的表格设计似乎不正确,其中一些列应该是数字而不是字符串,因此请检查您的表格设计。