简体   繁体   English

如何将默认属性值设置为今天的日期-MS SQL Server

[英]How to set default attribute value to today's date - MS SQL Server

I am building a database application with C# in Visual Studio 2013. The application is meant to email a customer 90 days after the date of their visit so, when they come in, their info is taken and today's date is stored with their other information. 我正在Visual Studio 2013中使用C#构建数据库应用程序。该应用程序旨在在客户访问日期后90天向其发送电子邮件,因此,当他们进入时,将获取他们的信息,并将今天的日期与其他信息一起存储。

I have a table called Customer ready to store customer information but instead of having the user type the date for each customer and risk them screwing up the format, I would like the default value for the column named DateOfVisit to be today's date. 我有一个名为Customer的表准备存储客户信息,但不是让用户键入每个客户的日期而是冒险DateOfVisit了格式,我希望名为DateOfVisit的列的默认值为今天的日期。

这是我的客户表的样子

I am looking for what to type into the default column. 我正在寻找要在默认列中键入的内容。 Something along the lines of getDate() that will give me today's date in a format that I can easily check against another future date and see if 90 days have past or not. getDate() ,它将以一种格式提供给我今天的日期,我可以轻松地对照另一个将来的日期,查看是否有90天过去了。

EXTRA: From the picture, you may notice I have the emailFlag 's data type set to bit because I thought that would be the closest thing to a boolean. 额外:从图片中,您可能会注意到我将emailFlag的数据类型设置为bit,因为我认为这是最接近布尔值的东西。 The purpose of emailFlag is that before a customer is sent an email, that flag will be checked and if it's false the email will go through and the flag will be set to true, so as to avoid spamming the customer emails in case the program messes up. emailFlag的目的是在向客户发送电子邮件之前,将检查该标志,如果错误则电子邮件将通过并且标志将设置为true,以避免在程序混乱的情况下向客户发送电子邮件起来。 Is this a good idea? 这是一个好主意吗? Do you have any suggestions as to how I could do it better? 您对我如何做得更好有任何建议?

tl;dr TL;博士
I need a method that will give me today's date to store in a database and that I can compare with other dates. 我需要一个方法,它将今天的日期存储在数据库中,并且我可以与其他日期进行比较。

Try this: 尝试这个:

In case you already have the table created 如果您已经创建了表

Alter Table dbo.customer Drop Column DateOfVisit;

Lets alter the table and add a field with the deafult value date 让我们改变表并添加一个带有deafult值日期的字段

Alter Table dbo.customer ADD DateOfVisit DATETIME NOT NULL DEFAULT (GETDATE());

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

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