Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用if条件编写sql触发器_Sql_Triggers - Fatal编程技术网

使用if条件编写sql触发器

使用if条件编写sql触发器,sql,triggers,Sql,Triggers,我需要写这个触发器的帮助,触发器在产品表上,当hourslef=0时,它需要更新表Offer status='Expired'。我是sql新手,非常感谢您的帮助 到目前为止,我有: CREATE TRIGGER dbo.Product_NoUnitsAvailable ON dbo.Product FOR UPDATE AS IF (SELECT dbo.Product.hoursLeft FROM dbo.Product, inserted i WHERE dbo.Offer.P

我需要写这个触发器的帮助,触发器在产品表上,当hourslef=0时,它需要更新表Offer status='Expired'。我是sql新手,非常感谢您的帮助

到目前为止,我有:

 CREATE TRIGGER dbo.Product_NoUnitsAvailable
 ON dbo.Product
 FOR UPDATE
 AS

 IF
 (SELECT dbo.Product.hoursLeft FROM dbo.Product, inserted i WHERE dbo.Offer.ProductId = i.ProductId) = 0

BEGIN

UPDATE dbo.Offer
SET Status = 'Expired'
WHERE dbo.Offer.Product = Inserted.ProductId

END

我怀疑您想要的查询是这样的:

UPDATE o
    SET Status = 'Expired'
    FROM dbo.Offer o JOIN
         Inserted i
         ON o.Product = i.Product
    WHERE i.hoursLeft = 0;

不需要显式
if
语句;
join
负责过滤。

Gordon,我正在测试你的解决方案