关于sqlserver更新的那些个事儿

deninghe 2007-08-20 10:01:24
我有一更新触发器,当pic表的view(查看次数)更新时,自动更新另一个表的view字段
但是这个表还有其它更新操作,比如修改名称,url啦之类的
能否在触发器里做是更新了pic表的哪个字段呢?
还是这种情况下不适于用Update触发器呢?
...全文
190 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
brother2605 2007-08-20
  • 打赏
  • 举报
回复
一个update操作是可以分解为delete 和 insert 两个步骤地,你如果要判断浏览次数(假设)是增加还是减少,可以通过取 deleted表和insert 表里的两个值进行比较,这样可以得到到底是增加了还是减少了
deninghe 2007-08-20
  • 打赏
  • 举报
回复
楼上的好可爱啊 
还是谢谢了
亲一个 -0-
brother2605 2007-08-20
  • 打赏
  • 举报
回复
晕,当我没说过
brother2605 2007-08-20
  • 打赏
  • 举报
回复
触发器里可以做,你可以先判断是更新了哪个字段,然后再根据更新的字段再做相应的处理,你可以在一个触发器里为任意几个字段的更新去写相应的操作,判断哪个字段被更新了好像有个函数:updated(),我记不清楚了,建议去查下帮助文件
deninghe 2007-08-20
  • 打赏
  • 举报
回复
哎呀 刚发出去就触发了
高手看看 是不是这样啊
CREATE TRIGGER [upt_album_views] ON [dbo].[hl_ualbumpic]
FOR UPDATE
AS
---注释
---当用户浏览图片时 程序更新hl_ualbumpic.pview的浏览次数
---本触发器 当hl_ualbumpic.pview更新时 
---自动更新hl_ualbum.aview相册浏览次数
begin
declare @cnt int
declare @aid varchar(50)
select @cnt = pview,@aid = aid from inserted
--select @aid = aid from inserted
--单纯使用 + 1 可能会有逻辑错误
--当hl_ualbumpic.pview的执行操作是 - 1 时 
--hl_ualbum.aview也应该执行 - 1 操作,这时可能引起该逻辑错误
--不过通常情况下,我们还是执行 + 1 操作
--注意:
--这里做了更新字段的判断!!!
--因为有可能本表有多种update
--这里只针对pview浏览次数的更新做出操作
if update(pview)
begin
update hl_ualbum set aview=aview + 1 where aid = @aid
end
end
yrwx001 2007-08-20
  • 打赏
  • 举报
回复
create trigger tr_tb1_u on pic for update
as
begin
if exsits( select 1 from deleted,inserted where deleted.key = inserted.key AND deleted.view <> inserted.view )
update tb2 set view = inserted.view from inserted,tb2 where tb2.key = inserted.key

end
yrwx001 2007-08-20
  • 打赏
  • 举报
回复
create trigger tr_tb1_u on tb1 for update
as
begin
if exsits( select 1 from deleted,inserted where deleted.key = inserted.key)
update tb2 set view = inserted.view from inserted,tb2 where tb2.key = inserted.key

end
mengmou 2007-08-20
  • 打赏
  • 举报
回复
能否在触发器里做是更新了pic表的哪个字段呢?
---------
啥意思

22,209

社区成员

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

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