简体   繁体   English

如何将 DATETIME 类型用于变量?

[英]How to use the DATETIME type for a variable?

What I want to do is make a variable date_arrived that stores a date and time value when a Client makes a Reservation for a hotel room.我想要做的是创建一个变量date_arrived来存储客户预订酒店房间时的日期和时间值。 Then, I want to Insert into the table a date and time value.然后,我想在表中Insert into一个日期和时间值。 The only way I can think of doing this is like so:我能想到的唯一方法是这样的:

CREATE TABLE Reservation
(
    id_reservation SMALLINT IDENTITY(1 ,1) PRIMARY KEY,
    date_arrived DATETIME NOT NULL
)

INSERT INTO Reservation 
VALUES ( ??? );

But I've seen people use the DATETIME type with @datetime syntax but I don't understand this and don't know if it will work with my example.但是我看到人们使用带有@datetime语法的DATETIME类型,但我不明白这一点,也不知道它是否适用于我的示例。 I know using varchar(n) is not convenient.我知道使用varchar(n)不方便。 So what is the best way to register a date and time values into a variable?那么将日期和时间值注册到变量中的最佳方法是什么?

I think you're confusing what you're asking for, it sounds like what you need is along the lines of:我认为你混淆了你的要求,听起来你需要的是:

(You haven't tagged your database but it looks like SQL Server syntax) (您尚未标记您的数据库,但它看起来像 SQL 服务器语法)

CREATE TABLE Reservation
(
    Reservation_Id INT NOT NULL IDENTITY(1 ,1) PRIMARY KEY,
    Booking_Date DATETIME NOT NULL default (GETDATE()),
    Arrival_Date DATETIME NOT NULL,
    Duration TINYINT NOT NULL default(1),
    Checkout_Date as DATEADD (day, Duration, Arrival_date)
)
  • Reservation_Id will link you to your other data for the reservation eg guests. Reservation_Id会将您链接到您的其他预订数据,例如客人。
  • Booking_Date is "right now" when you insert the data, ie are making the booking, the date on which they booked - this is automatically populated当您插入数据时, Booking_Date是“现在”,即正在预订,他们预订的日期 - 这是自动填充的
  • Arrival_Date is the date/time they are due to arrive/room reserved for etc, you populate this Arrival_Date是他们预定到达的日期/时间/预留房间等,您填充此
  • Duration - how many days/nights etc Duration - 多少天/多少夜等
  • Checkout_Date - you don't fill this in, it's caclulated from the Arrival_Date + Duration Checkout_Date - 你不填写这个,它是从 Arrival_Date + Duration 计算出来的

You would insert data, assuming your values are passed as two variables,您将插入数据,假设您的值作为两个变量传递,

insert into Reservation (Arrival_Date, Duration)
values (@ArrivalDate, @Duration)
select SCOPE_IDENTITY() --< returns the Reservation_Id for use elsewhere.

You can insert value as:您可以将值插入为:

Insert into reservation values(now());

In MySQL you can use now() for inserting values in your table.在 MySQL 中,您可以使用now()在表中插入值。

This can be taken care of by the database as well here is a syntax for this.这也可以由数据库来处理,这里有一个语法。

CREATE TABLE Reservation(id_reservation SMALLINT IDENTITY(1,1) PRIMARY KEY,
    date_arrived DATETIME Default now() NOT NULL)

Here is an example to fill values explicitly.这是一个显式填充值的示例。

INSERT INTO reservation (id_reservation,date_arrived) VALUES (678,'2020-11-10 11:26:27');

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

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