如何自动更新数据库中的记录?????急!!!急!!!急!!!

tiandiqing 2004-05-11 12:08:28
我有一个表:

id name order pubdate flag
1 aaa a122 2004-5-10 1
2 bbb a1223 2004-5-11 1
3 ccc a1224 2004-5-12 1
4 ddd a1225 2004-5-12 0


我的问题是这样的,如过pubdate的日期为当今日期,则flag值就自动设为0

我自己写了个存储过程

CREATE PROCEDURE proc_material_price_guoqi
as
begin

declare crs_material scroll cursor
for select pubdate from material for update of pubdate

open crs_material

declare @row int
set @row = @@cursor_rows

while @row > 0
begin
declare @mydate datetime
fetch next from crs_material into @mydate
if (CONVERT(char(10), @mydate, 126) = CONVERT(char(10), GETDATE(), 126))
update material set flag=0 where current of crs_material

set @row = @row - 1

end

close crs_material
deallocate crs_material

end
GO

但我知道对不对?
不对的话怎么写,放到哪里sql server才能自动的监测着它?
大家帮忙!
...全文
57 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
koskinen 2004-05-11
  • 打赏
  • 举报
回复
exits 改为 exists
koskinen 2004-05-11
  • 打赏
  • 举报
回复
我决的写个触发器就可以了
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'trig_mineAND' type = 'TR')
DROP TRIGGER trig_mine

create trigger trig_mine on 表
for insert,update
as
update 表 set flay=0 where exits(select * from inserted where pubdate=getdate())
tiandiqing 2004-05-11
  • 打赏
  • 举报
回复
楼上的,你也是高手,是不是放到作业里去啊
internetcsdn 2004-05-11
  • 打赏
  • 举报
回复
夜了,
帮你UP,
如何楼主精神还好的话,
不妨自己试下看有没有正确啊.
zh_zh_y 2004-05-11
  • 打赏
  • 举报
回复
关注,我不会,帮你顶一下。
zjcxc 2004-05-11
  • 打赏
  • 举报
回复
这样设置公式后,flag字段的值就会自动生成,不需要自己去做修改调整
zjcxc 2004-05-11
  • 打赏
  • 举报
回复
--用语句就是:

--如果flag字段已经建好,先删除它
alter table 你的表 drop column flag
go

--再添加上去
alter table 你的表 add flag as case datediff(day,pubdate,getdate()) when 0 then 0 else 1 end

zjcxc 2004-05-11
  • 打赏
  • 举报
回复
--企业管理器--右键你的表--设计表--选择flag字段--在该字段的公式属性中输入:
case datediff(day,pubdate,getdate()) when 0 then 0 else 1 end


ljh13448874 2004-05-11
  • 打赏
  • 举报
回复
Update material
Set flag =Case When Cast(putdate As Char(10))=Cast(Getdate() As Char(10)) Then 0 Else 1 End

koskinen 2004-05-11
  • 打赏
  • 举报
回复
create table t (id char(5),name char(6),pubdate datetime,flag int)

IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'trig_mine' and type = 'TR')
DROP TRIGGER trig_mine
go
create trigger trig_mine on t
for insert,update
as
update t set flag=0 where exists(select * from inserted where cast(pubdate as char(10))=cast(getdate() as char(10)))

insert into t
select 'aa1','a122','2004-5-11',1

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧