注册 登录
编程论坛 SQL Server论坛

请aei135老师进——关于一个日期比较的触发器。

arook 发布于 2010-12-21 17:42, 717 次点击
请问aei135老师:
    table1
 ID      PLANDATE
001     2010-12-20

我想做一个触发器,如果PLANDATE字段小于系统当天的日期,那么就改成系统当天的日期。
只有本站会员才能查看附件,请 登录

 我知道 select @sdate=select getdate()是错的,但是我不知道怎么弄才行~~
3 回复
#2
aei1352010-12-22 10:11
你的PLANDATE什么字段类型的啦?我下面的是PLANDATE VARCAR(10)
方法一:
create trigger sdate
on table1
for update,insert
as
set nocount on
declare @sdate varchar(10)
select @sdate=convert(varchar(10),getdate(),120)
update table1 set plandate=case when inserted.plandate<@sdate then @sdate else inserted.plandate end
from table1,inserted where table1.id=inserted.id
方法二:
create trigger sdate
on table1
for update,insert
as
set nocount on
declare @sdate varchar(10)
select @sdate=convert(varchar(10),getdate(),120)
update table1 set plandate=@sdate
from table1,inserted where table1.id=inserted.id
and inserted.plandate<@sdate
#3
juejitianya2010-12-22 11:09
"select @sdate=select getdate()"
应该调整为“select @sdate = getdate()”或者“set @sdate= getdate()”
#4
arook2010-12-22 17:32
多谢aei135老师,
测试了第一个,OK
1