简体   繁体   English

如何使用数据类型时间在SQL Server中插入时间

[英]How to INSERT time in SQL Server using data type time

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

You need to surround the DATE and TIME values with apostrophes: 您需要使用撇号包围DATETIME值:

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. 但是,您的表格设计似乎不正确,其中一些列应该是数字而不是字符串,因此请查看您的表格设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM