GetDate On Insert但不更新

编程入门 行业动态 更新时间:2024-10-28 08:19:19
本文介绍了GetDate On Insert但不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个c#程序,它定期将更新和插入命令写入db。在我的sql表中,我有一个名为modifiedDate的列,假设在将行插入表中时获取当前日期时间。我用这样的方式来设置这个列。

I have a c# program that periodically writes update and insert commands to a db. In my sql table i have a column called modifiedDate that is suppose to get the current datetime when the row is inserted into the table. I used a stement like this to set up this column that way.

ALTER TABLE MyTable ADD CONSTRAINT DF_MyTable_MyColumn DEFAULT GETDATE() FOR MyColumn

当我的应用程序向db发送一个insert语句时,这很有效,但我注意到当我执行更新语句时,modifiedDate字段不会更新当前的日期和时间。 我会想到,因为该字段设置为检索新数据发生的日期我在插入时根本不包括它声明。

And this works great when my app sends an insert statement to the db, but i've noticed that when i do an update statement, the modifiedDate field does not update with the current date and time. I would have thought that since that field was setup to retrieve the date when new data occurred i was not including it at all in my insert statement.

Update table set name= 'Bob', address = 'Main Street'' where id = '28'

每次插入或更新数据时,如何允许此字段为getdate()? 先谢谢!

How can I allow this field to getdate() everytime data is inserted or updated? Thanks in Advance!

推荐答案

默认约束仅适用于插入,更新使用触发器。触发器是最好的方法,因为这个逻辑与表紧密相关,而不是与应用程序紧密相关。这只是我能想到的唯一明显的正确使用触发器,除了更细粒度的参照完整性。 请参阅: [ ^ ] 还要查看一个类似的主题:此处 [ ^ ]
- Amit
A default constraint only works on inserts, for an update use a trigger. Triggers are the best way, because this logic is intimately associated with the table, and not the application. This is just about the only obvious proper use of a trigger that I can think of, apart from more granular referential integrity. Please see : Create a Modified On Column in SQL Server[^] And also check one similar thread : here[^]
--Amit

更多推荐

GetDate On Insert但不更新

本文发布于:2023-10-23 03:35:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但不   GetDate   Insert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!