谁来帮我改下这个触发器啊 懂触发器的请进

zilu2008 2006-08-30 03:25:41

表 [产品] 里有 [产品ID] [产品数量] [断货时间] 等字段

断货时间 与 产品数量 的关系是
当 产品数量<=0 时 断货时间=getdate()
当 产品数量>0 时 断货时间=null

下面是我写的触发器 当然是错的 希望高手帮忙改正
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
declare @stock int
declare @stime datetime(8)
set @stock=(select 产品数量 from 产品),
set @stime=(select 断货时间 from 产品),
if (@stock<=0 and isdate(@stime)=0)
begin
update 产品 set 断货时间=getdate()
end
if (@stock>0 and isdate(@stime)=1)
begin
update 产品 set 断货时间=null
end

这是第二次发问了
原先问题:
表A里有字段A1 A2
A1是数据类型的,大小会随时变化
A2根据A1求值
当A1<=0 并且A2要为空(不希望A2变化) 时A2等于此时的时间,以后不再改变除非A1>0
当A1>0时 A2为空
我是这样写的
case when a1<=0 and isdate(a2)=0 then getdate() else '' end
这是错的 因为a2不能包含在自己的公式里
求正确的答案

http://community.csdn.net/Expert/topic/4980/4980679.xml?temp=.1334497
...全文
295 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zilu2008 2006-08-31
  • 打赏
  • 举报
回复
另一正确答案 不知道哪个好

CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
update 产品 set 断货时间=getdate() where
断货时间 is null and 产品数量 <= 0

update 产品 set 断货时间=null where
断货时间 is not null and 产品数量 > 0
zilu2008 2006-08-31
  • 打赏
  • 举报
回复
刚刚测试通过的标准正确答案


CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
DECLARE @PID INT
declare @stock int
declare @stime datetime
IF @@ROWCOUNT = 1
begin
SELECT @stock=[产品数量], @stime=[断货时间], @PID=[产品ID] from INSERTED
if (@stock<=0 and @stime is null)
update 产品 set 断货时间=getdate() WHERE [产品ID] = @PID
else
if (@stock>0 and @stime is not null)
update 产品 set 断货时间=null WHERE [产品ID] = @PID
end

else
begin
declare tb cursor for select [产品id],产品数量,stime from inserted
open tb
fetch next from tb into @PID,@stock,@stime
while @@fetch_status=0
begin
if (@stock<=0 and @stime is null)
update 产品 set 断货时间=getdate() WHERE [产品ID]= @PID
else
if (@stock>0 and @stime is not null)
update 产品 set 断货时间=null WHERE [产品ID] = @PID
fetch next from tb into @PID,@stock,@stime
end

close tb
deallocate tb
end
toto1980 2006-08-30
  • 打赏
  • 举报
回复
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS

update 产品 set 断货时间=getdate() from 产品 , inserted where
产品.产品ID = inserted.产品ID and inserted.产品数量 < 0

update 产品 set 断货时间=null from 产品 , inserted where
产品.产品ID = inserted.产品ID and inserted.产品数量 > 0
zilu2008 2006-08-30
  • 打赏
  • 举报
回复
这是偶自己写的 检查语法没错 但不能更新数据了 已更新就没反映

请高人指点迷津

CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
DECLARE @PID INT
declare @stock int
declare @stime datetime
IF @@ROWCOUNT = 1

SELECT @stock=[stock], @stime=[断货时间], @PID=[ID] from INSERTED

if (@stock<=0 and @stime is null)
begin
update 产品 set 断货时间=getdate() WHERE [产品ID] = @PID
end

if (@stock>0 and @stime is not null)
begin
update 产品 set 断货时间=null WHERE [产品ID] = @PID
end

else'多行循环
begin
declare tb cursor local for select 产品id,产品数量,断货时间 from inserted
open tb
fetch next from tb into @PID,@stock,@stime
while @@fetch_status=0
if (@stock<=0 and @stime is null)
begin
update 产品 set 断货时间=getdate() WHERE [产品ID] = @PID
end
if (@stock>0 and @stime is not null)
begin
update 产品 set 断货时间=null WHERE [产品ID] = @PID
end
close tb
deallocate tb
end
ly342540479 2006-08-30
  • 打赏
  • 举报
回复
最近开始学习trigger
zilu2008 2006-08-30
  • 打赏
  • 举报
回复
现在已经有单行的答案了
如果同时更新多行改怎么写呢 谁来写个循环啊
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
DECLARE @PID INT
declare @stock int
declare @stime datetime(8)
SELECT @stock=[产品数量], @stime=[断货时间], @PID=[产品ID] from INSERTED

if (@stock<=0 and (@stime is null))
begin
update 产品 set 断货时间=getdate() WHERE [产品ID] = @PID
end

if (@stock>0 and (@stime is not null))
begin
update 产品 set 断货时间=null WHERE [产品ID] = @PID
end
specialsoldier 2006-08-30
  • 打赏
  • 举报
回复
CREATE TRIGGER [uptime] ON [产品]
FOR INSERT, UPDATE
AS
declare @stock int
declare @stime datetime(8)
set @stock=(select 产品数量 from inserted),
set @stime=(select 断货时间 from inserted),
if (@stock<=0 and isdate(@stime)=0)
begin
update 产品 set 断货时间=getdate() from 产品 join inserted on 产品.id=inserted.产品ID
end
if (@stock>0 and isdate(@stime)=1)
begin
update 产品 set 断货时间=null from 产品 join inserted on 产品.id=inserted.产品ID
end
specialsoldier 2006-08-30
  • 打赏
  • 举报
回复
set @stock=(select 产品数量 from 产品),
set @stime=(select 断货时间 from 产品),
-------------
先把上面的表"产品"改为inserted再说:
set @stock=(select 产品数量 from inserted),
set @stime=(select 断货时间 from inserted),

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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